Class: Barchart::Resource
- Inherits:
-
Object
- Object
- Barchart::Resource
- Defined in:
- lib/barchart/resources/resource.rb
Instance Attribute Summary collapse
-
#response_json ⇒ Object
readonly
Returns the value of attribute response_json.
-
#struct ⇒ Object
readonly
Returns the value of attribute struct.
Class Method Summary collapse
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
-
#initialize(response) ⇒ Resource
constructor
A new instance of Resource.
- #inspect ⇒ Object
- #method_missing(name, *args) ⇒ Object
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_json ⇒ Object (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 |
#struct ⇒ Object (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( = {}) @response_json end |
#inspect ⇒ Object
25 26 27 |
# File 'lib/barchart/resources/resource.rb', line 25 def inspect @struct.inspect.gsub(/#<RecursiveOpenStruct/,"#<#{self.class.name}") end |