Class: Barchart::Resource

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

Direct Known Subclasses

History, Quote

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Resource

Returns a new instance of Resource.



12
13
14
15
# File 'lib/barchart/resources/resource.rb', line 12

def initialize(response)
  @response_json = response
  @struct = RecursiveOpenStruct.new(@response_json, {recurse_over_arrays: true})
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



21
22
23
# File 'lib/barchart/resources/resource.rb', line 21

def method_missing(name, *args)
  @struct[name.to_sym]
end

Instance Attribute Details

#response_jsonObject (readonly)

Returns the value of attribute response_json.



10
11
12
# File 'lib/barchart/resources/resource.rb', line 10

def response_json
  @response_json
end

#structObject (readonly)

Returns the value of attribute struct.



10
11
12
# File 'lib/barchart/resources/resource.rb', line 10

def struct
  @struct
end

Class Method Details

.initialize_from_array_response(resource_class, response) ⇒ Object



4
5
6
7
8
# File 'lib/barchart/resources/resource.rb', line 4

def self.initialize_from_array_response(resource_class, response)
  JSON.parse(response.body).map do |resource_json|
    resource_class.new(resource_json)
  end
end

Instance Method Details

#as_json(options = {}) ⇒ Object



17
18
19
# File 'lib/barchart/resources/resource.rb', line 17

def as_json(options = {})
  @response_json
end

#inspectObject



25
26
27
# File 'lib/barchart/resources/resource.rb', line 25

def inspect
  @struct.inspect.gsub(/#<RecursiveOpenStruct/,"#<#{self.class.name}")
end