Class: Esi::Response
- Inherits:
-
Object
- Object
- Esi::Response
- Extended by:
- Forwardable
- Defined in:
- lib/esi/response.rb
Instance Attribute Summary collapse
-
#json ⇒ Object
readonly
Returns the value of attribute json.
-
#original_response ⇒ Object
readonly
Returns the value of attribute original_response.
Instance Method Summary collapse
- #cached_until ⇒ Object
- #data ⇒ Object
-
#initialize(response) ⇒ Response
constructor
A new instance of Response.
- #method_missing(method, *args, &block) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(response) ⇒ Response
Returns a new instance of Response.
11 12 13 14 15 |
# File 'lib/esi/response.rb', line 11 def initialize(response) @original_response = response @json = MultiJson.load(body, symbolize_keys: true) rescue {} @json = normalize_keys(@json) if @json.is_a?(Hash) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/esi/response.rb', line 35 def method_missing(method, *args, &block) begin data.send(method, *args, &block) rescue NameError super(name, *args, &block) end end |
Instance Attribute Details
#json ⇒ Object (readonly)
Returns the value of attribute json.
7 8 9 |
# File 'lib/esi/response.rb', line 7 def json @json end |
#original_response ⇒ Object (readonly)
Returns the value of attribute original_response.
7 8 9 |
# File 'lib/esi/response.rb', line 7 def original_response @original_response end |
Instance Method Details
#cached_until ⇒ Object
31 32 33 |
# File 'lib/esi/response.rb', line 31 def cached_until original_response.headers[:expires] ? Time.parse(original_response.headers[:expires]) : nil end |
#data ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/esi/response.rb', line 17 def data @data ||= begin if @json.is_a?(Array) @json[0].is_a?(Hash) ? @json.map { |e| RecursiveOpenStruct.new(e, recurse_over_arrays: true) } : @json else RecursiveOpenStruct.new(@json, recurse_over_arrays: true) end end end |
#to_s ⇒ Object
27 28 29 |
# File 'lib/esi/response.rb', line 27 def to_s MultiJson.dump(json, pretty: true) end |