Class: UrlRegexp::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/url_regexp/query.rb

Instance Method Summary collapse

Constructor Details

#initializeQuery

Returns a new instance of Query.



3
4
5
# File 'lib/url_regexp/query.rb', line 3

def initialize
  @queries = []
end

Instance Method Details

#append(query) ⇒ Object



7
8
9
10
11
# File 'lib/url_regexp/query.rb', line 7

def append(query)
  if !query.nil?
    @queries << query.to_s.split('&').reject(&:empty?)
  end
end

#to_regexp_sObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/url_regexp/query.rb', line 13

def to_regexp_s
  common_queries = @queries.reduce { |q1, q2| q1 & q2 } || []
  if 1 < common_queries.size
    "\\?(#{common_queries.map { |q| Regexp.quote(q) }.permutation.to_a.map { |qs| "(.*&)?#{qs.join('.*&')}(&.*)?" }.join('|')})"
  elsif 1 == common_queries.size
    "\\?(.*&)?#{Regexp.quote(common_queries.first)}(&.*)?"
  else
    "(\\?.*)?"
  end
end