Class: GrooveHQ::ResourceCollection
- Includes:
- Enumerable
- Defined in:
- lib/groovehq/resource_collection.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Attributes inherited from Resource
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(client, data, options = {}) ⇒ ResourceCollection
constructor
A new instance of ResourceCollection.
Methods inherited from Resource
Constructor Details
#initialize(client, data, options = {}) ⇒ ResourceCollection
Returns a new instance of ResourceCollection.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/groovehq/resource_collection.rb', line 8 def initialize(client, data, = {}) data = {} unless data.is_a?(Hash) data = data.with_indifferent_access @client = client = data.delete(:meta) { Hash.new } collection = Array(data.values.first).map do |item| Resource.new(client, item) end links = {} if .has_key?("pagination") @total_pages = ["pagination"]["total_pages"] @current_page = ["pagination"]["current_page"] links = { next: { href: ["pagination"]["next_page"] }, prev: { href: ["pagination"]["prev_page"] }, } end @data = OpenStruct.new(meta: , collection: collection) @rels = parse_links(links) @options = .with_indifferent_access end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class GrooveHQ::Resource
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/groovehq/resource_collection.rb', line 6 def @options end |
Instance Method Details
#each ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/groovehq/resource_collection.rb', line 40 def each return enum_for(:each) unless block_given? collection.each { |item| yield item } return self if @current_page == @total_pages rel = @rels[:next] or return self resource_collection = rel.get(@options.except(:page)) resource_collection.each(&Proc.new) @data = OpenStruct.new(meta: resource_collection., collection: collection + resource_collection.collection) @rels = resource_collection.rels end |