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.



367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/rufus/sixjo.rb', line 367

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)


381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/rufus/sixjo.rb', line 381

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