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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3d0119c07ff0759739f3244c10b03ff106fbccae
4
- data.tar.gz: 352c9d16c35137c619e88dcc50d54a4d5afbf8ac
3
+ metadata.gz: cb1da98293ff938d23b05e5f955b9afd97b51f2e
4
+ data.tar.gz: c978f7368484d1fe9e709885b07de15cbe14ca2e
5
5
  SHA512:
6
- metadata.gz: 2e78cfde4bb1df680c79aa94ae8d91f5dc98eb89989946d87a5368626d6dbe99b4afb2a009ab2070da70c91d73b92161b5c4dadf450b0f8de6c74e98f7ae6c53
7
- data.tar.gz: dda253ade62ff24990d5354699ab06efd9d2fc84b4df9a1b4f8aade16c19812b426166513a11fc592751509c5f474e898c8861b60529fe4fa34088b012c6ffb1
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
- then
40
+ To geocode an address:
41
41
 
42
42
  ```ruby
43
- BaiduApi::Geocoding.geocode('百度大厦')
44
- => {"status"=>0, "result"=>{"location"=>{"lng"=>116.30814954222, "lat"=>40.056885091681}, "precise"=>1, "confidence"=>80, "level"=>"商务大厦"}
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 )
@@ -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 [Baidu API Geocoder](http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-geocoding) client.
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(address, options)
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, options[:sk])}" if options[:sk]
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
@@ -1,5 +1,5 @@
1
1
  module BaiduApi
2
2
  module Geocoding
3
- VERSION = "0.0.1"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
@@ -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(address, options = {})
24
- options = fetch_options(options)
25
- version = options.delete(:version).upcase
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(address, options)
27
+ geocoder.geocode(data)
28
28
  end
29
29
 
30
- def fetch_options(options)
31
- %w(ak sk version).inject({}) do |ret, item|
32
- ret[item.intern] = fetch_option(options, item)
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 fetch_option(options, key)
38
- options.fetch(key.intern) { options.fetch(key.to_s, config[key]) }
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.1
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 [Baidu API Geocoder](http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-geocoding)
82
- client.
81
+ summary: A ruby version of Baidu API Geocoder client.
83
82
  test_files: []