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, call = nil) ⇒ Response



11
12
13
14
15
# File 'lib/esi/response.rb', line 11

def initialize(response, call=nil)
  @original_response = response
  @call = call
  @data = normalize_results
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



48
49
50
# File 'lib/esi/response.rb', line 48

def method_missing(method, *args, &block)
  data.send(method, *args, &block)
end

Instance Attribute Details

#callObject (readonly)

Returns the value of attribute call.



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

def call
  @call
end

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
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



30
31
32
# File 'lib/esi/response.rb', line 30

def cached_until
  @cached_until ||= headers[:expires] ? Time.parse(headers[:expires]) : nil
end

#merge(other_response) ⇒ Object



17
18
19
20
# File 'lib/esi/response.rb', line 17

def merge(other_response)
  @data += other_response.data
  self
end

#response_jsonObject



34
35
36
# File 'lib/esi/response.rb', line 34

def response_json
  @response_json ||= MultiJson.load(body, symbolize_keys: true) rescue {}
end

#saveObject



38
39
40
41
42
43
44
45
46
# File 'lib/esi/response.rb', line 38

def save
  return if call.nil?
  return if Esi.config.response_log_path.blank? || !Dir.exists?(Esi.config.response_log_path)

  call_name = call.class.to_s.split('::').last.underscore
  folder = Pathname.new(Esi.config.response_log_path).join(call_name)
  FileUtils.mkdir_p(folder)
  File.write(folder.join("#{Time.now.to_i.to_s}.json"), to_json)
end

#to_hObject



22
23
24
# File 'lib/esi/response.rb', line 22

def to_h
  data.is_a?(Array) ? data.map { |e| e.try(:to_h) || e } : e.to_h
end

#to_json(pretty: true) ⇒ Object



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

def to_json(pretty: true)
  MultiJson.dump(data, pretty: pretty)
end