osrm 0.3.0 → 0.4.0

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: 8597e75fad0414f8f8aa2b9ebce485b0f9ccd712
4
- data.tar.gz: 698e2fc52de22d09adfe7a06e57ba8f889a286f2
3
+ metadata.gz: 6c953314975877ae27e5bdd08fbeee9bfa05d81f
4
+ data.tar.gz: a94b634022234fe7dcf6526fb0d79416e22749b3
5
5
  SHA512:
6
- metadata.gz: dfc6876e927a578f05261f6519f9025040045ac49cbe2e785b81fed5f459b90c9f2425dc1954c358e883c9eff91db6b21f87eb5a8dae30ef77439d1378e15e74
7
- data.tar.gz: bf665ae1f754cd6486c9da9cb479b969fe5e277629759430e2979e367362ad8aa06f6d5887f80ab8c0220144fca0d1c0580d660bbbf4b0612a699a4c0651dae6
6
+ metadata.gz: 5122640743216979410cad71c32a0a91867564f8745425e61055eb4eb57e2383cf2ea47161f6880406bcaea21e5d8841199a0de3ac876dba4971586400373eae
7
+ data.tar.gz: f971c84db9359ca98ec542f191e6e8362c004f7c2ba711ef572e6eb574023e66f5642c6b0a7e31b3d42416e54425fcd9ae83969851c6b4d779ee79c40ca7137f
data/CHANGELOG.md ADDED
@@ -0,0 +1,30 @@
1
+ ## Changelog
2
+
3
+ ### OSRM Gem 0.4.0 (2016 Sep 17) ###
4
+
5
+ * Upgrade to server API 5
6
+ * Route object lost its summary attribute in favor of distance and duration
7
+ * Raise errors instead of printing warnings
8
+
9
+ ### OSRM Gem 0.3.0 (2014 Dec 21) ###
10
+
11
+ * Add before & after request callbacks
12
+
13
+ ### OSRM Gem 0.2.0 (2014 Dec 20) ###
14
+
15
+ * Add caching support
16
+ * Switch to MIT License
17
+
18
+ ### OSRM Gem 0.1.0 (2014 Dec 19) ###
19
+
20
+ Initial release:
21
+
22
+ * Add configuration options (server, port, use_ssl, timeout, user_agent)
23
+ * Implement route requesting
24
+
25
+ ### OSRM Gem 0.0.0 (2014 Dec 12) ###
26
+
27
+ First (and empty) release:
28
+
29
+ * Provide version number
30
+ * GPL v3 License
data/lib/osrm.rb CHANGED
@@ -13,12 +13,10 @@ module OSRM
13
13
  end
14
14
 
15
15
  def self.routes(*locations)
16
- OSRM::Query.new(*locations).execute
16
+ Query.new(*locations).execute
17
17
  end
18
18
 
19
19
  def self.route(*locations)
20
- # OPTIMIZE: Send a different request with alt=false
21
- # ...or not -> reusing the cache
22
20
  routes(*locations).first
23
21
  end
24
22
  end
@@ -36,7 +36,7 @@ module OSRM
36
36
 
37
37
  def server=(server)
38
38
  @data[:server] =
39
- server == :demo ? Configuration::DEMO_SERVER.dup : server
39
+ server == :demo ? DEMO_SERVER.dup : server
40
40
  end
41
41
 
42
42
  def use_demo_server?
@@ -44,7 +44,7 @@ module OSRM
44
44
  end
45
45
 
46
46
  def port=(port)
47
- @data[:port] = port && port.to_i
47
+ @data[:port] = port&.to_i
48
48
  end
49
49
 
50
50
  def use_ssl=(use_ssl)
@@ -52,12 +52,12 @@ module OSRM
52
52
  end
53
53
 
54
54
  def timeout=(timeout)
55
- @data[:timeout] = timeout && timeout.to_i
55
+ @data[:timeout] = timeout&.to_i
56
56
  end
57
57
 
58
58
  def before_request=(before_request)
59
59
  if before_request && !before_request.is_a?(Proc)
60
- fail "OSRM API error: Invalid before request #{before_request.inspect}"
60
+ raise "OSRM API error: Invalid before request #{before_request.inspect}"
61
61
  end
