Class: Pagaris::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pagaris/client.rb

Overview

In charge of the HTTP calls to the API

Constant Summary collapse

DOMAIN =
ENV.fetch('PAGARIS_DOMAIN', 'https://pagaris.com')
API_PREFIX =
'/api/v1/'
BASE_URL =
DOMAIN + API_PREFIX

Class Method Summary collapse

Class Method Details

.get(path) ⇒ Hash

Makes a GET HTTP request to the given ‘path`

Examples:

Client.get('orders') # => { orders: [{ id: '1234', amount: 1234.56}] }
Client.get('orders/1234') # => { id: '1234', amount: 1234.56 }

Parameters:

  • path (String)

    path that goes after the base url

Returns:

  • (Hash)

    Response body as a Hash with symbolized names

Raises:

  • (Error::HttpBaseError)

    Any of the subclasses of ‘HttpBaseError`, depending on the response status code



27
28
29
30
31
# File 'lib/pagaris/client.rb', line 27

def self.get(path)
  response = HTTParty.get(BASE_URL + path, request_options(path, 'GET'))
  throw_error(response) unless [200, 201].include?(response.code)
  JSON.parse(response.body, symbolize_names: true)
end

.post(path, body = nil) ⇒ Hash

Makes a POST HTTP request to the given ‘path`, with the given `body`

Examples:

Client.post('orders', { amount: 5432.10 })
# => { id: '4321', amount: 5432.10 }

Parameters:

  • path (String)

    path that goes after the base url

  • body (Hash) (defaults to: nil)

    Body to send on the request. Will be converted to JSON directly in this class.

Returns:

  • (Hash)

    Response body as a Hash with symbolized names

Raises:

  • (Error::HttpBaseError)

    Any of the subclasses of ‘HttpBaseError`, depending on the response status code



49
50
51
52
53
54
55
56
# File 'lib/pagaris/client.rb', line 49

def self.post(path, body = nil)
  response = HTTParty.post(
    BASE_URL + path,
    request_options(path, 'POST', body)
  )
  throw_error(response) unless [200, 201].include?(response.code)
  JSON.parse(response.body, symbolize_names: true)
end

.put(path, body = nil) ⇒ Hash

Makes a PUT HTTP request to the given ‘path`, with the given `body`

Examples:

Client.put('orders/1234/confirm')
# => { id: '1234', amount: 5432.10 }

Parameters:

  • path (String)

    path that goes after the base url

  • body (Hash) (defaults to: nil)

    Body to send on the request. Will be converted to JSON directly in this class.

Returns:

  • (Hash)

    Response body as a Hash with symbolized names

Raises:

  • (Error::HttpBaseError)

    Any of the subclasses of ‘HttpBaseError`, depending on the response status code



74
75
76
77
78
79
80
81
# File 'lib/pagaris/client.rb', line 74

def self.put(path, body = nil)
  response = HTTParty.put(
    BASE_URL + path,
    request_options(path, 'PUT', body)
  )
  throw_error(response) unless [200, 201].include?(response.code)
  JSON.parse(response.body, symbolize_names: true)
end