osrm 0.4.1 → 0.4.2

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: 169960f4f55e9adb038d4d74352338acf90947b6
4
- data.tar.gz: 713c1b28e080c8c48e1f3d3327ae80f2ecf15dc3
3
+ metadata.gz: 92463b2268cde98a25fd17ee40da3cf7776b419d
4
+ data.tar.gz: 7137bacdd9c3138599a072a7f0a9739a81b3c09f
5
5
  SHA512:
6
- metadata.gz: ec89dcde3038a0ad936478b4fe8315494a25bcf4740eecbdaf7b71b8f1c822435385284d761491457b89810ceb00ca177756ea479eb94fb805dfdcd7f6e61238
7
- data.tar.gz: caa178c62c8ac16d7182da470afa56e58168c698289293a466fa236ff23115561877f9f6d48418322fa5d7226c43e3871efdc6cae96e06edb56884a3f440a300
6
+ metadata.gz: 5531d84abc60df3286e5a6df00adbe4f761b8af9b271473703977ddb8d08fcde15c0c28caa4ee47a12cbc4e9ba407ba4acfbd0254a47e61bbbea508b37d69f0d
7
+ data.tar.gz: 5942655353720c6484a1a8474e0b46e85c82c095c2f658285bc5e283357d363690dd03811b4572bf3628e95be4c4d9aeb1b76bb6fa2fe05c82464d2fc9e68be7
@@ -1,5 +1,10 @@
1
1
  ## Changelog
2
2
 
3
+ ### OSRM Gem 0.4.2 (2017 Feb 23) ###
4
+
5
+ * Support Mapbox server
6
+ * Support routing profiles
7
+
3
8
  ### OSRM Gem 0.4.1 (2016 Sep 17) ###
4
9
 
5
10
  * Add overview option
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014 Freayd
1
+ Copyright (c) 2014-2017 Freayd
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  ## OSRM Gem
2
2
 