62
62
 
63
63
  @data[:before_request] = before_request
@@ -65,18 +65,44 @@ module OSRM
65
65
 
66
66
  def after_request=(after_request)
67
67
  if after_request && !after_request.is_a?(Proc)
68
- fail "OSRM API error: Invalid after request #{after_request.inspect}"
68
+ raise "OSRM API error: Invalid after request #{after_request.inspect}"
69
69
  end
70
70
 
71
71
  @data[:after_request] = after_request
72
72
  end
73
73
 
74
+ def cache=(cache)
75
+ @data[:cache] = cache
76
+ ensure_cache_version
77
+ end
78
+
79
+ # Raise an exception if major and minor versions of the cache and library are different
80
+ def ensure_cache_version
81
+ return unless cache
82
+
83
+ cache_version = cache[cache_key('version')]
84
+ if cache_version &&
85
+ Gem::Version.new(cache_version).bump != Gem::Version.new(OSRM::VERSION).bump
86
+ @data[:cache] = nil
87
+ raise "OSRM API error: Incompatible cache version #{cache_version}, expected #{OSRM::VERSION}"
88
+ end
89
+ end
90
+
91
+ def cache_key(url = nil)
92
+ if url
93
+ @data[:cache_key]&.gsub('{url}', url)
94
+ else
95
+ @data[:cache_key]
96
+ end
97
+ end
98
+
74
99
  def cache_key=(cache_key)
75
100
  unless cache_key.include?('{url}')
76
- fail "OSRM API error: Invalid cache key #{cache_key.inspect}"
101
+ raise "OSRM API error: Invalid cache key #{cache_key.inspect}"
77
102
  end
78
103
 
79
104
  @data[:cache_key] = cache_key
105
+ ensure_cache_version
80
106
  end
81
107
 
82
108
  # Dynamically add missing accessors
@@ -87,7 +113,9 @@ module OSRM
87
113
  end
88
114
  writer = :"#{option}="
89
115
 
90
- define_method(reader) { @data[option] }
116
+ unless method_defined?(reader)
117
+ define_method(reader) { @data[option] }
118
+ end
91
119
  unless method_defined?(writer)
92
120
  define_method(writer) { |value| @data[option] = value }
93
121
  end
data/lib/osrm/query.rb CHANGED
@@ -11,81 +11,55 @@ module OSRM
11
11
  @locations = locations.compact.reject(&:empty?)
12
12
  end
13
13
 
14
- # OPTIMIZE: Implement a custom locations= method
15
- # -> Remove consecutive duplicate locations (http://stackoverflow.com/a/8105422)
16
-
17
- # OPTIMIZE: Don't execute if 0 or 1 location(s)
18
14
  def execute
19
- json = fetch_json_data
20
- routes = []
21
- return routes if json.nil?
22
-
23
- # Main route
24
- routes << Route.new(
25
- geometry: json['route_geometry'],
26
- summary: json['route_summary']
27
- )
28
-
29
- # Alternative routes
30
- if json['found_alternative']
31
- json['alternative_geometries'].each_index do |index|
32
- routes << Route.new(
33
- geometry: json['alternative_geometries'][index],
34
- summary: json['alternative_summaries'][index]
35
- )
36
- end
37
- end
38
-
39
- routes
15
+ build_uri
16
+ fetch_json_data[:routes].map { |route| Route.new(route) }
40
17
  end
41
18
 
42
19
  private
43
20
 
44
21
  def fetch_json_data
45
- build_uri
46
22
  raw_data = cache || fetch_raw_data
47
23
 
48
- json = JSON.parse(raw_data) if raw_data
24
+ json = JSON.parse(raw_data, symbolize_names: true)
25
+ raise "OSRM API error: #{json[:message]} (#{json[:code]})" unless json[:code] == 'Ok'
49
26
 
50
27
  cache(raw_data)
51
28
  json
52
- rescue JSON::ParserError
53
- warn 'OSRM API error: Invalid JSON'
29
+ rescue JSON::ParserError => error
30
+ raise "OSRM API error: Invalid JSON: (#{error})"
54
31
  end
55
32
 
56
33
  def fetch_raw_data
