Sha256: 43a85f75a425a25e0191cd6b523b287c8e970f3a896bdb1f92f14172794aaa03

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

module Ecommerce
  module Resources
    #
    # A wrapper to Ecommerce collection returns from API. This wrapper represents a collection and it's responsible for processing pagination information as well.
    #
    class Collection < Base
      PAGE_REGEX = /page=(\d+)/

      attr_reader :response, :collection, :headers

      def initialize(response)
        @response = response
        @collection = []
        @headers = response.headers['Link'].split(',') if response.headers['Link']
      end

      def self.build(response)
        self.new(response).build
      end

      def build
        build_collection
        self
      end

      def next_page
        page_for(:next)
      end

      def last_page
        page_for(:last)
      end

      def previous_page
        page_for(:prev)
      end

      def first_page
        page_for(:first)
      end

      private

      def page_for(page_rel)
        header_link_for(page_rel).match(PAGE_REGEX)[1].to_i rescue nil
      end

      def header_link_for(rel)
        headers.select{|n| n =~ /rel=#{rel}/}.first
      end

      def build_collection
        raise NotImplementedError
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ecommerce-client-0.0.5 lib/ecommerce/resources/collection.rb
ecommerce-client-0.0.4 lib/ecommerce/resources/collection.rb