Sha256: 885961c0cef898a8d2819b8b40c51b6919c6ae5d7182b107d2604a1f8f859d7a

Contents?: true

Size: 814 Bytes

Versions: 2

Compression:

Stored size: 814 Bytes

Contents

module GrooveHQ

  class Resource
    attr_reader :rels, :data

    def initialize(client, data)
      data = {} unless data.is_a?(Hash)

      @client = client

      data = data.with_indifferent_access

      links        = data.delete(:links) { Hash.new }
      links[:self] = data.delete(:href) if data.has_key?(:href)

      @data   = OpenStruct.new(data.with_indifferent_access)

      @rels = parse_links(links).with_indifferent_access
    end

    def parse_links(links)
      (links || {}).each_with_object({}) do |(relation, value), result|
        result[relation] = Relation.new(@client, value["href"])
      end
    end

    def method_missing(method_sym, *arguments, &block)
      if @data.respond_to?(method_sym)
        @data.send(method_sym)
      else
        super
      end
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
groovehq-1.0.2 lib/groovehq/resource.rb
groovehq-1.0.0 lib/groovehq/resource.rb