baidumap 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 77d8784f543f554fea7653f79397de75950f3b65
4
+ data.tar.gz: 0b4c8dea9ba1b26801703d826be2948f40531c62
5
+ SHA512:
6
+ metadata.gz: 7e594ff223cd7dfb12f397649b6aef89250b3c8a26fb41813075c4230ab50470f0f2415c90a71eb66d1a77987b1775fbc8ff013bf97d0483827fea1e02d29efb
7
+ data.tar.gz: 62001c7b11685951902ffbc0d256484ba4b4d45c52aa580c314d9898c8fc98dceddcc6afebf8e33b7507e2480e8b784a109cc093c8b01f9daaa51bcfb65d0fce
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ lib/.DS_Store
20
+
21
+ lib/baidumap/.DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in baidumap.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 刘明
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.
@@ -0,0 +1,45 @@
1
+ # Baidumap
2
+
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ gem 'baidumap'
9
+
10
+ And then execute:
11
+
12
+ $ bundle
13
+
14
+ Or install it yourself as:
15
+
16
+ $ gem install baidumap
17
+
18
+ ## Usage
19
+
20
+ ```ruby
21
+ ak = 'aaaaaaaaaa' #assigned by baidu
22
+ place = Baidumap::Request::Place.new(ak)
23
+ response = place.subject.search_by_region('饭店','北京')
24
+ response = place.search_by_bounds('银行',39.915,116.404,39.975,116.414)
25
+ response = place.search_by_radius('银行',39.915,116.404,2000)
26
+ p response.status
27
+ p response.result
28
+
29
+
30
+ geocoder = Baidumap::Request::Geocoder.new(ak)
31
+ response = geocoder.encode('酒仙桥中路10号','北京')
32
+ response = geocoder.decode('39.983424','116.322987')
33
+ p response.status
34
+ p response.result
35
+
36
+ ```
37
+
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'baidumap/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "baidumap"
8
+ spec.version = Baidumap::VERSION
9
+ spec.authors = ["刘明"]
10
+ spec.email = ["charles.liu@corp.elong.com"]
11
+ spec.description = %q{Baidu map services: place, geocoding, direction}
12
+ spec.summary = %q{Baidu map gem}
13
+ spec.homepage = "http://github.com/seoaqua/baidumap"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "httparty"
24
+
25
+ end
@@ -0,0 +1,7 @@
1
+ require "baidumap/version"
2
+ require "baidumap/request"
3
+ require "baidumap/response"
4
+
5
+ module Baidumap
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,54 @@
1
+ require 'baidumap/request/place'
2
+ require 'baidumap/request/geocoder'
3
+ require 'httparty'
4
+ module Baidumap
5
+ module Request
6
+ HOST = 'api.map.baidu.com'
7
+ def initialize(ak,pagesize=10,page_num=0,scope=2)
8
+ service_name = self.class.name.split('::').last.downcase
9
+ @service_path = "/#{service_name}/v2"
10
+ @ak = ak
11
+ @segments = {:ak=>@ak,:output=>'json',:pagesize=>pagesize,:page_num => page_num,:scope=>scope}
12
+ end
13
+
14
+ #actions: search, detail, eventsearch, eventdetail
15
+ def act(action,params)
16
+ @action_path = File.join(@service_path,action.to_s)
17
+ @params = params
18
+ request
19
+ end
20
+
21
+ #next page
22
+ def next
23
+ @segments[:page_num] += 1
24
+ request
25
+ end
26
+
27
+ #previous page
28
+ def prev
29
+ @segments[:page_num] -= 1
30
+ request
31
+ end
32
+
33
+ private
34
+ #make http query from a hash
35
+ def make_query(segments)
36
+ URI.encode(segments.map{|k,v|"#{k}=#{v}"}.join('&'))
37
+ end
38
+
39
+ #send http request
40
+ def request
41
+ http_segments = @segments.clone
42
+ @params.each do |key,value|
43
+ http_segments[key] = value
44
+ end
45
+ uri = URI::HTTP.build(
46
+ :host => HOST,
47
+ :path => @action_path,
48
+ :query => make_query(http_segments)
49
+ ).to_s
50
+ result = JSON.parse(HTTParty.get(uri).parsed_response)
51
+ Baidumap::Response.new(result,self)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,15 @@
1
+ module Baidumap
2
+ module Request
3
+ class Geocoder
4
+ include Baidumap::Request
5
+ #地理编码, 把地址变成经纬度
6
+ def encode(address,city)
7
+ act('',{'address'=>address,'city'=>city})
8
+ end
9
+ #逆地理编码, 把经纬度变成地址
10
+ def decode(lng,lat)
11
+ act('',{'location'=>"#{lng},#{lat}"})
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,46 @@
1
+ module Baidumap
2
+ module Request
3
+ class Place
4
+ include Baidumap::Request
5
+
6
+ def search_by_region(query,region)
7
+ search({
8
+ 'query'=>query,
9
+ 'region'=>region
10
+ })
11
+ end
12
+
13
+ def search_by_bounds(query,lng1,lat1,lng2,lat2)
14
+ search({
15
+ 'query'=>query,
16
+ 'bounds'=>"#{lng1},#{lat1},#{lng2},#{lat2}"
17
+ })
18
+ end
19
+
20
+ def search_by_radius(query,lng,lat,radius)
21
+ search({
22
+ 'query'=>query,
23
+ 'location'=>"#{lng},#{lat}",
24
+ 'radius'=>radius
25
+ })
26
+ end
27
+
28
+ def detail(uid)
29
+ act(__method__,{'uid'=>uid})
30
+ end
31
+
32
+ def eventsearch
33
+ "todo"
34
+ end
35
+
36
+ def eventdetail
37
+ "todo"
38
+ end
39
+
40
+ private
41
+ def search(params)
42
+ act(__method__,params)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,22 @@
1
+ module Baidumap
2
+ class Response
3
+ attr_reader :status,:message,:total,:result
4
+ def initialize(result,request)
5
+ @status,@message,@total = result['status'],result['message'],result['total']
6
+ @results = result['results']
7
+ if result.has_key?'result'
8
+ @result = result['result']
9
+ elsif result.has_key?'results'
10
+ @result = result['results']
11
+ end
12
+ end
13
+
14
+ def next
15
+ @request.next
16
+ end
17
+
18
+ def prev
19
+ @request.prev
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module Baidumap
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Baidumap::Request::Geocoder do
4
+ subject{Baidumap::Request::Geocoder.new($ak)}
5
+ it "should be able to encode an address" do
6
+ #{"location"=>{"lng"=>116.50138754387, "lat"=>39.982778249893}, "precise"=>1, "confidence"=>80, "level"=>"道路"}
7
+ response = subject.encode('酒仙桥中路10号','北京')
8
+ response.class.should == Baidumap::Response
9
+ response.status.should == 0
10
+ response.result.should have_key 'location'
11
+ end
12
+
13
+ it "should be able to decode a location" do
14
+ #{"location"=>{"lng"=>116.32298703399, "lat"=>39.983424051248}, "formatted_address"=>"北京市海淀区中关村大街27号1101-08室", "business"=>"中关村,人民大学,苏州街", "addressComponent"=>{"city"=>"北京市", "district"=>"海淀区", "province"=>"北京市", "street"=>"中关村大街", "street_number"=>"27号1101-08室"}, "cityCode"=>131}
15
+ response = subject.decode('39.983424','116.322987')
16
+ response.class.should == Baidumap::Response
17
+ response.status.should == 0
18
+ response.result.should have_key 'formatted_address'
19
+ end
20
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Baidumap::Request::Place do
4
+ subject{Baidumap::Request::Place.new($ak)}
5
+ it "should be able to search by region" do
6
+ response = subject.search_by_region('饭店','北京')
7
+ response.class.should == Baidumap::Response
8
+ response.status.should == 0
9
+ result = response.result
10
+ result.class.should == Array
11
+ result.first.class.should == Hash
12
+ result.first.should have_key 'name'
13
+ end
14
+
15
+ it "should be able to search by bounds" do
16
+ response = subject.search_by_bounds('银行',39.915,116.404,39.975,116.414)
17
+ response.class.should == Baidumap::Response
18
+ response.status.should == 0
19
+ result = response.result
20
+ result.class.should == Array
21
+ result.first.class.should == Hash
22
+ result.first.should have_key 'name'
23
+ end
24
+
25
+ it "should be able to search by radius" do
26
+ response = subject.search_by_radius('银行',39.915,116.404,2000)
27
+ response.class.should == Baidumap::Response
28
+ response.status.should == 0
29
+ result = response.result
30
+ result.class.should == Array
31
+ result.first.class.should == Hash
32
+ result.first.should have_key 'name'
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
18
+ require 'baidumap'
19
+ $ak = nil
20
+ abort "need to config $ak in spec/spec_helper.rb first" if $ak.nil?
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: baidumap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - "刘明"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-20 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: 'Baidu map services: place, geocoding, direction'
56
+ email:
57
+ - charles.liu@corp.elong.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - baidumap.gemspec
69
+ - lib/baidumap.rb
70
+ - lib/baidumap/request.rb
71
+ - lib/baidumap/request/geocoder.rb
72
+ - lib/baidumap/request/place.rb
73
+ - lib/baidumap/response.rb
74
+ - lib/baidumap/version.rb
75
+ - spec/geocoder_spec.rb
76
+ - spec/place_spec.rb
77
+ - spec/spec_helper.rb
78
+ homepage: http://github.com/seoaqua/baidumap
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.2.2
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Baidu map gem
102
+ test_files:
103
+ - spec/geocoder_spec.rb
104
+ - spec/place_spec.rb
105
+ - spec/spec_helper.rb