57
34
  configuration.before_request.call if configuration.before_request
58
35
  response = api_request
59
36
  configuration.after_request.call if configuration.after_request
60
- # TODO: Be sure that after request is ALWAYS called???
61
- # (even if api_request failed)
62
37
 
63
- response.body if valid_response?(response)
38
+ ensure_valid_response(response)
39
+ response.body
64
40
  rescue SocketError
65
- warn 'OSRM API error: Unable to establish connection'
66
- rescue SystemCallError => err
41
+ raise 'OSRM API error: Unable to establish connection'
42
+ rescue SystemCallError => error
67
43
  # NOTE: Identify error class by string in case the class
68
44
  # is not implemented on the current platform
69
- case err.class.to_s
45
+ case error.class.to_s
70
46
  when 'Errno::EHOSTDOWN'
71
- warn 'OSRM API error: Host is down'
47
+ raise 'OSRM API error: Host is down'
72
48
  when 'Errno::ECONNREFUSED'
73
- warn 'OSRM API error: Connection refused'
49
+ raise 'OSRM API error: Connection refused'
74
50
  else
75
51
  raise
76
52
  end
77
- rescue TimeoutError
78
- warn 'OSRM API error: Timeout expired'
53
+ rescue Timeout::Error
54
+ raise 'OSRM API error: Timeout expired'
79
55
  end
80
56
 
81
- # TODO: Be sure that the HTTP query is ALWAYS closed
82
- # properly, even if an error happen (timeout or whatever)
83
57
  def api_request
