Class: GrooveHQ::ResourceCollection

Inherits:
Resource
  • Object
show all
Includes:
Enumerable
Defined in:
lib/groovehq/resource_collection.rb

Instance Attribute Summary collapse

Attributes inherited from Resource

#data, #rels

Instance Method Summary collapse

Methods inherited from Resource

#method_missing, #parse_links

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, options = {})
  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 = 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

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/groovehq/resource_collection.rb', line 6

def options
  @options
end

Instance Method Details

#eachObject



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.meta,
                         collection: collection + resource_collection.collection)
  @rels = resource_collection.rels
end