Class: OSRM::Configuration
- Inherits:
-
Object
- Object
- OSRM::Configuration
- Includes:
- Singleton
- Defined in:
- lib/osrm/configuration.rb
Constant Summary collapse
- DEFAULTS =
{ server: nil, port: nil, use_ssl: false, api_key: nil, path_prefix: '', timeout: 3, user_agent: "OSRMRubyGem/#{OSRM::VERSION}", before_request: nil, after_request: nil, cache: nil, cache_key: 'osrm:{url}', overview: :simplified }.freeze
- DEMO_SERVER =
'router.project-osrm.org'.freeze
- MAPBOX_SERVER =
'api.mapbox.com'.freeze
Instance Method Summary collapse
- #after_request=(after_request) ⇒ Object
- #before_request=(before_request) ⇒ Object
- #cache=(cache) ⇒ Object
- #cache_key(url = nil) ⇒ Object
- #cache_key=(cache_key) ⇒ Object
-
#ensure_cache_version ⇒ Object
Raise an exception if the cache isn’t compatible with the current library version.
- #ensure_valid_overview(overview) ⇒ Object
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #merge!(options) ⇒ Object
- #overview=(overview) ⇒ Object
- #path_prefix=(path_prefix) ⇒ Object
- #port=(port) ⇒ Object
- #server=(server) ⇒ Object
- #timeout=(timeout) ⇒ Object
- #use_demo_server? ⇒ Boolean
- #use_mapbox_server? ⇒ Boolean
- #use_ssl=(use_ssl) ⇒ Object
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
29 30 31 32 |
# File 'lib/osrm/configuration.rb', line 29 def initialize @data = {} merge!(DEFAULTS.dup) end |
Instance Method Details
#after_request=(after_request) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/osrm/configuration.rb', line 91 def after_request=(after_request) if after_request && !after_request.is_a?(Proc) raise "OSRM API error: Invalid after request #{after_request.inspect}" end @data[:after_request] = after_request end |
#before_request=(before_request) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/osrm/configuration.rb', line 83 def before_request=(before_request) if before_request && !before_request.is_a?(Proc) raise "OSRM API error: Invalid before request #{before_request.inspect}" end @data[:before_request] = before_request end |
#cache=(cache) ⇒ Object
99 100 101 102 |
# File 'lib/osrm/configuration.rb', line 99 def cache=(cache) @data[:cache] = cache ensure_cache_version end |
#cache_key(url = nil) ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/osrm/configuration.rb', line 118 def cache_key(url = nil) if url @data[:cache_key]&.gsub('{url}', url) else @data[:cache_key] end end |
#cache_key=(cache_key) ⇒ Object
126 127 128 129 130 131 132 133 |
# File 'lib/osrm/configuration.rb', line 126 def cache_key=(cache_key) unless cache_key.include?('{url}') raise "OSRM API error: Invalid cache key #{cache_key.inspect}" end @data[:cache_key] = cache_key ensure_cache_version end |
#ensure_cache_version ⇒ Object
Raise an exception if the cache isn’t compatible with the current library version
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/osrm/configuration.rb', line 105 def ensure_cache_version return unless cache cache_version = cache[cache_key('version')] minimum_version = '0.4.0' if cache_version && Gem::Version.new(cache_version) < Gem::Version.new(minimum_version) @data[:cache] = nil raise "OSRM API error: Incompatible cache version #{cache_version}, " + "expected #{minimum_version} or higher" end end |
#ensure_valid_overview(overview) ⇒ Object
140 141 142 143 144 |
# File 'lib/osrm/configuration.rb', line 140 def ensure_valid_overview(overview) unless [false, :simplified, :full].include?(overview) raise "OSRM API error: Invalid overview type #{overview.inspect}" end end |
#merge!(options) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/osrm/configuration.rb', line 34 def merge!() .each do |option, value| public_send(:"#{option}=", value) if DEFAULTS.key?(option.to_sym) end self end |
#overview=(overview) ⇒ Object
135 136 137 138 |
# File 'lib/osrm/configuration.rb', line 135 def overview=(overview) ensure_valid_overview(overview) @data[:overview] = overview end |
#path_prefix=(path_prefix) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/osrm/configuration.rb', line 71 def path_prefix=(path_prefix) unless path_prefix.empty? || path_prefix.match?(/\A\/.*[^\/]\z/) raise "OSRM API error: Invalid path prefix #{path_prefix.inspect}" end @data[:path_prefix] = path_prefix end |
#port=(port) ⇒ Object
63 64 65 |
# File 'lib/osrm/configuration.rb', line 63 def port=(port) @data[:port] = port&.to_i end |
#server=(server) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/osrm/configuration.rb', line 42 def server=(server) @data[:server] = case server when :demo, DEMO_SERVER DEMO_SERVER.dup when :mapbox, MAPBOX_SERVER self.use_ssl = true MAPBOX_SERVER.dup else server end end |
#timeout=(timeout) ⇒ Object
79 80 81 |
# File 'lib/osrm/configuration.rb', line 79 def timeout=(timeout) @data[:timeout] = timeout&.to_i end |
#use_demo_server? ⇒ Boolean
55 56 57 |
# File 'lib/osrm/configuration.rb', line 55 def use_demo_server? @data[:server] == DEMO_SERVER end |
#use_mapbox_server? ⇒ Boolean
59 60 61 |
# File 'lib/osrm/configuration.rb', line 59 def use_mapbox_server? @data[:server] == MAPBOX_SERVER end |
#use_ssl=(use_ssl) ⇒ Object
67 68 69 |
# File 'lib/osrm/configuration.rb', line 67 def use_ssl=(use_ssl) @data[:use_ssl] = use_ssl ? true : false end |