Sha256: e0694d1e2feeab3d888f76422f45cf95dd0b4d5272c2c4d2f29a1e0ea5efada3

Contents?: true

Size: 873 Bytes

Versions: 3

Compression:

Stored size: 873 Bytes

Contents

module GrooveHQ

  class ResourceCollection < Resource
    include Enumerable

    def initialize(client, data)
      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")
        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).with_indifferent_access
    end

    def each(&block)
      collection.each { |item| yield item }
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
groovehq-1.0.4 lib/groovehq/resource_collection.rb
groovehq-1.0.3 lib/groovehq/resource_collection.rb
groovehq-1.0.2 lib/groovehq/resource_collection.rb