mapbox-sdk 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5ada80d6e1cbf4bce52c8963bdcd9eb0d6c7ad93
4
- data.tar.gz: 4c58511165153eca7a4bb787ae979f75ebf277d7
3
+ metadata.gz: c0484d85c89aade2922c0a0d43b3d3d61c74db5f
4
+ data.tar.gz: 0a9304ae7755bbb5121bc237f61b915cdd35596c
5
5
  SHA512:
6
- metadata.gz: 0f8ddaff0bb1a510dba43c5b7b4a5ff9b13d230b390a9d734df095279cffbefa61d86ef04c220e53973241419dc9daafdd4fd7d47bf1bfa23b016e22ea7783d1
7
- data.tar.gz: 6c92af8cd540921a908634beba3cbb784e4bb2205d71889a758ee31992359f3c21e099b56b0434f814d84b20bc4ae5bc56994cf557c5dcc082a194652f94500a
6
+ metadata.gz: 2664c0e47fc458b1933b3476d7902edbb965613b5326f308b77e07be56f16e5ba2c3193b0abd4ec365e6538e7c7e2db3e7860320a8110f7c23bcfe5c9dad93a4
7
+ data.tar.gz: 68f8963b09a9f1b00c76778a79751467ff1d1c636c5454ef6aa934a3124f0daed07e54c6b540f8a151edbcbe0600324936f7a74088e3a9fd3a2d74590aa29995
data/README.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  The `mapbox-sdk` gem. A ruby interface to [Mapbox APIs](https://www.mapbox.com/developers/api/).
4
4
 
5
+ ## Installation
6
+
7
+ ```
8
+ gem install mapbox-sdk
9
+ ```
10
+
5
11
  ## Services
6
12
 
7
13
  * [Geocoding](https://www.mapbox.com/developers/api/geocoding/)
@@ -22,8 +28,8 @@ placenames = Mapbox::Geocoder.geocode_reverse({
22
28
  "longitude" => -100
23
29
  })
24
30
 
25
- # Forward geocoding
26
- places = Mapbox::Geocoder.geocode_forward("Chester, NJ")
31
+ # Forward geocoding with optional proximity parameter
32
+ places = Mapbox::Geocoder.geocode_forward("Chester, NJ", {:proximity => {:longitude => -74.6968, :latitude => 40.7843}})
27
33
 
28
34
  # Directions
29
35
  drivingDirections = Mapbox::Directions.directions([{
@@ -0,0 +1 @@
1
+ require 'mapbox'
@@ -3,15 +3,15 @@ require 'json'
3
3
  require 'mapbox/api_operations'
4
4
  require 'mapbox/authentication_error'
5
5
 
6
- # services
7
- require 'mapbox/geocoder'
8
- require 'mapbox/directions'
9
-
10
6
  module Mapbox
11
7
  @api_base = 'https://api.mapbox.com'
8
+ @request_opts = {}
9
+
10
+ LATITUDE_KEY = 'latitude'.freeze
11
+ LONGITUDE_KEY = 'longitude'.freeze
12
12
 
13
13
  class << self
14
- attr_accessor :access_token, :api_base
14
+ attr_accessor :access_token, :api_base, :request_opts
15
15
  end
16
16
 
17
17
  def self.request(method, url, api_key, params={}, headers={}, api_base_url=nil)
@@ -37,10 +37,10 @@ module Mapbox
37
37
  payload = nil
38
38
  end
39
39
 
40
- request_opts = {:verify_ssl => OpenSSL::SSL::VERIFY_PEER,
40
+ @request_opts = {:verify_ssl => OpenSSL::SSL::VERIFY_PEER,
41
41
  :ssl_ca_file => @ssl_bundle_path}
42
42
 
43
- request_opts.update(
43
+ @request_opts.update(
44
44
  :method => method,
45
45
  :open_timeout => 30,
46
46
  :payload => payload,
@@ -48,7 +48,7 @@ module Mapbox
48
48
  :timeout => 80)
49
49
 
50
50
  begin
51
- response = execute_request(request_opts)
51
+ response = execute_request(@request_opts)
52
52
  rescue SocketError => e
53
53
  handle_restclient_error(e, api_base_url)
54
54
  rescue NoMethodError => e
@@ -168,4 +168,15 @@ module Mapbox
168
168
 
169
169
  raise StandardError.new(message + "\n\n(Network error: #{e.message})")
170
170
  end
171
+
172
+ module HashUtils
173
+ def xy_from_hash h = {}
174
+ [ h.fetch(:longitude){ h[LONGITUDE_KEY] },
175
+ h.fetch(:latitude){ h[LATITUDE_KEY] } ]
176
+ end
177
+ end
171
178
  end
179
+
180
+ # services
181
+ require 'mapbox/geocoder'
182
+ require 'mapbox/directions'
@@ -4,10 +4,9 @@ require 'json'
4
4
  module Mapbox
5
5
  class Directions
6
6
  include Mapbox::APIOperations::Request
7
+ extend Mapbox::HashUtils
7
8
  def self.directions(waypoints, profile='mapbox.driving')
8
- formatted_waypoints = waypoints.map { |point|
9
- "#{point['longitude']},#{point['latitude']}"
10
- }.join(';')
9
+ formatted_waypoints = waypoints.map {|p| xy_from_hash(p).join ','}.join ';'
11
10
  return request(
12
11
  :get,
13
12
  "/v4/directions/#{profile}/#{formatted_waypoints}.json",
@@ -1,20 +1,43 @@
1
1
  require 'rest-client'
2
2
  require 'json'
3
+ require 'uri'
3
4
 
4
5
  module Mapbox
5
6
  class Geocoder
6
7
  include Mapbox::APIOperations::Request
7
- def self.geocode_forward(query, proximity=nil, dataset='mapbox.places')
8
+ extend Mapbox::HashUtils
9
+
10
+ def self.geocode_forward(query, options={}, dataset='mapbox.places')
11
+ proximity = options[:proximity]
12
+ bbox = options[:bbox]
13
+ params = ''
14
+ opts = options.select { |key, value| key != :proximity && key != :bbox}
15
+ if opts.length > 0
16
+ params += "#{params.length > 0 ? '&' : '?'}#{URI.encode_www_form(opts)}"
17
+ end
18
+
19
+ if proximity then
20
+ lon = proximity[:longitude].round(3)
21
+ lat = proximity[:latitude].round(3)
22
+ params += "#{params.length > 0 ? '&' : '?'}proximity=#{lon}%2C#{lat}"
23
+ end
24
+
25
+ if bbox then
26
+ params += "#{params.length > 0 ? '&' : '?'}bbox=#{bbox.join('%2C')}"
27
+ end
8
28
  return request(
9
29
  :get,
10
- "/v4/geocode/#{dataset}/#{URI.escape(query)}.json",
30
+ "/geocoding/v5/#{dataset}/#{URI.escape(query)}.json#{params}",
11
31
  nil)
12
32
  end
13
33
 
14
34
  def self.geocode_reverse(location, dataset='mapbox.places')
35
+ location[:longitude] = location[:longitude].round(5)
36
+ location[:latitude] = location[:latitude].round(5)
37
+
15
38
  return request(
16
39
  :get,
17
- "/v4/geocode/#{dataset}/#{location["longitude"]},#{location["latitude"]}.json",
40
+ "/geocoding/v5/#{dataset}/#{xy_from_hash(location).join(',')}.json",
18
41
  nil)
19
42
  end
20
43
  end
@@ -2,7 +2,7 @@ $:.unshift(File.join(File.dirname(__FILE__), 'lib'))
2
2
 
3
3
  spec = Gem::Specification.new do |s|
4
4
  s.name = 'mapbox-sdk'
5
- s.version = '1.0.0'
5
+ s.version = '2.0.0'
6
6
  s.summary = 'Ruby bindings for the Mapbox API'
7
7
  s.description = ''
8
8
  s.authors = ['Tom MacWright']
@@ -9,12 +9,12 @@ module Mapbox
9
9
  Mapbox.access_token = ENV["MapboxAccessToken"]
10
10
  result = Mapbox::Directions.directions([
11
11
  {
12
- "longitude" => -100,
13
- "latitude" => 38
12
+ :longitude => -100,
13
+ :latitude => 38
14
14
  },
15
15
  {
16
- "longitude" => -90,
17
- "latitude" => 38
16
+ :longitude => -90,
17
+ :latitude => 38
18
18
  }
19
19
  ])
20
20
  assert result
@@ -11,13 +11,53 @@ module Mapbox
11
11
  assert result
12
12
  end
13
13
 
14
- should "#geocode_reverse" do
14
+ should "#geocode_forward (rounded proximity to 3 decimal places)" do
15
+ Mapbox.access_token = ENV["MapboxAccessToken"]
16
+ result = Mapbox::Geocoder.geocode_forward("Chester, NJ", {:proximity => {:longitude => 0.1234567, :latitude => -10.987654}})
17
+ assert result
18
+ assert Mapbox.request_opts[:url].include? '?proximity=0.123%2C-10.988&'
19
+ end
20
+
21
+ should "#geocode_forward (include bbox param)" do
22
+ Mapbox.access_token = ENV["MapboxAccessToken"]
23
+ result = Mapbox::Geocoder.geocode_forward("Washington", {:bbox => [-78.3284,38.6039,-78.0428,38.7841]})
24
+ assert result
25
+ assert Mapbox.request_opts[:url].include? '?bbox=-78.3284%2C38.6039%2C-78.0428%2C38.7841';
26
+ end
27
+
28
+ should "#geocode_forward (include extra param)" do
29
+ Mapbox.access_token = ENV["MapboxAccessToken"]
30
+ result = Mapbox::Geocoder.geocode_forward("Washington", {:bbox => [-78.3284,38.6039,-78.0428,38.7841], :foo_key => "foo_val", :bar_key => "bar_val"})
31
+ assert result
32
+ assert Mapbox.request_opts[:url].include? '?foo_key=foo_val&bar_key=bar_val';
33
+ end
34
+
35
+ should "#geocode_forward (include country param)" do
36
+ Mapbox.access_token = ENV["MapboxAccessToken"]
37
+ result = Mapbox::Geocoder.geocode_forward("Washington", {:country => "ca"})
38
+ assert result
39
+ assert Mapbox.request_opts[:url].include? '?country=ca';
40
+ end
41
+
42
+ should "#geocode_reverse" do
15
43
  Mapbox.access_token = ENV["MapboxAccessToken"]
16
44
  result = Mapbox::Geocoder.geocode_reverse({
17
- "latitude" => 38,
18
- "longitude" => -100
45
+ :latitude => 38,
46
+ :longitude => -100
19
47
  })
20
48
  assert result
21
49
  end
50
+
51
+ should "#geocode_reverse (rounded coordinates to 5 decimal places)" do
52
+ Mapbox.access_token = ENV["MapboxAccessToken"]
53
+ result = Mapbox::Geocoder.geocode_reverse({
54
+ :latitude => 1.23456789,
55
+ :longitude => -99.8765432
56
+ })
57
+ for coord in result.first['query'] do
58
+ assert coord.to_s.split('.')[-1].length <= 5
59
+ end
60
+ end
61
+
22
62
  end
23
63
  end
@@ -15,4 +15,27 @@ module Mapbox
15
15
  Mapbox.access_token = "foo"
16
16
  end
17
17
  end
18
+
19
+ class HashUtilTest < Test::Unit::TestCase
20
+ include Mapbox::HashUtils
21
+
22
+ should "return array of long,lat from hash with string keys" do
23
+ assert_equal xy_from_hash({'longitude' => -122, 'latitude' => 45}),
24
+ [-122, 45]
25
+ end
26
+
27
+ should "return array of long,lat from hash with symbol keys" do
28
+ assert_equal xy_from_hash({:longitude => -122, :latitude => 45}),
29
+ [-122, 45]
30
+ end
31
+
32
+ should "return array of long,lat from hash with mixed keys" do
33
+ assert_equal xy_from_hash({:longitude => -122, 'latitude' => 45}),
34
+ [-122, 45]
35
+ end
36
+
37
+ should "raise arguemnt error if not given a hash" do
38
+ assert_raise { xy_from_hash(Object.new) }
39
+ end
40
+ end
18
41
  end
metadata CHANGED
@@ -1,97 +1,97 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mapbox-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom MacWright
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-23 00:00:00.000000000 Z
11
+ date: 2016-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.4'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.4'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: json
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: 1.8.1
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 1.8.1
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: mocha
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: 0.13.2
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 0.13.2
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: shoulda
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: 3.4.0
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 3.4.0
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: test-unit
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rake
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  description: ''
@@ -106,6 +106,7 @@ files:
106
106
  - LICENSE
107
107
  - README.md
108
108
  - Rakefile
109
+ - lib/mapbox-sdk.rb
109
110
  - lib/mapbox.rb
110
111
  - lib/mapbox/api_operations.rb
111
112
  - lib/mapbox/authentication_error.rb
@@ -125,17 +126,17 @@ require_paths:
125
126
  - lib
126
127
  required_ruby_version: !ruby/object:Gem::Requirement
127
128
  requirements:
128
- - - '>='
129
+ - - ">="
129
130
  - !ruby/object:Gem::Version
130
131
  version: '0'
131
132
  required_rubygems_version: !ruby/object:Gem::Requirement
132
133
  requirements:
133
- - - '>='
134
+ - - ">="
134
135
  - !ruby/object:Gem::Version
135
136
  version: '0'
136
137
  requirements: []
137
138
  rubyforge_project:
138
- rubygems_version: 2.0.14
139
+ rubygems_version: 2.5.1
139
140
  signing_key:
140
141
  specification_version: 4
141
142
  summary: Ruby bindings for the Mapbox API