Class: GrooveHQ::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/groovehq/resource.rb

Direct Known Subclasses

ResourceCollection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, data) ⇒ Resource

Returns a new instance of Resource.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/groovehq/resource.rb', line 6

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/groovehq/resource.rb', line 27

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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/groovehq/resource.rb', line 4

def data
  @data
end

#relsObject (readonly)

Returns the value of attribute rels.



4
5
6
# File 'lib/groovehq/resource.rb', line 4

def rels
  @rels
end

Instance Method Details



21
22
23
24
25
# File 'lib/groovehq/resource.rb', line 21

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