Class: Figo::HTTPS

Inherits:
Net::HTTP::Persistent
  • Object
show all
Defined in:
lib/helpers/https.rb

Overview

HTTPS class with certificate authentication and enhanced error handling.

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, proxy = nil) ⇒ HTTPS

Overwrite ‘initialize` method from `Net::HTTP::Persistent`.



6
7
8
# File 'lib/helpers/https.rb', line 6

def initialize(name = nil, proxy = nil)
  super(name: name, proxy: proxy)
end

Instance Method Details

#request(uri, req = nil, &block) ⇒ Object

Overwrite ‘request` method from `Net::HTTP::Persistent`.

Raise error when a REST API error is returned.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/helpers/https.rb', line 13

def request(uri, req = nil, &block)
  response = super(uri, req, &block)

  # Evaluate HTTP response.
  case response
    when Net::HTTPSuccess
      return response
    when Net::HTTPBadRequest
      hash = JSON.parse(response.body)
      raise Error.new(hash["error"], hash["error"]["description"])
    when Net::HTTPUnauthorized
      raise Error.new("unauthorized", "Missing, invalid or expired access token.")
    when Net::HTTPForbidden
      raise Error.new("forbidden", "Insufficient permission.")
    when Net::HTTPNotFound
      return nil
    when Net::HTTPMethodNotAllowed
      raise Error.new("method_not_allowed", "Unexpected request method.")
    when Net::HTTPServiceUnavailable
      raise Error.new("service_unavailable", "Exceeded rate limit.")
    else
      raise Error.new("internal_server_error", "We are very sorry, but something went wrong.")
  end
end