class OSRM::Configuration

Constants

DEFAULTS
DEMO_SERVER
MAPBOX_SERVER

Public Class Methods

new() click to toggle source
# File lib/osrm/configuration.rb, line 29
def initialize
  @data = {}
  merge!(DEFAULTS.dup)
end

Public Instance Methods

after_request=(after_request) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/osrm/configuration.rb, line 99
def cache=(cache)
  @data[:cache] = cache
  ensure_cache_version
end
cache_key(url = nil) click to toggle source
# 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) click to toggle source
# 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() click to toggle source

Raise an exception if the cache isn't compatible with the current library version

# 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) click to toggle source
# 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) click to toggle source
# File lib/osrm/configuration.rb, line 34
def merge!(options)
  options.each do |option, value|
    public_send(:"#{option}=", value) if DEFAULTS.key?(option.to_sym)
  end

  self
end
overview=(overview) click to toggle source
# File lib/osrm/configuration.rb, line 135
def overview=(overview)
  ensure_valid_overview(overview)
  @data[:overview] = overview
end
path_prefix=(path_prefix) click to toggle source
# 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) click to toggle source
# File lib/osrm/configuration.rb, line 63
def port=(port)
  @data[:port] = port&.to_i
end
server=(server) click to toggle source
# 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) click to toggle source
# File lib/osrm/configuration.rb, line 79
def timeout=(timeout)
  @data[:timeout] = timeout&.to_i
end
use_demo_server?() click to toggle source
# File lib/osrm/configuration.rb, line 55
def use_demo_server?
  @data[:server] == DEMO_SERVER
end
use_mapbox_server?() click to toggle source
# File lib/osrm/configuration.rb, line 59
def use_mapbox_server?
  @data[:server] == MAPBOX_SERVER
end
use_ssl=(use_ssl) click to toggle source
# File lib/osrm/configuration.rb, line 67
def use_ssl=(use_ssl)
  @data[:use_ssl] = use_ssl ? true : false
end