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