Class: TicketingHub::Collection

Inherits:
Enumerator::Generator
  • Object
show all
Defined in:
lib/ticketing_hub/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, path, options = {}) ⇒ Collection

Returns a new instance of Collection.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ticketing_hub/collection.rb', line 6

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']
      response = client.request(:get, next_url, options)
      response.body.each { |value| yielder << value }
    end
  end
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



4
5
6
# File 'lib/ticketing_hub/collection.rb', line 4

def client
  @client
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/ticketing_hub/collection.rb', line 4

def options
  @options
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/ticketing_hub/collection.rb', line 4

def path
  @path
end

Instance Method Details

#find(id = nil, options = {}, &block) ⇒ Object



23
24
25
26
# File 'lib/ticketing_hub/collection.rb', line 23

def find(id=nil, options = {}, &block)
  return super(&block) if block_given?
  client.request(:get, "#{path}/#{id}", options).body
end


28
29
30
31
32
33
34
35
# File 'lib/ticketing_hub/collection.rb', line 28

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