Class: LHC::Response

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

Overview

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

Defined Under Namespace

Classes: 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.



19
20
21
22
23
# File 'lib/lhc/response.rb', line 19

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.



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

def body_replacement
  @body_replacement
end

#from_cacheObject (readonly) Also known as: from_cache?

Returns the value of attribute from_cache.



11
12
13
# File 'lib/lhc/response.rb', line 11

def from_cache
  @from_cache
end

#requestObject

Returns the value of attribute request.



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

def request
  @request
end

Instance Method Details

#[](key) ⇒ Object



29
30
31
# File 'lib/lhc/response.rb', line 29

def [](key)
  data[key]
end

#bodyObject



33
34
35
# File 'lib/lhc/response.rb', line 33

def body
  body_replacement || raw.body.presence
end

#dataObject



25
26
27
# File 'lib/lhc/response.rb', line 25

def data
  @data ||= body.present? ? LHC::Response::Data.new(self) : nil
end

#formatObject



51
52
53
54
55
# File 'lib/lhc/response.rb', line 51

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

  request.format
end

#scrubbed_optionsObject



57
58
59
60
61
# File 'lib/lhc/response.rb', line 57

def scrubbed_options
  scrubbed_options = options.deep_dup
  scrubbed_options[:effective_url] = LHC::EffectiveUrlScrubber.new(scrubbed_options[:effective_url]).scrubbed
  scrubbed_options
end

#timeObject

Provides response time in seconds



38
39
40
# File 'lib/lhc/response.rb', line 38

def time
  raw.time || 0
end

#time_msObject

Provides response time in milliseconds



43
44
45
# File 'lib/lhc/response.rb', line 43

def time_ms
  time * 1000
end

#timeout?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/lhc/response.rb', line 47

def timeout?
  raw.timed_out?
end