Class: HALDecorator::Pagination::Uri

Inherits:
Object
  • Object
show all
Defined in:
lib/hal_decorator/pagination.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, query) ⇒ Uri

Returns a new instance of Uri.



31
32
33
34
# File 'lib/hal_decorator/pagination.rb', line 31

def initialize(uri, query)
  @uri = uri
  @query = query
end

Class Method Details

.parse(str) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/hal_decorator/pagination.rb', line 11

def self.parse(str)
  uri = nil
  query = nil
  unless str.nil? || str.empty?
    if m = str.match(%r(\A([^\?]+)\??([^\#]+)?.*\Z))
      uri = m[1]
      query = query_from_string m[2]
    end
  end
  new(uri, query)
end

.query_from_string(str) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/hal_decorator/pagination.rb', line 23

def self.query_from_string(str)
  return {} if str.nil? || str.empty?
  str.split('&').each_with_object({}) do |pair, q|
    key_value = pair.split('=')
    q[key_value[0]] = key_value[1];
  end
end

Instance Method Details

#+(query) ⇒ Object



36
37
38
# File 'lib/hal_decorator/pagination.rb', line 36

def +(query)
  self.class.new(@uri, @query.merge(query))
end

#to_sObject



40
41
42
43
44
45
46
# File 'lib/hal_decorator/pagination.rb', line 40

def to_s
  return if @uri.nil?
  @uri.dup.tap do |uri|
    next if @query.nil? || @query.empty?
    uri << "?" + @query.map { |k,v| "#{k}=#{v}" }.join('&')
  end
end