Class: TicketingHub::Collection
- Inherits:
-
Enumerator::Generator
- Object
- Enumerator::Generator
- TicketingHub::Collection
- Defined in:
- lib/ticketing_hub/collection.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#options ⇒ Object
Returns the value of attribute options.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #find(id = nil, options = {}, &block) ⇒ Object
-
#initialize(client, path, options = {}) ⇒ Collection
constructor
A new instance of Collection.
- #links(response) ⇒ Object
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, = {}) self.client = client self.path = path self. = super() do |yielder| response = client.request(:get, path, ) 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, ) response.body.each { |value| yielder << value } end end end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
4 5 6 |
# File 'lib/ticketing_hub/collection.rb', line 4 def client @client end |
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/ticketing_hub/collection.rb', line 4 def @options end |
#path ⇒ Object
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, = {}, &block) return super(&block) if block_given? client.request(:get, "#{path}/#{id}", ).body end |
#links(response) ⇒ Object
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 |