mapbox-sdk 1.0.0 → 2.3.1

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.
@@ -1,20 +1,57 @@
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.assemble_params(options={})
11
+ proximity = options[:proximity]
12
+ bbox = options[:bbox]
13
+ types = options[:types]
14
+ params = ''
15
+ opts = options.select { |key, value| key != :proximity && key != :bbox && key != :types}
16
+ if opts.length > 0
17
+ params += "#{params.length > 0 ? '&' : '?'}#{URI.encode_www_form(opts)}"
18
+ end
19
+
20
+ if proximity then
21
+ lon = proximity[:longitude].round(3)
22
+ lat = proximity[:latitude].round(3)
23
+ params += "#{params.length > 0 ? '&' : '?'}proximity=#{lon}%2C#{lat}"
24
+ end
25
+
26
+ if bbox then
27
+ params += "#{params.length > 0 ? '&' : '?'}bbox=#{bbox.join('%2C')}"
28
+ end
29
+
30
+ if types then
31
+ params += "#{params.length > 0 ? '&' : '?'}types=#{types.join('%2C')}"
32
+ end
33
+
34
+ return params
35
+ end
36
+
37
+ def self.geocode_forward(query, options={}, dataset='mapbox.places')
38
+ params = self.assemble_params(options)
39
+
8
40
  return request(
9
41
  :get,
10
- "/v4/geocode/#{dataset}/#{URI.escape(query)}.json",
42
+ "/geocoding/v5/#{dataset}/#{URI.escape(query)}.json#{params}",
11
43
  nil)
12
44
  end
13
45
 
14
- def self.geocode_reverse(location, dataset='mapbox.places')
46
+ def self.geocode_reverse(location, options={}, dataset='mapbox.places')
47
+ location[:longitude] = location[:longitude].round(5)
48
+ location[:latitude] = location[:latitude].round(5)
49
+
50
+ params = self.assemble_params(options)
51
+
15
52
  return request(
16
53
  :get,
17
- "/v4/geocode/#{dataset}/#{location["longitude"]},#{location["latitude"]}.json",
54
+ "/geocoding/v5/#{dataset}/#{xy_from_hash(location).join(',')}.json#{params}",
18
55
  nil)
19
56
  end
20
57
  end
