Class: MerchantZip::Client
- Inherits:
-
Object
- Object
- MerchantZip::Client
- Extended by:
- Forwardable
- Defined in:
- lib/merchant-zip/client.rb
Constant Summary collapse
- DEFAULT_API_MODE =
:live
- API_MODES =
[:live, :sandbox]
- DEFAULT_API_ENDPOINT =
{ sandbox: "https://global-api.sand.au.edge.zip.co/merchant", live: "https://global-api.prod.au.edge.zip.co/merchant" }
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #api_base ⇒ Object
- #api_mode ⇒ Object
- #execute_request(method, url, params = {}, opts = {}) ⇒ Object
-
#initialize(config = {}) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(config = {}) ⇒ Client
Returns a new instance of Client.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/merchant-zip/client.rb', line 19 def initialize(config = {}) @config = case config when Hash MerchantZip.config.reverse_duplicate_merge(config) when MerchantZip::Configuration config else MerchantZip.config.dup end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
14 15 16 |
# File 'lib/merchant-zip/client.rb', line 14 def config @config end |
Instance Method Details
#api_base ⇒ Object
38 39 40 |
# File 'lib/merchant-zip/client.rb', line 38 def api_base DEFAULT_API_ENDPOINT[api_mode] end |
#api_mode ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/merchant-zip/client.rb', line 30 def api_mode if API_MODES.include?(mode&.to_sym) mode.to_sym else DEFAULT_API_MODE end end |
#execute_request(method, url, params = {}, opts = {}) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/merchant-zip/client.rb', line 42 def execute_request(method, url, params = {}, opts = {}) request_opts = opts headers = { "Content-Type" => "application/json", "Authorization" => "Bearer #{api_key}", "idempotency-key" => SecureRandom.base64(18), } request_url = api_base + url.to_s case method.to_s.downcase.to_sym when :get, :head, :delete # Make params into GET parameters request_url += "#{URI.parse(url).query ? '&' : '?'}#{Util.encode_parameters(params)}" if params && params.any? payload = nil else payload = params end payload = payload.to_json if payload.is_a?(Hash) request_opts.update(method: method, headers: headers, payload: payload, url: request_url) response = RestClient::Request.execute(request_opts) JSON.parse(response.body, object_class: OpenStruct) rescue => e puts e.response raise end |