Sha256: 3de4239b4bddacc617b8c2097ce6ce78056681dc093936f5a96584155872d91e

Contents?: true

Size: 859 Bytes

Versions: 2

Compression:

Stored size: 859 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) { ActiveSupport::HashWithIndifferentAccess.new }
      links[:self] = { href: 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.4 lib/groovehq/resource.rb
groovehq-1.0.3 lib/groovehq/resource.rb