Class: Hubspot::HttpResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/hubspot/http_response.rb

Instance Attribute Summary collapse

Attributes inherited from Object

#result

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Object

#call, #errors, #failure?, #success?

Constructor Details

#initialize(response) ⇒ HttpResponse

Returns a new instance of HttpResponse.



7
8
9
# File 'lib/hubspot/http_response.rb', line 7

def initialize(response)
  @response = response
end

Instance Attribute Details

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/hubspot/http_response.rb', line 5

def response
  @response
end

Class Method Details

.call(response) ⇒ Object



11
12
13
# File 'lib/hubspot/http_response.rb', line 11

def self.call(response)
  new(response).constructor
end

Instance Method Details

#constructorObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hubspot/http_response.rb', line 15

def constructor
  case response
    when Net::HTTPSuccess
      body = JSON.parse response.body if response.body && response.body.length >= 2
      { 'body': body, 'status': response.code }
    when Net::HTTPUnauthorized
      { 'error' => "#{response.message}: username and password set and correct?", 'status' => response.code }
    when Net::HTTPServerError
      # errors.add(error: response.message,  :status, response.code)
      { 'error' => "#{response.message}: try again later?", 'status' => response.code }
    else
      # errors.add(error: response.message,  :status, response.code)
      { 'error' => response.message, 'status' => response.code }
  end
end