Class: PciProxy::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/pci_proxy/base.rb

Direct Known Subclasses

Check, Token

Constant Summary collapse

JSON_UTF8_CONTENT_TYPE =
'application/json; charset=UTF-8'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_endpointObject (readonly)

Returns the value of attribute api_endpoint.



9
10
11
# File 'lib/pci_proxy/base.rb', line 9

def api_endpoint
  @api_endpoint
end

#api_passwordObject (readonly)

Returns the value of attribute api_password.



9
10
11
# File 'lib/pci_proxy/base.rb', line 9

def api_password
  @api_password
end

#api_usernameObject (readonly)

Returns the value of attribute api_username.



9
10
11
# File 'lib/pci_proxy/base.rb', line 9

def api_username
  @api_username
end

Instance Method Details

#api_get(endpoint: @api_endpoint, params: {}, raise_on_error: true) ⇒ Hash

Perform an API request via HTTP GET

Parameters:

  • +endpoint+ (String)

    (Optional) - the API endpoint to hit - defaults to the value of the api_endpoint reader

  • +params+ (Hash)

    (Optional) - any URL parameters to supply to the API call

Returns:

  • (Hash)

    parsed JSON response

Raises:

  • (PciProxyAPIError)

    in cases where the API responds with a non-200 response code



29
30
31
32
33
34
35
36
37
# File 'lib/pci_proxy/base.rb', line 29

def api_get(endpoint: @api_endpoint, params: {}, raise_on_error: true)
  response = client.get(endpoint, params)

  if raise_on_error == false || response.status == HTTP_OK_CODE
    return MultiJson.load(response.body)
  end

  raise error_class(response), "HTTP status: #{response.status}, Response: #{response.body}"
end

#api_post(endpoint: @api_endpoint, body: {}, raise_on_error: true) ⇒ Hash

Perform an API request via HTTP POST

Parameters:

  • +endpoint+ (String)

    (Optional) - the API endpoint to hit - defaults to the value of the api_endpoint reader

  • +body+ (Hash)

    (Optional) - the API request payload (will be converted to JSON)

Returns:

  • (Hash)

    parsed JSON response

Raises:

  • (PciProxyAPIError)

    in cases where the API responds with a non-200 response code



47
48
49
50
51
52
53
54
55
# File 'lib/pci_proxy/base.rb', line 47

def api_post(endpoint: @api_endpoint, body: {}, raise_on_error: true)
  response = client.post(endpoint, MultiJson.dump(body), "Content-Type" => JSON_UTF8_CONTENT_TYPE)

  if raise_on_error == false || response.status == HTTP_OK_CODE
    return MultiJson.load(response.body)
  end

  raise error_class(response), "HTTP status: #{response.status}, Response: #{response.body}"
end

#clientObject

Create and memoise a Faraday client for this API client



13
14
15
16
17
18
19
# File 'lib/pci_proxy/base.rb', line 13

def client
  @client ||= Faraday.new(@api_endpoint) do |client|
    client.request :url_encoded
    client.adapter Faraday.default_adapter
    client.basic_auth(@api_username, @api_password)
  end
end

#error_class(response) ⇒ StandardError

Fetch the error klass appropriate for the given Faraday response

Parameters:

  • +response+ (Response)
    • the Faraday response object

Returns:

  • (StandardError)
    • the StandardError subclass appropriate to the response



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pci_proxy/base.rb', line 62

def error_class(response)
  case response.status
  when HTTP_BAD_REQUEST_CODE
    BadRequestError
  when HTTP_UNAUTHORIZED_CODE
    UnauthorizedError
  when HTTP_FORBIDDEN_CODE
    ForbiddenError
  when HTTP_NOT_FOUND_CODE
    NotFoundError
  when HTTP_UNPROCESSABLE_ENTITY_CODE
    UnprocessableEntityError
  else
    PciProxyAPIError
  end
end