Class: Wouter::Wrapper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(routes) ⇒ Wrapper

Returns a new instance of Wrapper.



92
93
94
# File 'lib/wouter.rb', line 92

def initialize(routes)
  @routes = routes
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



90
91
92
# File 'lib/wouter.rb', line 90

def routes
  @routes
end

Instance Method Details

#call(env) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/wouter.rb', line 96

def call(env)
  request = Rack::Request.new(env)

  route = routes.find do |route|
    if route[:method] == request.request_method.to_sym
      if parameterized_path?(route[:path])
        split_path = route[:path].split("/")
        path_regex = build_path_regex(split_path)
        path_regex.match?(request.path)
      else
        route[:path] == request.path
      end
    end
  end

  if route
    if parameterized_path?(route[:path])
      split_path = route[:path].split("/")
      path_parameter_data = build_path_regex(split_path).match(request.path)
      path_parameter_names = find_parameter_names_in_path(split_path)
      path_parameter_names.each_with_index do |parameter_name, i|
        request.update_param(parameter_name, path_parameter_data[i+1])
      end
    end

    resp = route[:route_class].call(request.env)
    resp.finish
  else
    not_found
  end
end