diff --git a/lib/trolley.rb b/lib/trolley.rb index 9980f11..54bdea4 100644 --- a/lib/trolley.rb +++ b/lib/trolley.rb @@ -10,6 +10,7 @@ require 'trolley/gateways/InvoiceGateway' require 'trolley/gateways/InvoicePaymentGateway' require 'trolley/gateways/VerificationGateway' +require 'trolley/gateways/PartnerGateway' require 'trolley/utils/PaginatedArray' require 'trolley/utils/ResponseMapper' diff --git a/lib/trolley/Gateway.rb b/lib/trolley/Gateway.rb index 8d77247..8c2ef4e 100644 --- a/lib/trolley/Gateway.rb +++ b/lib/trolley/Gateway.rb @@ -1,6 +1,6 @@ module Trolley class Gateway - attr_accessor :config, :client, :recipient, :recipient_account, :batch, :payment, :balance, :offline_payment, :invoice, :invoice_payment, :verification, :trust + attr_accessor :config, :client, :recipient, :recipient_account, :batch, :payment, :balance, :offline_payment, :invoice, :invoice_payment, :verification, :trust, :partner def initialize(config) @config = config @@ -15,6 +15,7 @@ def initialize(config) @invoice_payment = InvoicePaymentGateway.new(client) @verification = VerificationGateway.new(client) @trust = @verification + @partner = PartnerGateway.new(client) end def request(method, end_point, body = nil) diff --git a/lib/trolley/gateways/PartnerGateway.rb b/lib/trolley/gateways/PartnerGateway.rb new file mode 100644 index 0000000..8acdaa4 --- /dev/null +++ b/lib/trolley/gateways/PartnerGateway.rb @@ -0,0 +1,82 @@ +require_relative '../Client' +require 'json' +require 'ostruct' +require 'uri' + +module Trolley + class PartnerGateway + def initialize(client) + @client = client + end + + def get_fees(currency = nil) + path = '/v1/fees' + path = "#{path}?#{URI.encode_www_form(currency: currency)}" unless currency.nil? + parse_response(@client.get(path)) + end + + def update_fees(body) + parse_response(@client.patch('/v1/fees', body)) + end + + def list_payout_methods + parse_response(@client.get('/v1/payout-methods')) + end + + def get_payout_method(payout_method) + parse_response(@client.get("/v1/payout-methods/#{payout_method}")) + end + + def update_payout_method(payout_method, body) + parse_response(@client.patch("/v1/payout-methods/#{payout_method}", body)) + end + + def get_processing_settings + parse_response(@client.get('/v1/processing-settings')) + end + + def update_processing_settings(body) + parse_response(@client.patch('/v1/processing-settings', body)) + end + + def get_white_label_dns_records + parse_response(@client.get('/v1/white-label/dns-records')) + end + + def verify_white_label_dns_records + parse_response(@client.post('/v1/white-label/dns-records/verify', {})) + end + + def delete_white_label_email + parse_response(@client.delete('/v1/white-label/email')) + end + + def update_white_label_icon(body) + parse_response(@client.patch('/v1/white-label/icon', body)) + end + + def get_white_label_settings + parse_response(@client.get('/v1/white-label')) + end + + def update_white_label_settings(body) + parse_response(@client.patch('/v1/white-label', body)) + end + + def get_widget_configuration + parse_response(@client.get('/v1/iframe/config')) + end + + def update_widget_configuration(body = {}) + parse_response(@client.post('/v1/iframe/config', body)) + end + + private + + def parse_response(response) + return nil if response.nil? || response.empty? + + JSON.parse(response, object_class: OpenStruct) + end + end +end diff --git a/test/unit/PartnerGatewayTest.rb b/test/unit/PartnerGatewayTest.rb new file mode 100644 index 0000000..242caa4 --- /dev/null +++ b/test/unit/PartnerGatewayTest.rb @@ -0,0 +1,69 @@ +require_relative '../../lib/trolley' +require 'test/unit' +require 'webmock/test_unit' + +class PartnerGatewayTest < Test::Unit::TestCase + API_BASE = 'http://api.local.dev:3000'.freeze + + def setup + @partner = Trolley.client('key', 'secret', api_base: API_BASE).partner + @body = { enabled: true } + end + + def test_read_methods_call_documented_endpoints + %w[ + /v1/fees + /v1/payout-methods + /v1/payout-methods/bank-transfer + /v1/processing-settings + /v1/white-label/dns-records + /v1/white-label + /v1/iframe/config + ].each do |path| + stub_request(:get, "#{API_BASE}#{path}").to_return(status: 200, body: '{"ok":true}') + end + stub_request(:get, "#{API_BASE}/v1/fees?currency=USD") + .to_return(status: 200, body: '{"ok":true}') + + assert_equal true, @partner.get_fees.ok + assert_equal true, @partner.get_fees('USD').ok + assert_equal true, @partner.list_payout_methods.ok + assert_equal true, @partner.get_payout_method('bank-transfer').ok + assert_equal true, @partner.get_processing_settings.ok + assert_equal true, @partner.get_white_label_dns_records.ok + assert_equal true, @partner.get_white_label_settings.ok + assert_equal true, @partner.get_widget_configuration.ok + end + + def test_mutation_methods_call_documented_endpoints + patch_paths = %w[ + /v1/fees + /v1/payout-methods/bank-transfer + /v1/processing-settings + /v1/white-label/icon + /v1/white-label + ] + patch_paths.each do |path| + stub_request(:patch, "#{API_BASE}#{path}") + .with(body: @body.to_json) + .to_return(status: 200, body: '{"ok":true}') + end + stub_request(:post, "#{API_BASE}/v1/white-label/dns-records/verify") + .with(body: '{}') + .to_return(status: 200, body: '{"ok":true}') + stub_request(:post, "#{API_BASE}/v1/iframe/config") + .with(body: @body.to_json) + .to_return(status: 200, body: '{"ok":true}') + stub_request(:delete, "#{API_BASE}/v1/white-label/email") + .to_return(status: 200, body: '{"ok":true}') + + assert_equal true, @partner.update_fees(@body).ok + assert_equal true, @partner.update_payout_method('bank-transfer', @body).ok + assert_equal true, @partner.update_processing_settings(@body).ok + assert_equal true, @partner.verify_white_label_dns_records.ok + assert_equal true, @partner.delete_white_label_email.ok + assert_equal true, @partner.update_white_label_icon(@body).ok + assert_equal true, @partner.update_white_label_settings(@body).ok + assert_equal true, @partner.update_widget_configuration(@body).ok + end +end