Class: Economic::Response
- Inherits:
-
Object
- Object
- Economic::Response
- Defined in:
- lib/economic/response.rb
Defined Under Namespace
Classes: Pagination
Instance Attribute Summary collapse
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#entity ⇒ Object
Returns the value of attribute entity.
-
#meta_data ⇒ Object
Returns the value of attribute meta_data.
-
#pagination ⇒ Object
Returns the value of attribute pagination.
-
#self_link ⇒ Object
Returns the value of attribute self_link.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(self_link:, meta_data:, pagination: nil, collection: nil, entity: nil) ⇒ Response
constructor
A new instance of Response.
Constructor Details
#initialize(self_link:, meta_data:, pagination: nil, collection: nil, entity: nil) ⇒ Response
Returns a new instance of Response.
3 4 5 6 7 8 9 |
# File 'lib/economic/response.rb', line 3 def initialize(self_link:, meta_data:, pagination: nil, collection: nil, entity: nil) @self_link = self_link @meta_data = @pagination = pagination @collection = collection @entity = entity end |
Instance Attribute Details
#collection ⇒ Object
Returns the value of attribute collection.
11 12 13 |
# File 'lib/economic/response.rb', line 11 def collection @collection end |
#entity ⇒ Object
Returns the value of attribute entity.
11 12 13 |
# File 'lib/economic/response.rb', line 11 def entity @entity end |
#meta_data ⇒ Object
Returns the value of attribute meta_data.
11 12 13 |
# File 'lib/economic/response.rb', line 11 def @meta_data end |
#pagination ⇒ Object
Returns the value of attribute pagination.
11 12 13 |
# File 'lib/economic/response.rb', line 11 def pagination @pagination end |
#self_link ⇒ Object
Returns the value of attribute self_link.
11 12 13 |
# File 'lib/economic/response.rb', line 11 def self_link @self_link end |
Class Method Details
.from_json(json) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/economic/response.rb', line 14 def from_json(json) parsed = JSON.parse(json) if parsed.key?("collection") endpoint = parsed["self"][30..].split("?").first model_class = model_class_from_endpoint(endpoint) collection = parsed["collection"].map do |hash| # This method is very heavy. It takes about 2 seconds to run test/resources/customer_resource_test.rb:9, which instantiates 3684 customers # Not instantiating them results in a runtime of 0.056 seconds. model_class.from_hash(hash) end new( collection: collection, meta_data: parsed["metaData"], self_link: parsed["self"], pagination: Economic::Response::Pagination.from_hash(parsed["pagination"]) ) else # find model class endpoint = parsed["self"][30..].split("/")[0..-2].join("/") model_class = model_class_from_endpoint(endpoint) new( entity: model_class.from_json(json), self_link: parsed["self"], meta_data: parsed["metaData"] ) end end |
.model_class_from_endpoint(endpoint) ⇒ Object
46 47 48 |
# File 'lib/economic/response.rb', line 46 def model_class_from_endpoint(endpoint) "Economic::Models::#{endpoint.underscore.tr("0-9", "").gsub("//", "/").classify}".constantize end |