Ruby gem for Baidu API

前一个月专门写了一个Ruby版本的百度API( http://developer.baidu.com/dev), 取名 api4baidu,Github地址: https://github.com/lanrion/baidu

Rubygems: https://rubygems.org/gems/api4baidu

具体用法,在这里引用一下 homepage里的README

在Gemfile 安装:

gem "api4baidu"

配置你的应用信息:

$client = Baidu.configure do |config|
  config.api_key    = "you client id"
  config.api_secret = "you client secert"
end

这里举例一个 获取当前已经登录的用户信息的API:

$client.authorize_url

# 访问上面生成的链接,并复制授权码: "515a268fd483ff4df85d2d458d34b43a"
$client.token!("515a268fd483ff4df85d2d458d34b43a")

# 获取当前登录用户的用户uid、用户名和头像。
$client.get_loggedin_user

 => Result:
 {"uid"    => "1863251187",
 "uname"   => "0807515210",
 "portrait"=> "ba6f303830373531353231302c0a"}

在controller中使用:

class SessionController
  def oauth
    redirect_to $baidu.authorize_url(redirect_uri: "you redirect url")
  end

  def callback
    auth_code = params[:code]
    $baidu.token!(auth_code)
  end
end

更多的使用例子:

https://github.com/lanrion/baidu/blob/master/spec/feature_spec.rb

已经完成的API:

1)用户基础信息:

  1. 获取当前登录用户的信息: get_loggedin_user

  2. 返回指定用户的用户资料: get_user_info

  3. 平台授权相关的权限: get_app_permission

  4. 判断用户是否为应用用户: is_app_user

  5. 返回用户好友资料: get_friends

  6. 获得指定用户之间好友关系: areFriends

2)PCS:

  1. 获取当前用户空间配额信息: pcs_quota

  2. 上传单个文件: upload_single_file

  3. 下载单个文件: download_single_file

  4. 创建目录: create_directory

  5. 获取单个文件/目录的元信息: get_single_meta

  6. 删除单个文件/目录: delete_single_file

  7. 获取指定图片文件的缩略图: get_image_thumbnail

3)翻译

4)工具

  1. 查询IP地址所在地区: query_with_ip

这是目前已经完成的API。

猜你喜欢

转载自lanrion.iteye.com/blog/1965465