Sha256: cd91c64bff33f487dcfafec02df0b9165a9f6e073209094176f03a074d8a11ee

Contents?: true

Size: 859 Bytes

Versions: 1

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)
    end

    def parse_links(links)
      (links || {}).each_with_object({}) do |(relation, value), result|
        result[relation] = Relation.new(@client, value[:href])
      end.with_indifferent_access
    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

1 entries across 1 versions & 1 rubygems

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