class GrooveHQ::ResourceCollection

Attributes

options[R]

Public Class Methods

new(client, data, options = {}) click to toggle source
# 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

  meta_data = data.delete(:meta) { Hash.new }

  collection = Array(data.values.first).map do |item|
    Resource.new(client, item)
  end

  links = {}

  if meta_data.has_key?("pagination")
    @total_pages = meta_data["pagination"]["total_pages"]
    @current_page = meta_data["pagination"]["current_page"]
    links = {
      next: {
        href: meta_data["pagination"]["next_page"]
      },
      prev: {
        href: meta_data["pagination"]["prev_page"]
      },
    }
  end

  @data = OpenStruct.new(meta: meta_data, collection: collection)
  @rels = parse_links(links)
  @options = options.with_indifferent_access
end

Public Instance Methods

each() { |item| ... } click to toggle source
# 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