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 +4 -4
- data/CHANGELOG.md +5 -0
- data/LICENSE +1 -1
- data/README.md +9 -3
- data/lib/osrm.rb +20 -4
- data/lib/osrm/configuration.rb +21 -5
- data/lib/osrm/query.rb +28 -12
- data/lib/osrm/version.rb +1 -1
- metadata +11 -11
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 92463b2268cde98a25fd17ee40da3cf7776b419d
|
|
4
|
+
data.tar.gz: 7137bacdd9c3138599a072a7f0a9739a81b3c09f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5531d84abc60df3286e5a6df00adbe4f761b8af9b271473703977ddb8d08fcde15c0c28caa4ee47a12cbc4e9ba407ba4acfbd0254a47e61bbbea508b37d69f0d
|
|
7
|
+
data.tar.gz: 5942655353720c6484a1a8474e0b46e85c82c095c2f658285bc5e283357d363690dd03811b4572bf3628e95be4c4d9aeb1b76bb6fa2fe05c82464d2fc9e68be7
|
data/CHANGELOG.md
CHANGED
data/LICENSE
CHANGED
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)
|
|
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](
|
|
53
|
+
OSRM Gem is released under [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/osrm.rb
CHANGED
|
@@ -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)
|
|
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)
|
|
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
|
data/lib/osrm/configuration.rb
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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
|
|
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)
|
|
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},
|
|
104
|
+
raise "OSRM API error: Incompatible cache version #{cache_version}, " +
|
|
105
|
+
"expected #{minimum_version} or higher"
|
|
90
106
|
end
|
|
91
107
|
end
|
|
92
108
|
|
data/lib/osrm/query.rb
CHANGED
|
@@ -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]} (
|
|
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
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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,
|
data/lib/osrm/version.rb
CHANGED
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.
|
|
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:
|
|
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:
|
|
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:
|
|
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.
|
|
121
|
+
rubygems_version: 2.6.8
|
|
122
122
|
signing_key:
|
|
123
123
|
specification_version: 4
|
|
124
124
|
summary: OSRM API for Ruby
|