84
- timeout(configuration.timeout) do
85
- Net::HTTP.start(uri.host, uri.port,
58
+ Timeout.timeout(configuration.timeout) do
59
+ Net::HTTP.start(@uri.host, @uri.port,
86
60
  use_ssl: configuration.use_ssl?) do |http|
87
61
  response = http.get(
88
- uri.request_uri,
62
+ @uri.request_uri,
89
63
  'User-Agent' => configuration.user_agent
90
64
  )
91
65
 
@@ -99,60 +73,57 @@ module OSRM
99
73
  end
100
74
  end
101
75
 
102
- def valid_response?(response)
103
- return true if response.is_a?(Net::HTTPOK)
76
+ def ensure_valid_response(response)
77
+ return if %w(200 400).include?(response.code)
104
78
 
105
79
  if configuration.use_demo_server? &&
106
80
  response['location'] &&
107
81
  response['location']['forbidden.html']
108
- warn 'OSRM API error: API usage policy has been violated, see https://github.com/Project-OSRM/osrm-backend/wiki/Api-usage-policy'
82
+ raise 'OSRM API error: API usage policy has been violated, see https://github.com/Project-OSRM/osrm-backend/wiki/Api-usage-policy'
109
83
  else
110
- warn 'OSRM API error: Invalid response' \
111
- " #{response.code} #{response.message}"
84
+ raise 'OSRM API error: Invalid response' \
85
+ " #{response.code} #{response.message}"
112
86
  end
113
-
114
- false
115
87
  end
116
88
 
117
89
  def build_uri
118
- fail "OSRM API error: Server isn't configured" unless configuration.server
90
+ raise "OSRM API error: Server isn't configured" unless configuration.server
91
+
92
+ service = 'route'
93
+ version = 'v1'
94
+ profile = 'driving'
95
+ format = 'json'
119
96
 
120
- service = 'viaroute'
121
97
  params = [
122
- *@locations.map { |l| ['loc', l] },
123
- %w(output json),
124
- %w(instructions false),
125
- %w(alt true)
98
+ %w(alternatives true),
99
+ %w(geometries polyline)
126
100
  ]
127
101
 
128
102
  uri_class = configuration.use_ssl? ? URI::HTTPS : URI::HTTP
129
103
  @uri = uri_class.build(
130
104
  host: configuration.server,
131
105
  port: configuration.port,
132
- path: "/#{service}",
106
+ path: "/#{service}/#{version}/#{profile}/#{lonlat_locations.join(';')}.#{format}",
133
107
  query: URI.encode_www_form(params)
134
108
  )
135
109
  end
136
110
 
137
- def uri
138
- @uri || build_uri
111
+ # Reverse from ['latitude,longitude'] to ['longitude,latitude']
112
+ def lonlat_locations
113
+ locations.map { |location| location.split(',').reverse.join(',') }
139
114
  end
140
115
 
141
116
  def cache(value = nil)
142
- return nil unless OSRM.configuration.cache
117
+ return nil unless configuration.cache
143
118
 
144
119
  if value
145
- OSRM.configuration.cache[cache_key('version')] ||= OSRM::VERSION
146
- OSRM.configuration.cache[cache_key(uri.to_s)] = value
120
+ configuration.cache[configuration.cache_key('version')] ||= OSRM::VERSION
121
+ configuration.cache[configuration.cache_key(@uri.to_s)] = value
147
122
  else
148
- OSRM.configuration.cache[cache_key(uri.to_s)]
123
+ configuration.cache[configuration.cache_key(@uri.to_s)]
149
124
  end
150
125
  end
151
126
 
152
- def cache_key(url)
153
- OSRM.configuration.cache_key.gsub('{url}', url)
154
- end
155
-
156
127
  def configuration
157
128
  OSRM.configuration
158
129
  end
data/lib/osrm/route.rb CHANGED
@@ -2,12 +2,12 @@ require 'encoded_polyline'
2
2
 
3
3
  module OSRM
4
4
  class Route
5
- # TODO: Rename from geometry to points???
6
- attr_accessor :geometry, :summary
5
+ attr_accessor :geometry, :distance, :duration
7
6
 
8
- def initialize(geometry: nil, summary: nil)
9
- @geometry = decode_geometry(geometry)
10
- @summary = summary
7
+ def initialize(json = {})
8
+ @geometry = decode_geometry(json[:geometry])
9
+ @distance = json[:distance] || 0
10
+ @duration = json[:duration] || 0
11
11
  end
12
12
 
13
13
  private
@@ -15,7 +15,7 @@ module OSRM
15
15
  def decode_geometry(geometry)
16
16
  return [] if geometry.nil? || geometry.empty?
17
17
 
18
- EncodedPolyline.decode_points(geometry, 6).map do |point|
18
+ EncodedPolyline.decode_points(geometry, 5).map do |point|
19
19
  point.map { |coordinate| fix_float_precision(coordinate) }
20
20
  end
21
21
  end
data/lib/osrm/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module OSRM
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
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.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Freayd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-21 00:00:00.000000000 Z
11
+ date: 2016-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -50,20 +50,20 @@ dependencies:
50
50
  requirements:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
- version: '10'
53
+ version: '11'
54
54
  - - ">="
55
55
  - !ruby/object:Gem::Version
56
- version: 10.4.0
56
+ version: 11.2.2
57
57
  type: :development
58
58
  prerelease: false
59
59
  version_requirements: !ruby/object:Gem::Requirement
60
60
  requirements:
61
61
  - - "~>"
62
62
  - !ruby/object:Gem::Version
63
- version: '10'
63
+ version: '11'
64
64
  - - ">="
65
65
  - !ruby/object:Gem::Version
66
- version: 10.4.0
66
+ version: 11.2.2
67
67
  - !ruby/object:Gem::Dependency
68
68
  name: encoded_polyline
69
69
  requirement: !ruby/object:Gem::Requirement
@@ -90,6 +90,7 @@ executables: []
90
90
  extensions: []
91
91
  extra_rdoc_files: []
92
92
  files:
93
+ - CHANGELOG.md
93
94
  - LICENSE
94
95
  - README.md
95
96
  - lib/osrm.rb
@@ -109,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
109
110
  requirements:
110
111
  - - ">="
111
112
  - !ruby/object:Gem::Version
112
- version: 2.0.0
113
+ version: 2.3.0
113
114
  required_rubygems_version: !ruby/object:Gem::Requirement
114
115
  requirements:
115
116
  - - ">="
@@ -117,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
118
  version: '0'
118
119
  requirements: []
119
120
  rubyforge_project:
120
- rubygems_version: 2.4.3
121
+ rubygems_version: 2.5.1
121
122
  signing_key:
122
123
  specification_version: 4
123
124
  summary: OSRM API for Ruby