Class: Decidim::Dev::Test::MapServer

Inherits:
Object
  • Object
show all
Defined in:
lib/decidim/dev/test/map_server.rb

Overview

The test map server serves all map related requests for the app.

Works as a rack middleware that is mounted to the Rails app during tests (at the decidim-dev module’s engine).

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ MapServer

Returns a new instance of MapServer.



30
31
32
# File 'lib/decidim/dev/test/map_server.rb', line 30

def initialize(app)
  @app = app
end

Class Method Details

.hostObject



11
12
13
# File 'lib/decidim/dev/test/map_server.rb', line 11

def self.host
  "http://#{hostname}:#{Capybara.server_port}"
end

.hostnameObject



15
16
17
# File 'lib/decidim/dev/test/map_server.rb', line 15

def self.hostname
  "maps.lvh.me"
end

.url(endpoint) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/decidim/dev/test/map_server.rb', line 19

def self.url(endpoint)
  case endpoint
  when :tiles
    "#{host}/maptiles/{z}/{x}/{y}.png"
  when :static
    "#{host}/static"
  when :autocomplete
    "#{host}/photon_api"
  end
end

Instance Method Details

#call(env) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/decidim/dev/test/map_server.rb', line 34

def call(env)
  request = Rack::Request.new(env)
  return @app.call(env) unless request.host == self.class.hostname

  if (match = request.path.match(%r{^/maptiles/([0-9]+)/([0-9]+)/([0-9]+).png$}))
    return serve_maptiles(request, { z: match[1], x: match[2], y: match[3] })
  elsif request.path == "/static"
    return serve_static(request)
  elsif request.path == "/photon_api"
    return serve_autocomplete(request)
  end

  not_found
end