baidu_api-geocoding 0.0.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +18 -4
- data/baidu_api-geocoding.gemspec +1 -2
- data/lib/baidu_api/geocoding/v2/geocoder.rb +11 -9
- data/lib/baidu_api/geocoding/version.rb +1 -1
- data/lib/baidu_api/geocoding.rb +9 -9
- metadata +2 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb1da98293ff938d23b05e5f955b9afd97b51f2e
|
4
|
+
data.tar.gz: c978f7368484d1fe9e709885b07de15cbe14ca2e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68a23b438ab3d47ddca8fc0089b222835603eebf31f0efc353c59e7819b9f45317a038d6705757ab37f7cbc3233ab7a8ae0ba575b36d2a7ee720d63dd9ca11cc
|
7
|
+
data.tar.gz: 920eec7e77c27079ddc348ec16fa6686e6c77be9d652b97ee5a09cfabdb239218ccb2b12e06ec846d2872b67cc5a57da71dddf95618b126edc1b6057378eb991
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# BaiduApi::Geocoding
|
2
2
|
|
3
|
-
A ruby version [Baidu API Geocoder](http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-geocoding) client.
|
3
|
+
A ruby version of [Baidu API Geocoder](http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-geocoding) client.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -37,13 +37,27 @@ BaiduApi::Geocoding.setup do |config|
|
|
37
37
|
end
|
38
38
|
```
|
39
39
|
|
40
|
-
|
40
|
+
To geocode an address:
|
41
41
|
|
42
42
|
```ruby
|
43
|
-
|
44
|
-
|
43
|
+
|
44
|
+
BaiduApi::Geocoding.geocode(address: '百度大厦')
|
45
|
+
=> {"status"=>0, "result"=>{"location"=>{"lng"=>116.30814954222, "lat"=>40.056885091681}, "precise"=>1, "confidence"=>80, "level"=>"商务大厦"}}
|
46
|
+
|
47
|
+
BaiduApi::Geocoding.geocode(location: '40.056885091681,116.30814954222')
|
48
|
+
=> {"status"=>0,
|
49
|
+
"result"=>
|
50
|
+
{"location"=>{"lng"=>116.30814954222, "lat"=>40.056885160713},
|
51
|
+
"formatted_address"=>"北京市海淀区上地十街10",
|
52
|
+
"business"=>"西二旗,龙泽,回龙观",
|
53
|
+
"addressComponent"=>{"city"=>"北京市", "direction"=>"附近", "distance"=>"1", "district"=>"海淀区", "province"=>"北京市", "street"=>"上地十街", "street_number"=>"10"},
|
54
|
+
"poiRegions"=>[{"direction_desc"=>"内", "name"=>"百度大厦"}],
|
55
|
+
"cityCode"=>131}}
|
45
56
|
```
|
46
57
|
|
58
|
+
More options for parameter of BaiduApi::Geocoding.geocode, please see http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-geocoding
|
59
|
+
|
60
|
+
|
47
61
|
## Contributing
|
48
62
|
|
49
63
|
1. Fork it ( https://github.com/[my-github-username]/baidu_api-geocoding/fork )
|
data/baidu_api-geocoding.gemspec
CHANGED
@@ -8,8 +8,7 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = BaiduApi::Geocoding::VERSION
|
9
9
|
spec.authors = ["xiaohui"]
|
10
10
|
spec.email = ["xiaohui@zhangxh.net"]
|
11
|
-
spec.summary = %q{A ruby version
|
12
|
-
}
|
11
|
+
spec.summary = %q{A ruby version of Baidu API Geocoder client.}
|
13
12
|
spec.homepage = "http://github.com/xiaohui-zhangxh/baidu_api-geocoding"
|
14
13
|
spec.license = "MIT"
|
15
14
|
|
@@ -13,14 +13,10 @@ module BaiduApi
|
|
13
13
|
API_HOST = 'http://api.map.baidu.com'
|
14
14
|
GEOCODER_PATH = '/geocoder/v2/'
|
15
15
|
|
16
|
-
def geocode(
|
17
|
-
queries = [
|
18
|
-
[:address, address],
|
19
|
-
[:output, 'json'],
|
20
|
-
[:ak, options[:ak]]
|
21
|
-
]
|
16
|
+
def geocode(data)
|
17
|
+
queries = fetch_query_params(data) + [[:output, 'json']]
|
22
18
|
url = "#{API_HOST}#{GEOCODER_PATH}?#{to_query(queries)}"
|
23
|
-
url << "&sn=#{calculate_ak_sn(queries,
|
19
|
+
url << "&sn=#{calculate_ak_sn(queries, data[:sk])}" if data[:sk]
|
24
20
|
debug('url', url)
|
25
21
|
open(url) do |http|
|
26
22
|
ret = JSON.parse(http.read)
|
@@ -37,12 +33,18 @@ module BaiduApi
|
|
37
33
|
|
38
34
|
def to_query(arr)
|
39
35
|
query_str = arr.map do |k, v|
|
40
|
-
"#{CGI.escape(k.to_s)}=#{CGI.escape(v)}"
|
41
|
-
end.join('&')
|
36
|
+
v && "#{CGI.escape(k.to_s)}=#{CGI.escape(v)}"
|
37
|
+
end.compact.join('&')
|
42
38
|
debug('query_str', query_str)
|
43
39
|
query_str
|
44
40
|
end
|
45
41
|
|
42
|
+
def fetch_query_params(data)
|
43
|
+
%w(output ak address city coordtype location pois).map do |k|
|
44
|
+
[k.intern, data[k.intern]]
|
45
|
+
end.compact
|
46
|
+
end
|
47
|
+
|
46
48
|
def debug(subject, msg)
|
47
49
|
puts "#{subject}: #{msg}" if BaiduApi::Geocoding.config.debug
|
48
50
|
end
|
data/lib/baidu_api/geocoding.rb
CHANGED
@@ -20,22 +20,22 @@ module BaiduApi
|
|
20
20
|
@@config || fail(ArgumentError, 'Please setup API with BaiduApi::Geocoding.setup(options) before starting!')
|
21
21
|
end
|
22
22
|
|
23
|
-
def geocode(
|
24
|
-
|
25
|
-
version =
|
23
|
+
def geocode(data = {})
|
24
|
+
data = fetch_accepted_data(data)
|
25
|
+
version = data.delete(:version).upcase
|
26
26
|
geocoder = Object.const_get("BaiduApi::Geocoding::#{version}::Geocoder") rescue fail(ArgumentError, "API version #{version} doesn't exist!")
|
27
|
-
geocoder.geocode(
|
27
|
+
geocoder.geocode(data)
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
31
|
-
%w(ak sk version).inject({}) do |ret, item|
|
32
|
-
ret[item.intern] =
|
30
|
+
def fetch_accepted_data(data)
|
31
|
+
%w(ak sk version address city coordtype location pois).inject({}) do |ret, item|
|
32
|
+
ret[item.intern] = fetch_data(data, item)
|
33
33
|
ret
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
-
def
|
38
|
-
|
37
|
+
def fetch_data(data, key)
|
38
|
+
data.fetch(key.intern) { data.fetch(key.to_s, config[key]) }
|
39
39
|
end
|
40
40
|
|
41
41
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: baidu_api-geocoding
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- xiaohui
|
@@ -78,6 +78,5 @@ rubyforge_project:
|
|
78
78
|
rubygems_version: 2.4.4
|
79
79
|
signing_key:
|
80
80
|
specification_version: 4
|
81
|
-
summary: A ruby version
|
82
|
-
client.
|
81
|
+
summary: A ruby version of Baidu API Geocoder client.
|
83
82
|
test_files: []
|