Class: SynapsePayRest::HTTPClient
- Inherits:
-
Object
- Object
- SynapsePayRest::HTTPClient
- Defined in:
- lib/synapse_pay_rest/http_client.rb
Overview
Wrapper for HTTP requests using RestClient.
Instance Attribute Summary collapse
-
#base_url ⇒ String
The base url of the API (production or sandbox).
-
#config ⇒ Hash
Various settings related to request headers.
-
#proxy_url ⇒ Object
Returns the value of attribute proxy_url.
Instance Method Summary collapse
-
#delete(path) ⇒ Hash
Sends a DELETE request to the given path with the given payload.
-
#get(path) ⇒ Hash
Sends a GET request to the given path with the given payload.
-
#headers ⇒ Hash
(also: #get_headers)
Returns headers for HTTP requests.
-
#initialize(base_url:, client_id:, fingerprint:, ip_address:, client_secret:, **options) ⇒ HTTPClient
constructor
A new instance of HTTPClient.
-
#patch(path, payload) ⇒ Hash
Sends a PATCH request to the given path with the given payload.
-
#post(path, payload, **options) ⇒ Hash
Sends a POST request to the given path with the given payload.
-
#update_headers(oauth_key: nil, fingerprint: nil, client_id: nil, client_secret: nil, ip_address: nil, **options) ⇒ void
Updates headers.
Constructor Details
#initialize(base_url:, client_id:, fingerprint:, ip_address:, client_secret:, **options) ⇒ HTTPClient
Returns a new instance of HTTPClient.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/synapse_pay_rest/http_client.rb', line 25 def initialize(base_url:, client_id:, fingerprint:, ip_address:, client_secret:, **) log_to = [:log_to] || 'stdout' RestClient.log = log_to if [:logging] @logging = [:logging] RestClient.proxy = [:proxy_url] if [:proxy_url] @proxy_url = [:proxy_url] @config = { client_id: client_id, client_secret: client_secret, fingerprint: fingerprint, ip_address: ip_address, oauth_key: '', } @base_url = base_url end |
Instance Attribute Details
#base_url ⇒ String
Returns the base url of the API (production or sandbox).
11 12 13 |
# File 'lib/synapse_pay_rest/http_client.rb', line 11 def base_url @base_url end |
#config ⇒ Hash
Returns various settings related to request headers.
11 |
# File 'lib/synapse_pay_rest/http_client.rb', line 11 attr_accessor :base_url, :config |
#proxy_url ⇒ Object
Returns the value of attribute proxy_url.
15 16 17 |
# File 'lib/synapse_pay_rest/http_client.rb', line 15 def proxy_url @proxy_url end |
Instance Method Details
#delete(path) ⇒ Hash
Sends a DELETE request to the given path with the given payload.
134 135 136 137 138 |
# File 'lib/synapse_pay_rest/http_client.rb', line 134 def delete(path) response = with_error_handling { RestClient.delete(full_url(path), headers) } p 'RESPONSE:', JSON.parse(response) if @logging JSON.parse(response) end |
#get(path) ⇒ Hash
Sends a GET request to the given path with the given payload.
121 122 123 124 125 |
# File 'lib/synapse_pay_rest/http_client.rb', line 121 def get(path) response = with_error_handling { RestClient.get(full_url(path), headers) } p 'RESPONSE:', JSON.parse(response) if @logging JSON.parse(response) end |
#headers ⇒ Hash Also known as: get_headers
Returns headers for HTTP requests.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/synapse_pay_rest/http_client.rb', line 47 def headers user = "#{config[:oauth_key]}|#{config[:fingerprint]}" gateway = "#{config[:client_id]}|#{config[:client_secret]}" headers = { :content_type => :json, :accept => :json, 'X-SP-GATEWAY' => gateway, 'X-SP-USER' => user, 'X-SP-USER-IP' => config[:ip_address] } end |
#patch(path, payload) ⇒ Hash
Sends a PATCH request to the given path with the given payload.
108 109 110 111 112 |
# File 'lib/synapse_pay_rest/http_client.rb', line 108 def patch(path, payload) response = with_error_handling { RestClient::Request.execute(:method => :patch, :url => full_url(path), :payload => payload.to_json, :headers => headers, :timeout => 300) } p 'RESPONSE:', JSON.parse(response) if @logging JSON.parse(response) end |
#post(path, payload, **options) ⇒ Hash
Sends a POST request to the given path with the given payload.
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/synapse_pay_rest/http_client.rb', line 89 def post(path, payload, **) headers = get_headers if [:idempotency_key] headers = headers.merge({'X-SP-IDEMPOTENCY-KEY' => [:idempotency_key]}) end response = with_error_handling { RestClient::Request.execute(:method => :post, :url => full_url(path), :payload => payload.to_json, :headers => headers, :timeout => 300) } p 'RESPONSE:', JSON.parse(response) if @logging JSON.parse(response) end |
#update_headers(oauth_key: nil, fingerprint: nil, client_id: nil, client_secret: nil, ip_address: nil, **options) ⇒ void
This method returns an undefined value.
Updates headers.
70 71 72 73 74 75 76 77 78 |
# File 'lib/synapse_pay_rest/http_client.rb', line 70 def update_headers(oauth_key: nil, fingerprint: nil, client_id: nil, client_secret: nil, ip_address: nil, **) config[:fingerprint] = fingerprint if fingerprint config[:oauth_key] = oauth_key if oauth_key config[:client_id] = client_id if client_id config[:client_secret] = client_secret if client_secret config[:ip_address] = ip_address if ip_address nil end |