Class: Rufus::Sixjo::Route

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

Overview

Wrapping all the details about route.match?(path) hereā€¦

Constant Summary collapse

C_CHAR =
'[^/?:,&#\.]'
R_PARAM =

capturing route params

/:(#{C_CHAR}+)/
R_FORMAT =

capturing the resource/file format

/\.(#{C_CHAR}+)$/

Instance Method Summary collapse

Constructor Details

#initialize(route, options) ⇒ Route

Returns a new instance of Route.



338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/rufus/sixjo.rb', line 338

def initialize (route, options)

  @param_keys = []

  @regex = route.gsub(R_PARAM) do
    @param_keys << $1.to_sym
    "(#{C_CHAR}+)" # ready to capture param values
  end

  @regex = /^#{@regex}(?:\.(#{C_CHAR}+))?$/

  # TODO : do something with the options :agent, :accept, ...
end

Instance Method Details

#match?(env) ⇒ Boolean

Returns:

  • (Boolean)


352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/rufus/sixjo.rb', line 352

def match? (env)

  m = env['PATH_INFO'].match(@regex)

  return false unless m

  values = m.to_a[1..-1]
  params = @param_keys.zip(values).inject({}) { |r, (k, v)| r[k] = v; r }

  env['_ROUTE_PARAMS'] = params

  env['_FORMAT'] = values.last if values.length > @param_keys.length

  true
end