Class: Gin::Router::Route

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/gin/router.rb

Overview

Represents an HTTP path and path matcher, with inline params, and new path generation functionality.

r = Route.new "get", "/foo/:id.:format", [FooController, :show], :show_foo
r.to_path id: 123, format: "json"
#=> "/foo/123.json"

Constant Summary collapse

SEP =

:nodoc:

"/"
VAR_MATCHER =

:nodoc:

/:(\w+)/
PARAM_MATCHER =

:nodoc:

"(.*?)"

Constants included from Constants

Constants::ASYNC_CALLBACK, Constants::CACHE_CTRL, Constants::CNT_DISPOSITION, Constants::CNT_LENGTH, Constants::CNT_TYPE, Constants::ENV_DEV, Constants::ENV_PROD, Constants::ENV_STAGE, Constants::ENV_TEST, Constants::EPOCH, Constants::ETAG, Constants::EXPIRES, Constants::FWD_FOR, Constants::FWD_HOST, Constants::GIN_APP, Constants::GIN_CTRL, Constants::GIN_ERRORS, Constants::GIN_PATH_PARAMS, Constants::GIN_RELOADED, Constants::GIN_ROUTE, Constants::GIN_STACK, Constants::GIN_STATIC, Constants::GIN_TARGET, Constants::GIN_TEMPLATES, Constants::GIN_TIMESTAMP, Constants::HOST_NAME, Constants::HTTP_VERSION, Constants::IF_MATCH, Constants::IF_MOD_SINCE, Constants::IF_NONE_MATCH, Constants::IF_UNMOD_SINCE, Constants::LAST_MOD, Constants::LOCATION, Constants::PATH_INFO, Constants::PRAGMA, Constants::QUERY_STRING, Constants::RACK_INPUT, Constants::REMOTE_ADDR, Constants::REMOTE_USER, Constants::REQ_METHOD, Constants::SERVER_NAME, Constants::SERVER_PORT, Constants::SESSION_SECRET

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verb, path, target = [], name = nil) ⇒ Route

Returns a new instance of Route.



170
171
172
173
174
# File 'lib/gin/router.rb', line 170

def initialize verb, path, target=[], name=nil
  @target = target
  @name   = name.to_sym if name
  build verb, path
end

Instance Attribute Details

#match_keysObject (readonly)

Array of path parts for tree-based matching.



151
152
153
# File 'lib/gin/router.rb', line 151

def match_keys
  @match_keys
end

#nameObject (readonly)

Arbitrary name of the route.



160
161
162
# File 'lib/gin/router.rb', line 160

def name
  @name
end

#param_keysObject (readonly)

Parsed out path param key names.



148
149
150
# File 'lib/gin/router.rb', line 148

def param_keys
  @param_keys
end

#pathObject (readonly)

Computed path String with wildcards.



154
155
156
# File 'lib/gin/router.rb', line 154

def path
  @path
end

#targetObject (readonly)

Target of the route, in this case an Array with controller and action.



157
158
159
# File 'lib/gin/router.rb', line 157

def target
  @target
end

#verbObject (readonly)

HTTP verb used by the route.



163
164
165
# File 'lib/gin/router.rb', line 163

def verb
  @verb
end

Instance Method Details

#===(other) ⇒ Object

Returns true if the argument matches the route_id. The route id is an array with verb and original path.



221
222
223
# File 'lib/gin/router.rb', line 221

def === other
  @route_id == other
end

#to_env(params = {}, headers = {}) ⇒ Object

Creates a Rack env hash with the given params and headers.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/gin/router.rb', line 199

def to_env params={}, headers={}
  headers ||= {}
  params  ||= {}

  path_info, query_string = to_path(params).split('?', 2)

  env = headers.merge(
    PATH_INFO => path_info,
    REQ_METHOD => @verb,
    QUERY_STRING => query_string
  )

  # TODO: implement multipart streams for requests that support a body
  env[RACK_INPUT] ||= ''
  env
end

#to_path(params = {}) ⇒ Object

Render a route path by giving it inline (and other) params.

route.to_path :id => 123, :format => "json", :foo => "bar"
#=> "/foo/123.json?foo=bar"


182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/gin/router.rb', line 182

def to_path params={}
  params ||= {}
  rendered_path = @path.dup
  rendered_path = rendered_path % @param_keys.map do |k|
    val = params.delete(k) || params.delete(k.to_sym)
    raise(PathArgumentError, "Missing param #{k}") unless val
    CGI.escape(val.to_s)
  end unless @param_keys.empty?

  rendered_path << "?#{Gin.build_query(params)}" unless params.empty?
  rendered_path
end