Class: GrooveHQ::Resource
- Inherits:
-
Object
- Object
- GrooveHQ::Resource
- Defined in:
- lib/groovehq/resource.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#rels ⇒ Object
readonly
Returns the value of attribute rels.
Instance Method Summary collapse
-
#initialize(client, data) ⇒ Resource
constructor
A new instance of Resource.
- #method_missing(method_sym, *arguments, &block) ⇒ Object
- #parse_links(links) ⇒ Object
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
4 5 6 |
# File 'lib/groovehq/resource.rb', line 4 def data @data end |
#rels ⇒ Object (readonly)
Returns the value of attribute rels.
4 5 6 |
# File 'lib/groovehq/resource.rb', line 4 def rels @rels end |
Instance Method Details
#parse_links(links) ⇒ Object
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 |