Coder Social home page Coder Social logo

baidu_apis's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.