Class: OSRM::Configuration

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/osrm/configuration.rb

Constant Summary collapse

DEFAULTS =
{
  server:  nil,
  port:    nil,
  use_ssl: false,
  api_key: nil,

  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

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



28
29
30
31
# File 'lib/osrm/configuration.rb', line 28

def initialize
  @data = {}
  merge!(DEFAULTS.dup)
end

Instance Method Details

#after_request=(after_request) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/osrm/configuration.rb', line 82

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



74
75
76
77
78
79
80
# File 'lib/osrm/configuration.rb', line 74

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



90
91
92
93
# File 'lib/osrm/configuration.rb', line 90

def cache=(cache)
  @data[:cache] = cache
  ensure_cache_version
end

#cache_key(url = nil) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/osrm/configuration.rb', line 109

def cache_key(url = nil)
  if url
    @data[:cache_key]&.gsub('{url}', url)
  else
    @data[:cache_key]
  end
end

#cache_key=(cache_key) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/osrm/configuration.rb', line 117

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_versionObject

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



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/osrm/configuration.rb', line 96

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



131
132
133
134
135
# File 'lib/osrm/configuration.rb', line 131

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



33
34
35
36
37
38
39
# File 'lib/osrm/configuration.rb', line 33

def merge!(options)
  options.each do |option, value|
    public_send(:"#{option}=", value) if DEFAULTS.key?(option.to_sym)
  end

  self
end

#overview=(overview) ⇒ Object



126
127
128
129
# File 'lib/osrm/configuration.rb', line 126

def overview=(overview)
  ensure_valid_overview(overview)
  @data[:overview] = overview
end

#port=(port) ⇒ Object



62
63
64
# File 'lib/osrm/configuration.rb', line 62

def port=(port)
  @data[:port] = port&.to_i
end

#server=(server) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/osrm/configuration.rb', line 41

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



70
71
72
# File 'lib/osrm/configuration.rb', line 70

def timeout=(timeout)
  @data[:timeout] = timeout&.to_i
end

#use_demo_server?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/osrm/configuration.rb', line 54

def use_demo_server?
  @data[:server] == DEMO_SERVER
end

#use_mapbox_server?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/osrm/configuration.rb', line 58

def use_mapbox_server?
  @data[:server] == MAPBOX_SERVER
end

#use_ssl=(use_ssl) ⇒ Object



66
67
68
# File 'lib/osrm/configuration.rb', line 66

def use_ssl=(use_ssl)
  @data[:use_ssl] = use_ssl ? true : false
end