Class: Zspay::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/zspay/resource.rb

Overview

The Resource class provides methods to perform HTTP requests to the Zspay API. This class is designed to be a base class from which other resource-specific classes can inherit.

Direct Known Subclasses

BankAccount, Client, Establishment, Sale, Transfer

Class Method Summary collapse

Class Method Details

.delete(path, custom_token = nil) ⇒ OpenStruct

Performs a DELETE request to the specified API path.

Parameters:

  • path (String)

    the API path to send the request to.

  • custom_token (String, nil) (defaults to: nil)

    an optional custom token to use for the request.

Returns:

  • (OpenStruct)

    the parsed response.



43
44
45
# File 'lib/zspay/resource.rb', line 43

def delete(path, custom_token = nil)
  req(:delete, path, {}, custom_token)
end

.endpointString (private)

Returns the base endpoint URL from the configuration.

Returns:

  • (String)

    the API endpoint URL.



122
123
124
# File 'lib/zspay/resource.rb', line 122

def endpoint
  Zspay.configuration.endpoint
end

.get(path, custom_token = nil) ⇒ OpenStruct

Performs a GET request to the specified API path.

Parameters:

  • path (String)

    the API path to send the request to.

  • custom_token (String, nil) (defaults to: nil)

    an optional custom token to use for the request.

Returns:

  • (OpenStruct)

    the parsed response.



34
35
36
# File 'lib/zspay/resource.rb', line 34

def get(path, custom_token = nil)
  req(:get, path, {}, custom_token)
end

.headers(custom_token = nil) ⇒ Hash (private)

Constructs the headers for the request. If a custom token is provided, it is used; otherwise, the default token from configuration is used.

Parameters:

  • custom_token (String, nil) (defaults to: nil)

    an optional custom token for the request.

Returns:

  • (Hash)

    the request headers.



131
132
133
134
135
136
# File 'lib/zspay/resource.rb', line 131

def headers(custom_token = nil)
  token = "Bearer #{custom_token || Zspay.configuration.token}"
  {
    Authorization: token
  }
end

.parse_body(response) ⇒ OpenStruct (private)

Parses the response body and returns it as an OpenStruct. If the request was successful, returns the JSON parsed body. Otherwise, logs the error and returns an error message.

Parameters:

  • response (HTTP::Response)

    the HTTP response to parse.

Returns:

  • (OpenStruct)

    the parsed response.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/zspay/resource.rb', line 103

def parse_body(response)
  if success_request?(response)
    body = response.body.to_s
    json = parse_json(body)

    return json if json.is_a? OpenStruct

    OpenStruct.new({ success: false, error: body })
  else
    error_log = Logger.new($stderr)
    error_log.error("Request error to: #{response.uri}\ncode: #{response.code}\nbody: #{response.body}")

    OpenStruct.new({ success: false, message: "An error occurred while making the request" })
  end
end

.parse_json(json) ⇒ OpenStruct, String (private)

Parses a JSON string into an OpenStruct. If parsing fails, returns the original JSON string.

Parameters:

  • json (String)

    the JSON string to parse.

Returns:

  • (OpenStruct, String)

    the parsed JSON or the original string if parsing failed.



150
151
152
153
154
# File 'lib/zspay/resource.rb', line 150

def parse_json(json)
  JSON.parse(json, object_class: OpenStruct)
rescue JSON::ParserError => _e
  json
end

.patch(path, payload, custom_token = nil) ⇒ OpenStruct

Performs a PATCH request to the specified API path.

Parameters:

  • path (String)

    the API path to send the request to.

  • payload (Hash)

    the request payload.

  • custom_token (String, nil) (defaults to: nil)

    an optional custom token to use for the request.

Returns:

  • (OpenStruct)

    the parsed response.



25
26
27
# File 'lib/zspay/resource.rb', line 25

def patch(path, payload, custom_token = nil)
  req(:patch, path, payload, custom_token)
