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

Returns a new instance of Response.



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



52
53
54
# File 'lib/esi/response.rb', line 52

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

#log_directoryObject



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

def log_directory
  call.class.to_s.split('::').last.underscore
  dir = Pathname.new(Esi.config.response_log_path).join(call_name)
  FileUtils.mkdir_p(dir)
  dir
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

#saveObject



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

def save
  File.write(log_directroy.join("#{Time.now.to_i}.json"), to_json) if should_log_response?
  self
end

#should_log_response?Boolean

Returns:

  • (Boolean)


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

def should_log_response?
  return false if call.nil?
  return false if Esi.config.response_log_path.blank? || !Dir.exist?(Esi.config.response_log_path)
  true
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