@@ -0,0 +1,35 @@
1
+ module Mapbox
2
+ class Isochrone
3
+ include Mapbox::APIOperations::Request
4
+
5
+ def self.assemble_params(options={})
6
+ contours_minutes = options[:contours_minutes]
7
+ contours_colors = options[:contours_colors]
8
+
9
+ params = ''
10
+ opts = options.select { |key, value| key != :contours_minutes && key != :contours_colors }
11
+ if opts.length > 0
12
+ params += "#{params.length > 0 ? '&' : '?'}#{URI.encode_www_form(opts)}"
13
+ end
14
+
15
+ if contours_minutes then
16
+ params += "#{params.length > 0 ? '&' : '?'}contours_minutes=#{contours_minutes.join(',')}"
17
+ end
18
+
19
+ if contours_colors then
20
+ params += "#{params.length > 0 ? '&' : '?'}contours_colors=#{contours_colors.join(',')}"
21
+ end
22
+
23
+ return params
24
+ end
25
+
26
+ def self.isochrone(profile, coordinates, options={})
27
+ params = self.assemble_params(options)
28
+
29
+ return request(
30
+ :get,
31
+ "/isochrone/v1/mapbox/#{profile}/#{coordinates}#{params}",
32
+ nil)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,57 @@
1
+ module Mapbox
2
+ class MapMatching
3
+ include Mapbox::APIOperations::Request
4
+ extend Mapbox::HashUtils
5
+
6
+ def self.assemble_params(options={})
7
+ annotations = options[:annotations]
8
+ approaches = options[:approaches]
9
+ radiuses = options[:radiuses]
10
+ timestamps = options[:timestamps]
11
+ waypoint_names = options[:waypoint_names]
12
+ waypoints = options[:waypoints]
13
+
14
+ params = ''
15
+ opts = options.select { |key, value| key != :annotations && key != :approaches && key != :radiuses && key != :timestamps && key != :waypoint_names && key != :waypoints }
16
+ if opts.length > 0
17
+ params += "#{params.length > 0 ? '&' : '?'}#{URI.encode_www_form(opts)}"
18
+ end
19
+
20
+ if annotations then
21
+ params += "#{params.length > 0 ? '&' : '?'}annotations=#{annotations.join(',')}"
22
+ end
23
+
24
+ if approaches then
25
+ params += "#{params.length > 0 ? '&' : '?'}approaches=#{approaches.join(';')}"
26
+ end
27
+
28
+ if radiuses then
29
+ params += "#{params.length > 0 ? '&' : '?'}radiuses=#{radiuses.join ';'}"
30
+ end
31
+
32
+ if timestamps then
33
+ params += "#{params.length > 0 ? '&' : '?'}timestamps=#{timestamps.join ';'}"
34
+ end
35
+
36
+ if waypoint_names then
37
+ params += "#{params.length > 0 ? '&' : '?'}waypoint_names=#{waypoint_names.join ';'}"
38
+ end
39
+
40
+ if waypoints then
41
+ params += "#{params.length > 0 ? '&' : '?'}waypoints=#{waypoints.join ';'}"
42
+ end
43
+
44
+ return params
45
+ end
46
+
47
+ def self.map_matching(waypoints, profile, options={})
48
+ formatted_waypoints = waypoints.map {|p| xy_from_hash(p).join ','}.join ';'
49
+ params = self.assemble_params(options)
50
+
51
+ return request(
52
+ :get,
53
+ "/matching/v5/mapbox/#{profile}/#{formatted_waypoints}.json#{params}",
54
+ nil)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,32 @@
1
+ module Mapbox
2
+ class Matrix
3
+ include Mapbox::APIOperations::Request
4
+ extend Mapbox::HashUtils
5
+
6
+ def self.assemble_params(options={})
7
+ opts = options.dup
8
+
9
+ self.joinArrayParam(opts, :destinations)
10
+ self.joinArrayParam(opts, :annotations, ',')
11
+ self.joinArrayParam(opts, :approaches)
12
+ self.joinArrayParam(opts, :sources)
13
+
14
+ return "?#{URI.encode_www_form(opts)}"
15
+ end
16
+
17
+ def self.joinArrayParam(opts, name, joinSymbol = ';')
18
+ if opts[name].kind_of?(Array)
19
+ opts[name] = opts[name].join(joinSymbol)
20
+ end
21
+ end
22
+
23
+ def self.matrix(waypoints, profile, options={})
24
+ formatted_waypoints = waypoints.map {|p| xy_from_hash(p).join ','}.join ';'
25
+ params = self.assemble_params(options)
26
+ return request(
27
+ :get,
28
+ "/directions-matrix/v1/mapbox/#{profile}/#{formatted_waypoints}.json#{params}",
29
+ nil)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,47 @@
1
+ module Mapbox
2
+ class Optimization
3
+ include Mapbox::APIOperations::Request
4
+ extend Mapbox::HashUtils
5
+
6
+ def self.assemble_params(options={})
7
+ annotations = options[:annotations]
8
+ approaches = options[:approaches]
9
+ distributions = options[:distributions]
10
+ radiuses = options[:radiuses]
11
+
12
+ params = ''
13
+ opts = options.select { |key, value| key != :annotations && key != :approaches && key != :distributions && key != :radiuses }
14
+ if opts.length > 0
15
+ params += "#{params.length > 0 ? '&' : '?'}#{URI.encode_www_form(opts)}"
16
+ end
17
+
18
+ if annotations then
19
+ params += "#{params.length > 0 ? '&' : '?'}annotations=#{annotations.join(',')}"
20
+ end
21
+
22
+ if approaches then
23
+ params += "#{params.length > 0 ? '&' : '?'}approaches=#{approaches.join(';')}"
24
+ end
25
+
26
+ if distributions then
27
+ params += "#{params.length > 0 ? '&' : '?'}distributions=#{distributions.join(';')}"
28
+ end
29
+
30
+ if radiuses then
31
+ params += "#{params.length > 0 ? '&' : '?'}radiuses=#{radiuses.join(';')}"
32
+ end
33
+
34
+ return params
35
+ end
36
+
37
+ def self.optimization(waypoints, profile, options={})
38
+ formatted_waypoints = waypoints.map {|p| xy_from_hash(p).join ','}.join ';'
39
+ params = self.assemble_params(options)
40
+
41
+ return request(
42
+ :get,
43
+ "/optimized-trips/v1/mapbox/#{profile}/#{formatted_waypoints}.json#{params}",
44
+ nil)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,15 @@
1
+ module Mapbox
2
+ class Tilequery
3
+ include Mapbox::APIOperations::Request
4
+ extend Mapbox::HashUtils
5
+
6
+ def self.tilequery(mapid, location, radius, limit)
7
+ lon = location['longitude'].round(5)
8
+ lat = location['latitude'].round(5)
9
+ return request(
10
+ :get,
11
+ "/v4/#{mapid}/tilequery/#{lon},#{lat}.json?radius=#{radius}&limit=#{limit}",
12
+ nil)
13
+ end
14
+ end
15
+ 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.3.1'
6
6
  s.summary = 'Ruby bindings for the Mapbox API'
