baidu_api-geocoding 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +53 -0
- data/Rakefile +2 -0
- data/baidu_api-geocoding.gemspec +23 -0
- data/lib/baidu_api/geocoding/configuration.rb +19 -0
- data/lib/baidu_api/geocoding/v2/geocoder.rb +53 -0
- data/lib/baidu_api/geocoding/version.rb +5 -0
- data/lib/baidu_api/geocoding.rb +42 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3d0119c07ff0759739f3244c10b03ff106fbccae
|
4
|
+
data.tar.gz: 352c9d16c35137c619e88dcc50d54a4d5afbf8ac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2e78cfde4bb1df680c79aa94ae8d91f5dc98eb89989946d87a5368626d6dbe99b4afb2a009ab2070da70c91d73b92161b5c4dadf450b0f8de6c74e98f7ae6c53
|
7
|
+
data.tar.gz: dda253ade62ff24990d5354699ab06efd9d2fc84b4df9a1b4f8aade16c19812b426166513a11fc592751509c5f474e898c8861b60529fe4fa34088b012c6ffb1
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 xiaohui
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# BaiduApi::Geocoding
|
2
|
+
|
3
|
+
A ruby version [Baidu API Geocoder](http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-geocoding) client.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'baidu_api-geocoding'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install baidu_api-geocoding
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'baidu_api/geocoding'
|
25
|
+
```
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
BaiduApi::Geocoding.setup ak: 'xxxx', sk: 'xxxx'
|
29
|
+
```
|
30
|
+
|
31
|
+
or
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
BaiduApi::Geocoding.setup do |config|
|
35
|
+
config.ak = 'xxxx'
|
36
|
+
config.sk = 'xxxx'
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
then
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
BaiduApi::Geocoding.geocode('百度大厦')
|
44
|
+
=> {"status"=>0, "result"=>{"location"=>{"lng"=>116.30814954222, "lat"=>40.056885091681}, "precise"=>1, "confidence"=>80, "level"=>"商务大厦"}
|
45
|
+
```
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
1. Fork it ( https://github.com/[my-github-username]/baidu_api-geocoding/fork )
|
50
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
51
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
52
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
53
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'baidu_api/geocoding/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "baidu_api-geocoding"
|
8
|
+
spec.version = BaiduApi::Geocoding::VERSION
|
9
|
+
spec.authors = ["xiaohui"]
|
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
|
+
}
|
13
|
+
spec.homepage = "http://github.com/xiaohui-zhangxh/baidu_api-geocoding"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module BaiduApi
|
2
|
+
module Geocoding
|
3
|
+
class Configuration
|
4
|
+
attr_accessor :ak, :sk, :version, :debug
|
5
|
+
|
6
|
+
def initialize(options)
|
7
|
+
@ak = options.fetch(:ak){ options.fetch('ak') { fail ArgumentError, 'Missing argument :ak for configuration!' } }
|
8
|
+
@sk = options.fetch(:sk){ options.fetch('sk', nil) }
|
9
|
+
@version = options.fetch(:version){ options.fetch('version', 'v2') }
|
10
|
+
@debug = options.fetch(:debug){ options.fetch('debug', false) }
|
11
|
+
end
|
12
|
+
|
13
|
+
def [](key)
|
14
|
+
respond_to?(key) ? send(key) : nil
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
require 'digest/md5'
|
3
|
+
require 'erb'
|
4
|
+
require 'open-uri'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
module BaiduApi
|
8
|
+
module Geocoding
|
9
|
+
module V2
|
10
|
+
module Geocoder
|
11
|
+
extend self
|
12
|
+
|
13
|
+
API_HOST = 'http://api.map.baidu.com'
|
14
|
+
GEOCODER_PATH = '/geocoder/v2/'
|
15
|
+
|
16
|
+
def geocode(address, options)
|
17
|
+
queries = [
|
18
|
+
[:address, address],
|
19
|
+
[:output, 'json'],
|
20
|
+
[:ak, options[:ak]]
|
21
|
+
]
|
22
|
+
url = "#{API_HOST}#{GEOCODER_PATH}?#{to_query(queries)}"
|
23
|
+
url << "&sn=#{calculate_ak_sn(queries, options[:sk])}" if options[:sk]
|
24
|
+
debug('url', url)
|
25
|
+
open(url) do |http|
|
26
|
+
ret = JSON.parse(http.read)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def calculate_ak_sn(queries, sk)
|
31
|
+
query_str = ERB::Util.url_encode("#{GEOCODER_PATH}?#{to_query(queries)}#{sk}")
|
32
|
+
debug('unencrypted query', query_str)
|
33
|
+
sn = Digest::MD5.hexdigest(query_str)
|
34
|
+
debug('sn', sn)
|
35
|
+
sn
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_query(arr)
|
39
|
+
query_str = arr.map do |k, v|
|
40
|
+
"#{CGI.escape(k.to_s)}=#{CGI.escape(v)}"
|
41
|
+
end.join('&')
|
42
|
+
debug('query_str', query_str)
|
43
|
+
query_str
|
44
|
+
end
|
45
|
+
|
46
|
+
def debug(subject, msg)
|
47
|
+
puts "#{subject}: #{msg}" if BaiduApi::Geocoding.config.debug
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
|
2
|
+
require 'baidu_api/geocoding/version'
|
3
|
+
require 'baidu_api/geocoding/configuration'
|
4
|
+
require 'baidu_api/geocoding/v2/geocoder'
|
5
|
+
|
6
|
+
module BaiduApi
|
7
|
+
module Geocoding
|
8
|
+
extend self
|
9
|
+
# options:
|
10
|
+
# :ak
|
11
|
+
# :sk
|
12
|
+
# :output default: json
|
13
|
+
# :version default: v2
|
14
|
+
def setup(options = {})
|
15
|
+
@@config = BaiduApi::Geocoding::Configuration.new(options)
|
16
|
+
yield @@config if block_given?
|
17
|
+
end
|
18
|
+
|
19
|
+
def config
|
20
|
+
@@config || fail(ArgumentError, 'Please setup API with BaiduApi::Geocoding.setup(options) before starting!')
|
21
|
+
end
|
22
|
+
|
23
|
+
def geocode(address, options = {})
|
24
|
+
options = fetch_options(options)
|
25
|
+
version = options.delete(:version).upcase
|
26
|
+
geocoder = Object.const_get("BaiduApi::Geocoding::#{version}::Geocoder") rescue fail(ArgumentError, "API version #{version} doesn't exist!")
|
27
|
+
geocoder.geocode(address, options)
|
28
|
+
end
|
29
|
+
|
30
|
+
def fetch_options(options)
|
31
|
+
%w(ak sk version).inject({}) do |ret, item|
|
32
|
+
ret[item.intern] = fetch_option(options, item)
|
33
|
+
ret
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def fetch_option(options, key)
|
38
|
+
options.fetch(key.intern) { options.fetch(key.to_s, config[key]) }
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: baidu_api-geocoding
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- xiaohui
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- xiaohui@zhangxh.net
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- baidu_api-geocoding.gemspec
|
54
|
+
- lib/baidu_api/geocoding.rb
|
55
|
+
- lib/baidu_api/geocoding/configuration.rb
|
56
|
+
- lib/baidu_api/geocoding/v2/geocoder.rb
|
57
|
+
- lib/baidu_api/geocoding/version.rb
|
58
|
+
homepage: http://github.com/xiaohui-zhangxh/baidu_api-geocoding
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.4.4
|
79
|
+
signing_key:
|
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.
|
83
|
+
test_files: []
|