Class: DHC::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/dhc/response.rb

Overview

The response contains the raw response (typhoeus) and provides functionality to access response data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, request, from_cache: false) ⇒ Response

A response is initalized with the underlying raw response (typhoeus in our case) and the associated request.



18
19
20
21
22
# File 'lib/dhc/response.rb', line 18

def initialize(raw, request, from_cache: false)
  self.request = request
  self.raw = raw
  @from_cache = from_cache
end

Instance Attribute Details

#body_replacementObject

Returns the value of attribute body_replacement.



9
10
11
# File 'lib/dhc/response.rb', line 9

def body_replacement
  @body_replacement
end

#from_cacheObject (readonly) Also known as: from_cache?

Returns the value of attribute from_cache.



10
11
12
# File 'lib/dhc/response.rb', line 10

def from_cache
  @from_cache
end

#requestObject

Returns the value of attribute request.



9
10
11
# File 'lib/dhc/response.rb', line 9

def request
  @request
end

Instance Method Details

#[](key) ⇒ Object



28
29
30
# File 'lib/dhc/response.rb', line 28

def [](key)
  data[key]
end

#bodyObject



32
33
34
# File 'lib/dhc/response.rb', line 32

def body
  body_replacement || raw.body.presence
end

#dataObject



24
25
26
# File 'lib/dhc/response.rb', line 24

def data
  @data ||= body.present? ? JSON.parse(body) : nil
end

#formatObject



50
51
52
53
# File 'lib/dhc/response.rb', line 50

def format
  return DHC::Formats::JSON.new if request.nil?
  request.format
end

#timeObject

Provides response time in seconds



37
38
39
# File 'lib/dhc/response.rb', line 37

def time
  raw.time || 0
end

#time_msObject

Provides response time in milliseconds



42
43
44
# File 'lib/dhc/response.rb', line 42

def time_ms
  time * 1000
end

#timeout?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/dhc/response.rb', line 46

def timeout?
  raw.timed_out?
end