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

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#dataObject



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

def data
  @data ||= process_body

  @data
end

#find_friendly_errorObject

Print Error information

  1. Use Typheous ‘return_message` if there isn’t any return body

For network/uri/dns related issues

  1. Use body for parsed responses

For Bad Request, invalid params

  1. Return raw data

For non body responses



119
120
121
122
123
124
125
126
127
128
# File 'lib/labclient/http.rb', line 119

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_errorObject



130
131
132
# File 'lib/labclient/http.rb', line 130

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

#inspectObject



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

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

#json_body?Boolean

Check if response body can be parsed

Returns:

  • (Boolean)


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

def json_body?
  content_type = headers['content-type']

  return false unless content_type
  return true if content_type.include? 'application/json'

  false
end

#process_bodyObject



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/labclient/http.rb', line 95

def process_body
  return nil if body.empty?

  if json_body?
    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
  else
    body
  end
end

#responseObject

Shim for CurlHelper



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

def response
  self
end

#retry?Boolean

Retry Helper Accessor

Returns:

  • (Boolean)


108
109
110
# File 'lib/labclient/http.rb', line 108

def retry?
  code == 429
end