Class: Zspay::Resource
- Inherits:
-
Object
- Object
- Zspay::Resource
- 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
Class Method Summary collapse
-
.delete(path, custom_token = nil) ⇒ OpenStruct
Performs a DELETE request to the specified API path.
-
.endpoint ⇒ String
private
Returns the base endpoint URL from the configuration.
-
.get(path, custom_token = nil) ⇒ OpenStruct
Performs a GET request to the specified API path.
-
.headers(custom_token = nil) ⇒ Hash
private
Constructs the headers for the request.
-
.parse_body(response) ⇒ OpenStruct
private
Parses the response body and returns it as an OpenStruct.
-
.parse_json(json) ⇒ OpenStruct, String
private
Parses a JSON string into an OpenStruct.
-
.patch(path, payload, custom_token = nil) ⇒ OpenStruct
Performs a PATCH request to the specified API path.
-
.post(path, payload = {}, custom_token = nil, body: "json") ⇒ OpenStruct
Performs a POST request to the specified API path.
-
.put(path, payload = {}, custom_token = nil) ⇒ OpenStruct
Performs a PUT request to the specified API path.
-
.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.
-
.req_form(method, path, payload, custom_token) ⇒ OpenStruct
private
A private method to send a request with form body format.
-
.req_json(method, path, payload, custom_token) ⇒ OpenStruct
private
A private method to send a request with JSON body format.
-
.success_request?(response) ⇒ Boolean
private
Determines whether the response was successful based on the status code.
Class Method Details
.delete(path, custom_token = nil) ⇒ OpenStruct
Performs a DELETE request to the specified API path.
43 44 45 |
# File 'lib/zspay/resource.rb', line 43 def delete(path, custom_token = nil) req(:delete, path, {}, custom_token) end |
.endpoint ⇒ String (private)
Returns the base endpoint URL from the configuration.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
142 143 144 |
# File 'lib/zspay/resource.rb', line 142 def success_request?(response) response.code.to_s =~ /2../ && response end |