Class: Rosendo::Routes::Route::Path

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

Defined Under Namespace

Classes: URL

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Path

Returns a new instance of Path.



37
38
39
40
# File 'lib/rosendo/routes.rb', line 37

def initialize(path)
  @path = path
  @regexp, @keys = parse
end

Instance Method Details

#matches?(url) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/rosendo/routes.rb', line 42

def matches?(url)
  url = URL.new(url)
  url.path =~ @regexp
end

#params(url) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/rosendo/routes.rb', line 47

def params(url)
  url = URL.new(url)
  match = url.path.match(@regexp)
  {}.tap do |params|
    @keys.each_with_index do |key, i|
      params[key] = match[i + 1]
    end
  end.merge(url.query_params)
end

#parseObject



57
58
59
60
61
62
63
64
# File 'lib/rosendo/routes.rb', line 57

def parse
  keys = []
  pattern = @path.gsub(/:(\w+)/) do |match|
    keys << $1.to_sym
    '(\w+)'
  end
  [Regexp.new("^#{pattern}$"), keys]
end