end

.post(path, payload = {}, custom_token = nil, body: "json") ⇒ OpenStruct

Performs a POST request to the specified API path.

Parameters:

  • path (String)

    the API path to send the request to.

  • payload (Hash) (defaults to: {})

    the request payload, defaults to an empty hash.

  • custom_token (String, nil) (defaults to: nil)

    an optional custom token to use for the request.

  • body (String) (defaults to: "json")

    the format of the request body, defaults to ‘json’.

Returns:

  • (OpenStruct)

    the parsed response.



15
16
17
# File 'lib/zspay/resource.rb', line 15

def post(path, payload = {}, custom_token = nil, body: "json")
  req(:post, path, payload, custom_token, body: body)
end

.put(path, payload = {}, custom_token = nil) ⇒ OpenStruct

Performs a PUT request to the specified API path.

Parameters:

  • path (String)

    the API path to send the request to.

  • payload (Hash) (defaults to: {})

    the request payload, defaults to an empty hash.

  • custom_token (String, nil) (defaults to: nil)

    an optional custom token to use for the request.

Returns:

  • (OpenStruct)

    the parsed response.



53
54
55
# File 'lib/zspay/resource.rb', line 53

def put(path, payload = {}, custom_token = nil)
  req(:put, path, payload, custom_token)
end

.req(method, path, payload = {}, custom_token = nil, body: "json") ⇒ OpenStruct (private)

A private method to perform an HTTP request using the specified method, path, and payload.

Parameters:

  • method (Symbol)

    the HTTP method to use for the request.

  • path (String)

    the API path to send the request to.

  • payload (Hash) (defaults to: {})

    the request payload, defaults to an empty hash.

  • custom_token (String, nil) (defaults to: nil)

    an optional custom token to use for the request.

  • body (String) (defaults to: "json")

    the format of the request body, defaults to ‘json’.

Returns:

  • (OpenStruct)

    the parsed response.



67
68
69
# File 'lib/zspay/resource.rb', line 67

def req(method, path, payload = {}, custom_token = nil, body: "json")
  send("req_#{body}", method, path, payload, custom_token)
end

.req_form(method, path, payload, custom_token) ⇒ OpenStruct (private)

A private method to send a request with form body format.

Parameters:

  • method (Symbol)

    the HTTP method.

  • path (String)

    the path to send the request to.

  • payload (Hash)

    the request payload.

  • custom_token (String, nil)

    an optional custom token for the request.

Returns:

  • (OpenStruct)

    the parsed response.



91
92
93
94
95
# File 'lib/zspay/resource.rb', line 91

def req_form(method, path, payload, custom_token)
  payload = HTTP::FormData::Multipart.new(payload)
  res = HTTP.headers(headers(custom_token)).send(method, "#{endpoint}#{path}", form: payload)
  parse_body(res)
end

.req_json(method, path, payload, custom_token) ⇒ OpenStruct (private)

A private method to send a request with JSON body format.

Parameters:

  • method (Symbol)

    the HTTP method.

  • path (String)

    the path to send the request to.

  • payload (Hash)

    the request payload.

  • custom_token (String, nil)

    an optional custom token for the request.

Returns:

  • (OpenStruct)

    the parsed response.



78
79
80
81
82
# File 'lib/zspay/resource.rb', line 78

def req_json(method, path, payload, custom_token)
  res = HTTP.headers(headers(custom_token)).send(method, "#{endpoint}#{path}", json: payload)

  parse_body(res)
end

.success_request?(response) ⇒ Boolean (private)

Determines whether the response was successful based on the status code.

Parameters:

  • response (HTTP::Response)

    the response to check.

Returns:

  • (Boolean)

    true if the response was successful, false otherwise.



142
143
144
# File 'lib/zspay/resource.rb', line 142

def success_request?(response)
  response.code.to_s =~ /2../ && response
end