baidu_apis 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +81 -0
- data/lib/baidu_apis.rb +30 -0
- data/lib/baidu_apis/helper.rb +52 -0
- data/lib/baidu_apis/operation.rb +32 -0
- data/lib/baidu_apis/version.rb +3 -0
- metadata +78 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2a4acb58e16246c374a114e85707b6ceb456d059
|
4
|
+
data.tar.gz: 7a13ce081e41d48018801932705b6a02f646e35c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4afeadbb1d8da4bd7c725c1a96d531c0a9e88f7e17cca6e36fa0c2554e194ce9f542a916eb3af6e6516663ffefbfdc10722186db51a8c5f9bfe6e7560d354474
|
7
|
+
data.tar.gz: f6d2eeae8ba7168b47d475f62a3672a345cbf16e1c948c1b9eef781af7e93241e9fba0a211adc551f35e82fe34ed777bcacca6e3b72da4ace3134d71bc6d6f19
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 adamshen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# BaiduApis
|
2
|
+
|
3
|
+
BaiduAPiStore的ruby wrapper,[http://apistore.baidu.com/](http://apistore.baidu.com/)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'baidu_apis'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install baidu_apis
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'baidu_apis'
|
25
|
+
```
|
26
|
+
|
27
|
+
#### 设置api_key
|
28
|
+
```ruby
|
29
|
+
BaiduApis.api_key = "blabla"
|
30
|
+
```
|
31
|
+
|
32
|
+
#### 常规api请求
|
33
|
+
```ruby
|
34
|
+
BaiduApis::Operation.get("apistore/weatherservice/weather") do |params|
|
35
|
+
params["citypinyin"] = "shanghai"
|
36
|
+
end
|
37
|
+
=> {"errNum"=>0,
|
38
|
+
"errMsg"=>"success",
|
39
|
+
"retData"=>
|
40
|
+
{"city"=>"上海",
|
41
|
+
"pinyin"=>"shanghai",
|
42
|
+
"citycode"=>"101020100",
|
43
|
+
"date"=>"16-04-15",
|
44
|
+
"time"=>"11:00",
|
45
|
+
"postCode"=>"200000",
|
46
|
+
"longitude"=>121.445,
|
47
|
+
"latitude"=>31.213,
|
48
|
+
"altitude"=>"19",
|
49
|
+
"weather"=>"多云",
|
50
|
+
...
|
51
|
+
```
|
52
|
+
- 目前仅支持get/post方法
|
53
|
+
- url写在参数里,也可以是"apistore", "weatherservice", "weather"这样的形式
|
54
|
+
- block里写request参数
|
55
|
+
|
56
|
+
#### 使用helper方法
|
57
|
+
```ruby
|
58
|
+
BaiduApis.iplookup("ip" => "8.8.8.8")
|
59
|
+
=> {"errNum"=>0,
|
60
|
+
"errMsg"=>"success",
|
61
|
+
"retData"=>{"ip"=>"8.8.8.8", "country"=>"美国", "province"=>"None", "city"=>"None", "district"=>"None", "carrier"=>"未知"}}
|
62
|
+
```
|
63
|
+
|
64
|
+
#### 注册一个helper方法
|
65
|
+
```ruby
|
66
|
+
BaiduApis::Helper.register_helper(:pm, {method: "get", url: "apistore/aqiservice/aqi"})
|
67
|
+
BaiduApis.pm("city" => "上海")
|
68
|
+
=> {"errNum"=>0, "retMsg"=>"success", "retData"=>{"city"=>"上海", "time"=>"2016-04-15T14:00:00Z", "aqi"=>77, "level"=>"良", "core"=>"颗粒物(PM10)"}}
|
69
|
+
```
|
70
|
+
|
71
|
+
#### 设定param_key
|
72
|
+
```ruby
|
73
|
+
BaiduApis::Helper.register_helper(:pm, {method: "get", url: "apistore/aqiservice/aqi", param_key: "city"})
|
74
|
+
BaiduApis.pm("上海")
|
75
|
+
=> {"errNum"=>0, "retMsg"=>"success", "retData"=>{"city"=>"上海", "time"=>"2016-04-15T14:00:00Z", "aqi"=>77, "level"=>"良", "core"=>"颗粒物(PM10)"}}
|
76
|
+
```
|
77
|
+
|
78
|
+
## License
|
79
|
+
|
80
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
81
|
+
|
data/lib/baidu_apis.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require "json"
|
2
|
+
require "faraday"
|
3
|
+
|
4
|
+
require_relative "baidu_apis/version"
|
5
|
+
require_relative "baidu_apis/operation"
|
6
|
+
require_relative "baidu_apis/helper"
|
7
|
+
|
8
|
+
module BaiduApis
|
9
|
+
class << self
|
10
|
+
attr_accessor :api_key
|
11
|
+
|
12
|
+
def method_missing(method_id, *args, &block)
|
13
|
+
if BaiduApis::Helper.respond_to?(method_id)
|
14
|
+
config = BaiduApis::Helper.send(method_id)
|
15
|
+
|
16
|
+
params = get_params(config, args)
|
17
|
+
response = BaiduApis::Operation.req(config[:method], config[:url], params)
|
18
|
+
return JSON.parse(response.body)
|
19
|
+
end
|
20
|
+
|
21
|
+
super
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
def get_params(config, args)
|
26
|
+
return args.first if args.first.is_a?(Hash)
|
27
|
+
{config[:param_key] => args.first}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module BaiduApis
|
2
|
+
class Helper
|
3
|
+
class << self
|
4
|
+
def stock
|
5
|
+
{
|
6
|
+
method: "get",
|
7
|
+
url: "apistore/stockservice/stock"
|
8
|
+
}
|
9
|
+
end
|
10
|
+
|
11
|
+
def weather
|
12
|
+
{
|
13
|
+
method: "get",
|
14
|
+
url: "apistore/weatherservice/cityname",
|
15
|
+
param_key: "cityname"
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
def phone
|
21
|
+
{
|
22
|
+
method: "get",
|
23
|
+
url: "apistore/mobilenumber/mobilenumber",
|
24
|
+
param_key: "phone"
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
def iplookup
|
30
|
+
{
|
31
|
+
method: "get",
|
32
|
+
url: "apistore/iplookupservice/iplookup",
|
33
|
+
param_key: "ip"
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
def meinv
|
38
|
+
{
|
39
|
+
method: "get",
|
40
|
+
url: "txapi/mvtp/meinv",
|
41
|
+
param_key: "num"
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
def register_helper(name, config)
|
46
|
+
send :define_singleton_method, name do
|
47
|
+
config
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module BaiduApis
|
2
|
+
class Operation
|
3
|
+
API_URL = "http://apis.baidu.com/"
|
4
|
+
|
5
|
+
class << self
|
6
|
+
def req(method_name, url, params)
|
7
|
+
url_path = url.is_a?(Array) ? url.join('/') : url
|
8
|
+
|
9
|
+
Faraday.send(method_name, "#{API_URL}#{url_path}", params) do |req|
|
10
|
+
req.headers['apikey'] = BaiduApis.api_key
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def method_missing(method_id, *args, &block)
|
15
|
+
method_name = method_id.id2name
|
16
|
+
raise "invalid operation method" unless valid_method?(method_name)
|
17
|
+
raise "must set api_key first" unless BaiduApis.api_key
|
18
|
+
|
19
|
+
option = {}
|
20
|
+
block.call(option) if block_given?
|
21
|
+
|
22
|
+
response = req(method_name, args, option)
|
23
|
+
JSON.parse(response.body)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
def valid_method?(method_name)
|
28
|
+
%w(get post).include?(method_name)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: baidu_apis
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- adamshen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Simple wrapper for the Baidu APIs
|
42
|
+
email:
|
43
|
+
- adam_ruby@126.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- LICENSE.txt
|
49
|
+
- README.md
|
50
|
+
- lib/baidu_apis.rb
|
51
|
+
- lib/baidu_apis/helper.rb
|
52
|
+
- lib/baidu_apis/operation.rb
|
53
|
+
- lib/baidu_apis/version.rb
|
54
|
+
homepage: https://github.com/adamshen/baidu_apis
|
55
|
+
licenses:
|
56
|
+
- MIT
|
57
|
+
metadata: {}
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 2.5.1
|
75
|
+
signing_key:
|
76
|
+
specification_version: 4
|
77
|
+
summary: Simple wrapper for the Baidu APIs
|
78
|
+
test_files: []
|