Class: Stacktor::Core::Resource

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, client, opts = {}) ⇒ Resource

Returns a new instance of Resource.



9
10
11
12
# File 'lib/stacktor/core/resource.rb', line 9

def initialize(data, client, opts={})
  @client = client
  self.handle_data(data, opts)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



34
35
36
# File 'lib/stacktor/core/resource.rb', line 34

def method_missing(method, *args, &block)
  return self[method]
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



7
8
9
# File 'lib/stacktor/core/resource.rb', line 7

def client
  @client
end

#dataObject

Returns the value of attribute data.



7
8
9
# File 'lib/stacktor/core/resource.rb', line 7

def data
  @data
end

Instance Method Details

#[](field) ⇒ Object



23
24
25
# File 'lib/stacktor/core/resource.rb', line 23

def [](field)
  @data[field.to_s] || @options[:headers][field.to_s]
end

#[]=(field, val) ⇒ Object



26
27
28
# File 'lib/stacktor/core/resource.rb', line 26

def []=(field,val)
  @data[field.to_s]=val
end

#handle_data(data, opts = {}) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/stacktor/core/resource.rb', line 14

def handle_data(data, opts={})
  if data.is_a?(String)
    data = JSON.parse(data)
  end
  @data = data || {}
  @options = opts
  @options[:headers] ||= {}
end

#headersObject



30
31
32
# File 'lib/stacktor/core/resource.rb', line 30

def headers
  @options[:headers]
end