OpenJd
京东开放平台 JOS Ruby SDK
感谢
代码参考了 open_taobao, 非常感谢 Ryan
京东开放平台
安装
添加下列代码到 Gemfile 文件
gem 'open_jd'
如果使用 patron 代替 Net::HTTP, 请在 Gemfile 中添加下列代码
gem 'patron'
执行 bundle 安装:
$ bundle
或者使用 gem 命令安装:
$ gem install open_jd
使用
Rails 使用 yaml 配置文件
运行 generator 创建配置文件:
$ rails g open_jd:install
上述命令会在 config 目录创建 jd.yml 配置文件
打开这个文件并填写您在京东开放平台的相关信息
注意: app_key
, secret_key
, endpoint
为必填项
其中 app_key
和 secret_key
会注册为 ENV['JD_API_KEY']
, ENV['JD_SECRET_KEY']
使用 OpenJd.get
或 OpenJd.post
:
hash = OpenJd.post(
method: 'jingdong.ware.product.detail.search.list.get',
fields: { skuId: params[:id], isLoadWareScore: false, client: 'm' }
)
返回值为 Hash 格式.
另外也可以使用 OpenJd.get!
和 OpenJd.post!
在发生错误时会抛出 OpenJd::Error
.
在 Ruby 代码中使用
OpenJd.config = {
'app_key' => 'test',
'secret_key' => 'test',
'endpoint' => 'http://gw.api.jd.com/routerjson'
}
OpenJd.initialize_session
hash = OpenJd.get(
method: 'jingdong.ware.product.detail.search.list.get',
fields: { skuId: params[:id], isLoadWareScore: false, client: 'm' }
)
查看请求字符串
如果需要查看发出的请求字符串, 请使用 OpenJd.url 方法
OpenJd.url(
method: 'jingdong.ware.product.detail.search.list.get',
fields: { skuId: params[:id], isLoadWareScore: false, client: 'm' }
)
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request