Class: Signpost::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/signpost/router.rb

Constant Summary collapse

RACK_REQUEST_METHOD =
'REQUEST_METHOD'.freeze
RACK_REQUEST_PATH =
'PATH_INFO'.freeze
RACK_QUERY_HASH =
'rack.request.query_hash'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#named_routesObject (readonly)

Returns the value of attribute named_routes.



9
10
11
# File 'lib/signpost/router.rb', line 9

def named_routes
  @named_routes
end

#params_keyObject (readonly)

Returns the value of attribute params_key.



11
12
13
# File 'lib/signpost/router.rb', line 11

def params_key
  @params_key
end

#routesObject (readonly)

Returns the value of attribute routes.



7
8
9
# File 'lib/signpost/router.rb', line 7

def routes
  @routes
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/signpost/router.rb', line 13

def call(env)
  @routes[env[RACK_REQUEST_METHOD]].each do |route|
    params = route.match(env[RACK_REQUEST_PATH], env)

    case params
      when nil
        next
      when Hash
        if @options[:rack_params]
          env[RACK_QUERY_HASH] = env[RACK_QUERY_HASH] ? env[RACK_QUERY_HASH].merge(params) : params
        end

        env[params_key] = params

        return route.endpoint.call(env)
      else
        return params
    end
  end

  default_action
end

#expand(name, data = {}) ⇒ Object



36
37
38
# File 'lib/signpost/router.rb', line 36

def expand(name, data={})
  @named_routes[name].expand(data)
end