Class: Esi::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/esi/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#jsonObject (readonly)

Returns the value of attribute json.



7
8
9
# File 'lib/esi/response.rb', line 7

def json
  @json
end

#original_responseObject (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_untilObject



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

#dataObject



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_sObject



27
28
29
# File 'lib/esi/response.rb', line 27

def to_s
  MultiJson.dump(json, pretty: true)
end