Sha256: 642fdc740a700e310165c999fecccf291e47a632adacb3ee681b56cb0a619db0

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

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 = {}, &block)
      super(&block) if block_given?
      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

1 entries across 1 versions & 1 rubygems

Version Path
ticketinghub-1.0.6 lib/ticketing_hub/collection.rb