Class: Helium::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/helium/metadata.rb

Overview

TODO make Metadata inherit from Resource and implement method_missing for all resources to automatically generate methods for attributes rather than whitelisting them with hardcoding

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Metadata

Returns a new instance of Metadata.



6
7
8
9
10
11
# File 'lib/helium/metadata.rb', line 6

def initialize(opts = {})
  @client = opts.fetch(:client)
  @klass  = opts.fetch(:klass)

  @params = fetch_params
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



25
26
27
# File 'lib/helium/metadata.rb', line 25

def method_missing(method_name, *arguments, &block)
  properties[method_name.to_s] || super
end

Instance Method Details

#idObject



13
14
15
# File 'lib/helium/metadata.rb', line 13

def id
  @klass.id
end

#inspectObject



21
22
23
# File 'lib/helium/metadata.rb', line 21

def inspect
  "<Helium::Metadata properties=#{properties}>"
end

#propertiesObject



17
18
19
# File 'lib/helium/metadata.rb', line 17

def properties
  @params["attributes"]
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/helium/metadata.rb', line 29

def respond_to_missing?(method_name, include_private = false)
  properties[method_name.to_s] || super
end

#update(attributes = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/helium/metadata.rb', line 33

def update(attributes = {})
  body = {
    data: {
      attributes: attributes,
      id: id,
      type: "metadata"
    }
  }

  response = @client.patch(path, body: body)
  @params = JSON.parse(response.body)["data"]
  return self
end