Class: CleverSDK::Api::Response

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(faraday_response) ⇒ Response

Returns a new instance of Response.



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

def initialize(faraday_response)
  @_response = faraday_response
end

Class Method Details

.handle(faraday_response) ⇒ Object



6
7
8
9
10
# File 'lib/clever_sdk/api/response.rb', line 6

def self.handle(faraday_response)
  return CleverSDK::Api::Response.new(faraday_response) if faraday_response.success?

  raise CleverSDK::Error.handle(faraday_response)
end

.status_code_explanationsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/clever_sdk/api/response.rb', line 12

def self.status_code_explanations
  # from https://dev.clever.com/reference/responses-errors
  @status_code_explanations ||= {
    200	=> {meaning: "Success", action_to_take:	"Process the response."},
    400	=> {meaning: "Bad request",	action_to_take: "Check your API token and request body. Do not retry request. The troubleshooting pages for each API also have tips for what to look for!"},
    401	=> {meaning: "Invalid auth", action_to_take:	"Check your API token and request body. Do not retry request. The troubleshooting pages for each API also have tips for what to look for!"},
    404	=> {meaning: "Resource not available", action_to_take: "Check your API token and request URL - you may be using the wrong token or the resource may have been unshared with your application or deleted."},
    413	=> {meaning: "Request entity too large", action_to_take: "Your limit parameter may be over 10000; reduce accordingly."},
    429	=> {meaning: "Rate limit exceeded", action_to_take: "Try again after rate limit period expires."},
    500 => {meaning: "Clever API failure", action_to_take: "Wait and retry request. If you see these regularly, reach out to [email protected]."},
    502 => {meaning: "Clever API failure", action_to_take: "Wait and retry request. If you see these regularly, reach out to [email protected]."},
    503 => {meaning: "Clever API failure", action_to_take: "Wait and retry request. If you see these regularly, reach out to [email protected]."},
    501	=> {meaning: "Not implemented", action_to_take: "Your client is attempting an unsupported request. Try an alternative request. Do not retry request."}
  }
end

Instance Method Details

#access_tokenObject



53
54
55
56
# File 'lib/clever_sdk/api/response.rb', line 53

def access_token
  # Dig the access token out of the response object.
  @_response.env.request_headers["Authorization"].split(" ").last
end

#bodyObject



48
49
50
51
# File 'lib/clever_sdk/api/response.rb', line 48

def body
  # Any response that does not return JSON is handled as an error.
  @body ||= JSON.parse(@_response.body)
end

#codeObject



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

def code
  @_response.status
end

#headersObject



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

def headers
  @_response.headers
end

#raw_requestObject



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

def raw_request
  @_response.env.request
end

#raw_responseObject



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

def raw_response
  @_response
end