7
7
  s.description = ''
8
8
  s.authors = ['Tom MacWright']
@@ -10,8 +10,7 @@ spec = Gem::Specification.new do |s|
10
10
  s.homepage = 'https://mapbox.com/developers'
11
11
  s.license = 'MIT'
12
12
 
13
- s.add_dependency('rest-client', '~> 1.4')
14
- s.add_dependency('json', '~> 1.8.1')
13
+ s.add_dependency('rest-client', '~> 2.1.0')
15
14
 
16
15
  s.add_development_dependency('mocha', '~> 0.13.2')
17
16
  s.add_development_dependency('shoulda', '~> 3.4.0')
@@ -0,0 +1,4 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+ Dir[File.dirname(File.absolute_path(__FILE__)) + '/**/*_test.rb'].each {|file| require file }
@@ -9,14 +9,16 @@ 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
+ ], "cycling", {
20
+ geometries: "geojson"
21
+ })
20
22
  assert result
21
23
  end
22
24
  end
@@ -11,13 +11,60 @@ 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_forward (include types param)" do
43
+ Mapbox.access_token = ENV["MapboxAccessToken"]
44
+ result = Mapbox::Geocoder.geocode_forward("Washington", {:types => ['poi.landmark', 'address'], :foo_key => "foo_val", :bar_key => "bar_val"})
45
+ assert result
46
+ assert Mapbox.request_opts[:url].include? '?foo_key=foo_val&bar_key=bar_val&types=poi.landmark%2Caddress';
47
+ end
48
+
49
+ should "#geocode_reverse" do
15
50
  Mapbox.access_token = ENV["MapboxAccessToken"]
16
51
  result = Mapbox::Geocoder.geocode_reverse({
17
- "latitude" => 38,
18
- "longitude" => -100
52
+ :latitude => 38,
53
+ :longitude => -100
19
54
  })
20
55
  assert result
21
56
  end
57
+
58
+ should "#geocode_reverse (rounded coordinates to 5 decimal places)" do
59
+ Mapbox.access_token = ENV["MapboxAccessToken"]
60
+ result = Mapbox::Geocoder.geocode_reverse({
61
+ :latitude => 1.23456789,
62
+ :longitude => -99.8765432
63
+ })
64
+ for coord in result.first['query'] do
65
+ assert coord.to_s.split('.')[-1].length <= 5
66
+ end
67
+ end
68
+
22
69
  end
23
70
  end
@@ -0,0 +1,15 @@
1
+ require 'mapbox'
2
+ require 'test/unit'
3
+ require 'mocha/setup'
4
+ require 'shoulda'
5
+
6
+ module Mapbox
7
+ class IsochroneTest < Test::Unit::TestCase
8
+ should "#isochrone" do
9
+ Mapbox.access_token = ENV["MapboxAccessToken"]
10
+ result = Mapbox::Isochrone.isochrone(
11
+ "walking", "-118.22258,33.99038", {contours_minutes: [5,10,15]})
12
+ assert result
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,46 @@
1
+ require 'mapbox'
2
+ require 'test/unit'
3
+ require 'mocha/setup'
4
+ require 'shoulda'
5
+
6
+ module Mapbox
7
+ class MapMatchingTest < Test::Unit::TestCase
8
+ should "#map match with just coordinates and profile" do
9
+ Mapbox.access_token = ENV["MapboxAccessToken"]
10
+ result = Mapbox::MapMatching.map_matching([{
11
+ "longitude" => -117.17282,
12
+ "latitude" => 32.71204
13
+ }, {
14
+ "longitude" => -117.17288,
15
+ "latitude" => 32.71225
16
+ }, {
17
+ "longitude" => -117.17293,
18
+ "latitude" => 32.71244
19
+ },{
20
+ "longitude" => -117.17292,
21
+ "latitude" => 32.71256
22
+ }], "driving")
23
+ assert result
24
+ end
25
+ should "#map match with optional params" do
26
+ Mapbox.access_token = ENV["MapboxAccessToken"]
27
+ result = Mapbox::MapMatching.map_matching([{
28
+ "longitude" => -117.17282,
29
+ "latitude" => 32.71204
30
+ }, {
31
+ "longitude" => -117.17288,
32
+ "latitude" => 32.71225
33
+ }, {
34
+ "longitude" => -117.17293,
35
+ "latitude" => 32.71244
36
+ }, {
37
+ "longitude" => -117.17292,
38
+ "latitude" => 32.71256
39
+ }], "driving", {
40
+ annotations: ["duration", "distance", "speed"],
41
+ approaches: ["curb","curb","curb","curb"]
42
+ })
43
+ assert result
44
+ end
45
+ end
46
+ 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