3
3
  OSRM Gem is a Ruby API that allows you to request [Open Source Routing Machine](http://project-osrm.org/) servers.
4
- You can [run your own server](https://github.com/Project-OSRM/osrm-backend/wiki) or use the [demo server](https://github.com/Project-OSRM/osrm-backend/wiki/Api-usage-policy).
4
+ You can [run your own server](https://github.com/Project-OSRM/osrm-backend/wiki), use the [OSRM demo server](https://github.com/Project-OSRM/osrm-backend/wiki/Demo-server) or the [Mapbox server](https://www.mapbox.com/developers/).
5
5
 
6
6
  ## Usage
7
7
 
@@ -18,6 +18,11 @@ You can [run your own server](https://github.com/Project-OSRM/osrm-backend/wiki)
18
18
  server: 'example.com', # Must be specified
19
19
  port: 8080, # Default: 80 or 443 if SSL
20
20
  use_ssl: true, # Default: false
21
+ ## OSRM demo server connection
22
+ # server: :demo,
23
+ ## Mapbox server connection
24
+ # server: :mapbox,
25
+ # api_key: 'access-token',
21
26
 
22
27
  # Connection (advanced)
23
28
  timeout: 10, # Default: 3
@@ -40,8 +45,9 @@ You can [run your own server](https://github.com/Project-OSRM/osrm-backend/wiki)
40
45
  3. Request
41
46
 
42
47
  OSRM.routes('50.202712,8.582738', '50.20232,8.574447')
43
- OSRM.route('50.202712,8.582738', '50.20232,8.574447')
48
+ OSRM.route('50.202712,8.582738', '50.20232,8.574447', '50.2099431,8.5710665')
49
+ OSRM.route_by_cycling('45.734818,7.3219649', '45.868829,7.1533417')
44
50
 
45
51
  ## License
46
52
 
47
- OSRM Gem is released under [MIT License](http://opensource.org/licenses/MIT).
53
+ OSRM Gem is released under [MIT License](https://opensource.org/licenses/MIT).
@@ -12,11 +12,27 @@ module OSRM
12
12
  Configuration.instance
13
13
  end
14
14
 
15
- def self.routes(*locations, overview: nil)
16
- Query.new(*locations).execute(alternatives: true, overview: overview)
15
+ def self.routes(*locations, overview: nil, profile: nil)
16
+ Query.new(*locations)
17
+ .execute(alternatives: true, overview: overview, profile: profile)
17
18
  end
18
19
 
19
- def self.route(*locations, overview: nil)
20
- Query.new(*locations).execute(alternatives: false, overview: overview).first
20
+ def self.route(*locations, overview: nil, profile: nil)
21
+ Query.new(*locations)
22
+ .execute(alternatives: false, overview: overview, profile: profile)
23
+ .first
24
+ end
25
+
26
+ def self.method_missing(method, *arguments, **keyword_arguments)
27
+ if /^(?<delegate>routes?)_by_(?<profile>[a-z]+)$/ =~ method
28
+ keyword_arguments[:profile] = profile
29
+ method(delegate).call(*arguments, **keyword_arguments)
30
+ else
31
+ super
32
+ end
33
+ end
34
+
35
+ def self.respond_to_missing?(method, *)
36
+ /^routes?_by_[a-z]+$/.match(method) || super
21
37
  end
22
38
  end
@@ -9,6 +9,7 @@ module OSRM
9
9
  server: nil,
10
10
  port: nil,
11
11
  use_ssl: false,
12
+ api_key: nil,
12
13
 
13
14
  timeout: 3,
14
15
  user_agent: "OSRMRubyGem/#{OSRM::VERSION}",
@@ -21,7 +22,8 @@ module OSRM
21
22
  overview: :simplified
22
23
  }.freeze
23
24
 
24
- DEMO_SERVER = 'router.project-osrm.org'.freeze
25
+ DEMO_SERVER = 'router.project-osrm.org'.freeze
26
+ MAPBOX_SERVER = 'api.mapbox.com'.freeze
25
27
 
26
28
  def initialize
27
29
  @data = {}
@@ -38,13 +40,25 @@ module OSRM
38
40
 
39
41
  def server=(server)
40
42
  @data[:server] =
41
- server == :demo ? DEMO_SERVER.dup : server
43
+ case server
44
+ when :demo, DEMO_SERVER
45
+ DEMO_SERVER.dup
46
+ when :mapbox, MAPBOX_SERVER
47
+ self.use_ssl = true
48
+ MAPBOX_SERVER.dup
49
+ else
50
+ server
51
+ end
42
52
  end
43
53
 
44
54
  def use_demo_server?
45
55
  @data[:server] == DEMO_SERVER
46
56
  end
47
57
 
58
+ def use_mapbox_server?
59
+ @data[:server] == MAPBOX_SERVER
60
+ end
61
+
48
62
  def port=(port)
49
63
  @data[:port] = port&.to_i
50
64
  end
@@ -78,15 +92,17 @@ module OSRM
78
92
  ensure_cache_version
79
93
  end
80
94
 
81
- # Raise an exception if major and minor versions of the cache and library are different
95
+ # Raise an exception if the cache isn't compatible with the current library version
82
96
  def ensure_cache_version
83
97
  return unless cache
84
98
 
85
99
  cache_version = cache[cache_key('version')]
100
+ minimum_version = '0.4.0'
86
101
  if cache_version &&
87
- Gem::Version.new(cache_version).bump != Gem::Version.new(OSRM::VERSION).bump
102
+ Gem::Version.new(cache_version) < Gem::Version.new(minimum_version)
88
103
  @data[:cache] = nil
89
- raise "OSRM API error: Incompatible cache version #{cache_version}, expected #{OSRM::VERSION}"
104
+ raise "OSRM API error: Incompatible cache version #{cache_version}, " +
105
+ "expected #{minimum_version} or higher"
90
106
  end
91
107
  end
92
108
 
@@ -11,8 +11,8 @@ module OSRM
11
11
  @locations = locations.compact.reject(&:empty?)
12
12
  end
13
13
 
14
- def execute(alternatives: nil, overview: nil)
15
- build_uri(alternatives: alternatives, overview: overview)
14
+ def execute(alternatives: nil, overview: nil, profile: nil)
15
+ build_uri(alternatives: alternatives, overview: overview, profile: profile)
16
16
  fetch_json_data[:routes].map { |route| Route.new(route) }
17
17
  end
18
18
 
@@ -22,7 +22,7 @@ module OSRM
22
22
  raw_data = cache || fetch_raw_data
23
23
 
24
24
  json = JSON.parse(raw_data, symbolize_names: true)
25
- raise "OSRM API error: #{json[:message]} (#{json[:code]})" unless json[:code] == 'Ok'
25
+ raise "OSRM API error: #{json[:message]}#{' ('+json[:code]+')' if json[:code]}" unless json[:code] == 'Ok'
26
26
 
27
27
  cache(raw_data)
28
28
  json
@@ -76,22 +76,36 @@ module OSRM
76
76
  def ensure_valid_response(response)
77
77
  return if %w(200 400).include?(response.code)
78
78
 
79
+ begin
80
+ return if response.body.start_with?('{') && JSON.parse(response.body)
81
+ rescue JSON::ParserError
82
+ nil
83
+ end
84
+
79
85
  if configuration.use_demo_server? &&
80
- response['location'] &&
81
- response['location']['forbidden.html']
86
+ response['location']&.include?('forbidden.html')
82
87
  raise 'OSRM API error: API usage policy has been violated, see https://github.com/Project-OSRM/osrm-backend/wiki/Api-usage-policy'
83
- else
84
- raise 'OSRM API error: Invalid response' \
85
- " #{response.code} #{response.message}"
86
88
  end
89
+
90
+ ensure_ready_server if response.code == '404'
91
+ raise 'OSRM API error: Invalid response' + \
92
+ " #{response.code} #{response.message}"
87
93
  end
88
94
 
89
- def build_uri(alternatives: nil, overview: nil)
95
+ def ensure_ready_server
96
+ root_uri = @uri.class.build(host: @uri.host, port: @uri.port)
97
+ root_response = Net::HTTP.get(root_uri)
98
+ raise "OSRM API error: #{root_response}" if root_response.match(/NOT READY/i)
99
+ end
100
+
101
+ def build_uri(alternatives: nil, overview: nil, profile: nil)
90
102
  raise "OSRM API error: Server isn't configured" unless configuration.server
91
103
 
92
- service = 'route'
93
- version = 'v1'
94
- profile = 'driving'
104
+ use_mapbox = configuration.use_mapbox_server?
105
+ service = use_mapbox ? 'directions' : 'route'
106
+ version = use_mapbox ? 'v5' : 'v1'
107
+ profile ||= 'driving'
108
+ profile.prepend('mapbox/') if use_mapbox
95
109
  format = 'json'
96
110
 
97
111
  alternatives = alternatives.nil? ? false : (alternatives == true)
@@ -107,6 +121,8 @@ module OSRM
107
121
  ['overview', overview.to_s]
108
122
  ]
109
123
 
124
+ params << ['access_token', configuration.api_key] if use_mapbox
125
+
110
126
  uri_class = configuration.use_ssl? ? URI::HTTPS : URI::HTTP
111
127
  @uri = uri_class.build(
112
128
  host: configuration.server,
@@ -1,3 +1,3 @@
1
1
  module OSRM
2
- VERSION = '0.4.1'
2
+ VERSION = '0.4.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: osrm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Freayd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-17 00:00:00.000000000 Z
11
+ date: 2017-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -48,22 +48,22 @@ dependencies:
48
48
  name: rake
49
49
  requirement: !ruby/object:Gem::Requirement
50
50
  requirements:
51
- - - "~>"
52
- - !ruby/object:Gem::Version
53
- version: '11'
54
51
  - - ">="
55
52
  - !ruby/object:Gem::Version
56
- version: 11.2.2
53
+ version: 10.2.0
54
+ - - "<"
55
+ - !ruby/object:Gem::Version
56
+ version: '13.0'
57
57
  type: :development
58
58
  prerelease: false
59
59
  version_requirements: !ruby/object:Gem::Requirement
60
60
  requirements:
61
- - - "~>"
62
- - !ruby/object:Gem::Version
63
- version: '11'
64
61
  - - ">="
65
62
  - !ruby/object:Gem::Version
66
- version: 11.2.2
63
+ version: 10.2.0
64
+ - - "<"
65
+ - !ruby/object:Gem::Version
66
+ version: '13.0'
67
67
  - !ruby/object:Gem::Dependency
68
68
  name: encoded_polyline
69
69
  requirement: !ruby/object:Gem::Requirement
@@ -118,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
118
  version: '0'
119
119
  requirements: []
120
120
  rubyforge_project:
121
- rubygems_version: 2.5.1
121
+ rubygems_version: 2.6.8
122
122
  signing_key:
123
123
  specification_version: 4
124
124
  summary: OSRM API for Ruby