Sha256: 15efb73a65d3dde15db8b493c9f8a1f3bf344344d9a422d0384da6b6a8905fab

Contents?: true

Size: 865 Bytes

Versions: 1

Compression:

Stored size: 865 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"]
          }
        }.with_indifferent_access
      end

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

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

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
groovehq-1.0.5 lib/groovehq/resource_collection.rb