Class: Rack::AppMapper

Inherits:
Object show all
Defined in:
lib/marfa/rack/app_mapper.rb

Constant Summary collapse

NOT_FOUND =
[404, { CONTENT_TYPE => 'text/plain' }, []].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apps, app_map = {}, catch = [404, 405]) ⇒ AppMapper

Returns a new instance of AppMapper.



7
8
9
10
11
12
13
14
15
16
# File 'lib/marfa/rack/app_mapper.rb', line 7

def initialize(apps, app_map = {}, catch = [404, 405])
  @apps = []
  @has_app = {}
  @app_map = app_map

  apps.each { |app| add app }

  @catch = {}
  [*catch].each { |status| @catch[status] = true }
end

Instance Attribute Details

#appsObject (readonly)

Returns the value of attribute apps.



5
6
7
# File 'lib/marfa/rack/app_mapper.rb', line 5

def apps
  @apps
end

Instance Method Details

#add(app) ⇒ Object Also known as: <<



33
34
35
36
# File 'lib/marfa/rack/app_mapper.rb', line 33

def add(app)
  @has_app[app] = true
  @apps << app
end

#call(env) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/marfa/rack/app_mapper.rb', line 18

def call(env)
  res = NOT_FOUND

  @app_map.keys.each do |key|
    uri = env['PATH_INFO']
    next unless uri == key.to_s || uri.include?(key.to_s)

    app = @app_map[key]
    res = app.call(env)
    break
  end

  res
end

#include?(app) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/marfa/rack/app_mapper.rb', line 38

def include?(app)
  @has_app.include? app
end