Class: MC2P::APIRequest
- Inherits:
-
Object
- Object
- MC2P::APIRequest
- Defined in:
- lib/request.rb
Overview
API request - class used to connect with the API
Instance Method Summary collapse
-
#_request(method, status_code = 200, path = nil, data = nil, abs_url = nil, resource = nil, resource_id = nil) ⇒ Object
- Decorator to make the request based on the method received Params:
method
- method to make the request
status_code
-
value to check if the request receive a correct response Returns: a function to make the request.
- method to make the request
- Decorator to make the request based on the method received Params:
-
#delete(path = nil, data = nil, abs_url = nil, resource = nil, resource_id = nil) ⇒ Object
DELETE 204 function.
-
#get(path = nil, data = nil, abs_url = nil, resource = nil, resource_id = nil) ⇒ Object
GET 200 function.
-
#get_abs_url(path) ⇒ Object
- Params:
path
-
relative url Returns: The absolute url to send the request.
- Params:
-
#headers ⇒ Object
Creates the headers to include in the request Returns: A dictionary with the headers needed for the API.
-
#initialize(key, secret_key) ⇒ APIRequest
constructor
- Initializes an api request Params:
key
- key to connect with API
secret_key
-
secret key to connect with API.
- key to connect with API
- Initializes an api request Params:
-
#patch(path = nil, data = nil, abs_url = nil, resource = nil, resource_id = nil) ⇒ Object
PATCH 200 function.
-
#post(path = nil, data = nil, abs_url = nil, resource = nil, resource_id = nil) ⇒ Object
POST 201 function.
-
#post200(path = nil, data = nil, abs_url = nil, resource = nil, resource_id = nil) ⇒ Object
POST 200 function.
Constructor Details
#initialize(key, secret_key) ⇒ APIRequest
Initializes an api request Params:
key
-
key to connect with API
secret_key
-
secret key to connect with API
8 9 10 11 12 13 14 |
# File 'lib/request.rb', line 8 def initialize(key, secret_key) @key = key @secret_key = secret_key @authorization_header = 'AppKeys' @api_url = 'api.mychoice2pay.com/v1' end |
Instance Method Details
#_request(method, status_code = 200, path = nil, data = nil, abs_url = nil, resource = nil, resource_id = nil) ⇒ Object
Decorator to make the request based on the method received Params:
method
-
method to make the request
status_code
-
value to check if the request receive a correct response
Returns: a function to make the request
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/request.rb', line 37 def _request(method, status_code = 200, path = nil, data = nil, abs_url = nil, resource = nil, resource_id = nil) url = abs_url.nil? ? get_abs_url(path) : abs_url request = Unirest.send(method.downcase, url, headers: headers, parameters: data.to_json) if request.code != status_code raise InvalidRequestError.new( "Error #{request.code}", request.body, resource, resource_id ) end status_code != 204 ? request.body : {} end |
#delete(path = nil, data = nil, abs_url = nil, resource = nil, resource_id = nil) ⇒ Object
DELETE 204 function
82 83 84 85 |
# File 'lib/request.rb', line 82 def delete(path = nil, data = nil, abs_url = nil, resource = nil, resource_id = nil) _request('delete', 204, path, data, abs_url, resource, resource_id) end |
#get(path = nil, data = nil, abs_url = nil, resource = nil, resource_id = nil) ⇒ Object
GET 200 function
70 71 72 73 |
# File 'lib/request.rb', line 70 def get(path = nil, data = nil, abs_url = nil, resource = nil, resource_id = nil) _request('get', 200, path, data, abs_url, resource, resource_id) end |
#get_abs_url(path) ⇒ Object
Params:
path
-
relative url
Returns: The absolute url to send the request
28 29 30 |
# File 'lib/request.rb', line 28 def get_abs_url(path) "https://#{@api_url}#{path}" end |
#headers ⇒ Object
Creates the headers to include in the request Returns: A dictionary with the headers needed for the API
18 19 20 21 22 23 |
# File 'lib/request.rb', line 18 def headers { 'authorization' => "#{@authorization_header} #{@key}:#{@secret_key}", 'content-type' => 'application/json' } end |
#patch(path = nil, data = nil, abs_url = nil, resource = nil, resource_id = nil) ⇒ Object
PATCH 200 function
76 77 78 79 |
# File 'lib/request.rb', line 76 def patch(path = nil, data = nil, abs_url = nil, resource = nil, resource_id = nil) _request('patch', 200, path, data, abs_url, resource, resource_id) end |
#post(path = nil, data = nil, abs_url = nil, resource = nil, resource_id = nil) ⇒ Object
POST 201 function
58 59 60 61 |
# File 'lib/request.rb', line 58 def post(path = nil, data = nil, abs_url = nil, resource = nil, resource_id = nil) _request('post', 201, path, data, abs_url, resource, resource_id) end |
#post200(path = nil, data = nil, abs_url = nil, resource = nil, resource_id = nil) ⇒ Object
POST 200 function
64 65 66 67 |
# File 'lib/request.rb', line 64 def post200(path = nil, data = nil, abs_url = nil, resource = nil, resource_id = nil) _request('post', 200, path, data, abs_url, resource, resource_id) end |