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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, request) ⇒ Response

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



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

def initialize(raw, request)
  self.request = request
  self.raw = raw
end

Instance Attribute Details

#requestObject

Returns the value of attribute request.



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

def request
  @request
end

Instance Method Details

#bodyObject



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

def body
  raw.body
end

#codeObject



36
37
38
# File 'lib/lhc/response.rb', line 36

def code
  raw.code
end

#dataObject

Access response data. Cache parsing.



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

def data
  @data ||= case format
    when :json
      JSON.parse(raw.body, object_class: OpenStruct)
    else # default is json
      JSON.parse(raw.body, object_class: OpenStruct)
  end
  @data
end

#effective_urlObject



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

def effective_url
  raw.effective_url
end

#headersObject



40
41
42
# File 'lib/lhc/response.rb', line 40

def headers
  raw.headers
end

#optionsObject



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

def options
  raw.options
end

#success?Boolean

Returns:

  • (Boolean)


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

def success?
  raw.success?
end

#timeObject

Provides response time in ms.



49
50
51
# File 'lib/lhc/response.rb', line 49

def time
  (raw.time || 0) * 1000
end

#timeout?Boolean

Returns:

  • (Boolean)


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

def timeout?
  raw.timed_out?
end