Class: HALPresenter::Pagination::Uri
- Inherits:
-
Object
- Object
- HALPresenter::Pagination::Uri
- Defined in:
- lib/hal_presenter/pagination.rb
Class Method Summary collapse
Instance Method Summary collapse
- #+(query) ⇒ Object
-
#initialize(uri, query) ⇒ Uri
constructor
A new instance of Uri.
- #to_s ⇒ Object
Constructor Details
#initialize(uri, query) ⇒ Uri
Returns a new instance of Uri.
29 30 31 32 |
# File 'lib/hal_presenter/pagination.rb', line 29 def initialize(uri, query) @uri = uri @query = query end |
Class Method Details
.parse(str) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/hal_presenter/pagination.rb', line 9 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
21 22 23 24 25 26 27 |
# File 'lib/hal_presenter/pagination.rb', line 21 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
34 35 36 |
# File 'lib/hal_presenter/pagination.rb', line 34 def +(query) self.class.new(@uri, @query.merge(query)) end |
#to_s ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/hal_presenter/pagination.rb', line 38 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 |