Class: Typhoeus::Response

Inherits:
Object
  • Object
show all
Includes:
CurlHelper
Defined in:
lib/labclient/http.rb

Overview

Add Data : OJ Parsing

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CurlHelper

#curl

Instance Attribute Details

#client ⇒ Object (readonly)

Returns the value of attribute client.



72
73
74
# File 'lib/labclient/http.rb', line 72

def client
  @client
end

#path ⇒ Object (readonly)

Returns the value of attribute path.



72
73
74
# File 'lib/labclient/http.rb', line 72

def path
  @path
end

Instance Method Details

#data ⇒ Object



74
75
76
77
78
# File 'lib/labclient/http.rb', line 74

def data
  @data ||= process_body

  @data
end

#find_friendly_error ⇒ Object

Print Error information

  1. Use Typheous return_message if there isn't any return body For network/uri/dns related issues
  2. Use body for parsed responses For Bad Request, invalid params
  3. Return raw data For non body responses


109
110
111
112
113
114
115
116
117
118
# File 'lib/labclient/http.rb', line 109

def find_friendly_error
  case data
  when nil
    return_message
  when LabClient::LabStruct
    data[:message] || data[:error]
  else # Handle String as well
    data
  end
end

#friendly_error ⇒ Object



120
121
122
# File 'lib/labclient/http.rb', line 120

def friendly_error
  "#{code} - #{find_friendly_error}"
end

#inspect ⇒ Object



68
69
70
# File 'lib/labclient/http.rb', line 68

def inspect
  "#<TyphoeusResponse code: #{code}>"
end

#process_body ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/labclient/http.rb', line 85

def process_body
  if body.empty?
    nil
  elsif headers['content-type']&.include? 'text/plain'
    body
  else
    result = Oj.load(body, mode: :compat, symbol_keys: true, object_class: LabClient::LabStruct)
    result.instance_variable_set(:@response, self) if result.instance_of?(LabClient::LabStruct)
    result
  end
end

#response ⇒ Object

Shim for CurlHelper



81
82
83
# File 'lib/labclient/http.rb', line 81

def response
  self
end

#retry? ⇒ Boolean

Retry Helper Accessor

Returns:

  • (Boolean)


98
99
100
# File 'lib/labclient/http.rb', line 98

def retry?
  code == 429
end