Project

baidu_apis

0.0
No commit activity in last 3 years
No release in over 3 years
Simple wrapper for the Baidu APIs
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 0
 Project Readme

BaiduApis

BaiduAPiStore的ruby wrapper,http://apistore.baidu.com/

Installation

Add this line to your application's Gemfile:

gem 'baidu_apis'

And then execute:

$ bundle

Or install it yourself as:

$ gem install baidu_apis

Usage

require 'baidu_apis'

设置api_key

BaiduApis.api_key = "blabla"

常规api请求

BaiduApis::Operation.get("apistore/weatherservice/weather") do |params|
  params["citypinyin"] = "shanghai"      
end  
=> {"errNum"=>0,
 "errMsg"=>"success",
 "retData"=>
  {"city"=>"上海",
   "pinyin"=>"shanghai",
   "citycode"=>"101020100",
   "date"=>"16-04-15",
   "time"=>"11:00",
   "postCode"=>"200000",
   "longitude"=>121.445,
   "latitude"=>31.213,
   "altitude"=>"19",
   "weather"=>"多云",
...
  • 目前仅支持get/post方法
  • url写在参数里,也可以是"apistore", "weatherservice", "weather"这样的形式
  • block里写request参数

使用helper方法

BaiduApis.iplookup("ip" => "8.8.8.8")
=> {"errNum"=>0,
 "errMsg"=>"success",
 "retData"=>{"ip"=>"8.8.8.8", "country"=>"美国", "province"=>"None", "city"=>"None", "district"=>"None", "carrier"=>"未知"}}

注册一个helper方法

BaiduApis::Helper.register_helper(:pm, {method: "get", url: "apistore/aqiservice/aqi"})
BaiduApis.pm("city" => "上海")
=> {"errNum"=>0, "retMsg"=>"success", "retData"=>{"city"=>"上海", "time"=>"2016-04-15T14:00:00Z", "aqi"=>77, "level"=>"良", "core"=>"颗粒物(PM10)"}}

设定param_key

BaiduApis::Helper.register_helper(:pm, {method: "get", url: "apistore/aqiservice/aqi", param_key: "city"})
BaiduApis.pm("上海")
=> {"errNum"=>0, "retMsg"=>"success", "retData"=>{"city"=>"上海", "time"=>"2016-04-15T14:00:00Z", "aqi"=>77, "level"=>"良", "core"=>"颗粒物(PM10)"}}

License

The gem is available as open source under the terms of the MIT License.