Sha256: 8d296572cb0128512c40b43f3b7b06474ed54dd6aeca8f630aacb1740a649f0f

Contents?: true

Size: 987 Bytes

Versions: 2

Compression:

Stored size: 987 Bytes

Contents

module TicketingHub
  class Collection < Enumerator::Generator

    attr_accessor :client, :path, :options

    def initialize(client, path, options = {})
      self.client = client
      self.path = path
      self.options = options

      super() do |yielder|
        response = client.request(:get, path, options)
        if response.body.is_a?(Array)
          response.body.each { |value| yielder << value }
        else yielder << response.body end
        while next_url = links(response)['next']
          client.request(:get, next_url, options).body.each do |value|
            yielder << value
          end
        end
      end
    end

    def find(id, options = {})
      client.request(:get, "#{path}/#{id}", options).body
    end

    def links(response)
      links = ( response.headers["Link"] || "" ).split(', ').map do |link|
        url, type = link.match(/<(.*?)>; rel="(\w+)"/).captures
        [ type, url ]
      end

      Hash[*links.flatten]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ticketinghub-1.0.5 lib/ticketing_hub/collection.rb
ticketinghub-1.0.4 lib/ticketing_hub/collection.rb