class Ecommerce::Resources::Collection
A wrapper to Ecommerce
collection returns from API. This wrapper represents a collection and it’s responsible for processing pagination information as well.
Constants
- PAGE_REGEX
Attributes
collection[R]
headers[R]
response[R]
Public Class Methods
build(response)
click to toggle source
# File lib/ecommerce/resources/collection.rb, line 17 def self.build(response) self.new(response).build end
new(response)
click to toggle source
# File lib/ecommerce/resources/collection.rb, line 11 def initialize(response) @response = response @collection = [] @headers = response.headers['Link'].split(',') if response.headers['Link'] end
Public Instance Methods
build()
click to toggle source
# File lib/ecommerce/resources/collection.rb, line 21 def build build_collection self end
first_page()
click to toggle source
# File lib/ecommerce/resources/collection.rb, line 38 def first_page page_for(:first) end
last_page()
click to toggle source
# File lib/ecommerce/resources/collection.rb, line 30 def last_page page_for(:last) end
next_page()
click to toggle source
# File lib/ecommerce/resources/collection.rb, line 26 def next_page page_for(:next) end
previous_page()
click to toggle source
# File lib/ecommerce/resources/collection.rb, line 34 def previous_page page_for(:prev) end
Private Instance Methods
build_collection()
click to toggle source
# File lib/ecommerce/resources/collection.rb, line 52 def build_collection raise NotImplementedError end
header_link_for(rel)
click to toggle source
# File lib/ecommerce/resources/collection.rb, line 48 def header_link_for(rel) headers.select{|n| n =~ /rel=#{rel}/}.first end
page_for(page_rel)
click to toggle source
# File lib/ecommerce/resources/collection.rb, line 44 def page_for(page_rel) header_link_for(page_rel).match(PAGE_REGEX)[1].to_i rescue nil end