Class: Wayfarer::Routing::QueryMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/wayfarer/routing/query_matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields) ⇒ QueryMatcher

Returns a new instance of QueryMatcher.



8
9
10
# File 'lib/wayfarer/routing/query_matcher.rb', line 8

def initialize(fields)
  @fields = fields
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



6
7
8
# File 'lib/wayfarer/routing/query_matcher.rb', line 6

def fields
  @fields
end

Instance Method Details

#match(url) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/wayfarer/routing/query_matcher.rb', line 12

def match(url)
  query = url.query

  # CGI::parse throws a NoMethodError if the query is an empty string
  return false if query.nil? || query.empty?

  CGI.parse(query).none? { |field, vals| violates?(field, vals) }
end

#params(url) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/wayfarer/routing/query_matcher.rb', line 21

def params(url)
  return {} unless match(url)

  CGI.parse(url.query)
     .select { |(k, _)| fields.keys.include?(k.to_sym) }
     .transform_values(&:last)
end