From 2d9f7709dbc288823839e4f00ebdc689d8a8a781 Mon Sep 17 00:00:00 2001 From: Adam Cooke Date: Thu, 15 Oct 2020 12:11:36 +0100 Subject: [PATCH] feat: support for custom loggers for API & execution --- README.md | 10 +++------- lib/xero_exporter/api.rb | 5 +++-- lib/xero_exporter/executor.rb | 5 +++-- lib/xero_exporter/export.rb | 4 ++-- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 991b454..d12fa73 100644 --- a/README.md +++ b/README.md @@ -106,13 +106,9 @@ end # happening in a log file so you can see exactly what's going to be exported. # Create a new API instance which can be used to authenticate to the API. -# You'll need to register appropriately with Xero to obtain client id/secret -# and then use OAuth to generate an access token. -api = XeroExporter::API.new(client_id, client_secret, access_token) +api = XeroExporter::API.new(access_token, tenant_id) # Once you have your API instance, go ahead and initiate the export by providing -# your export instance and the ID of the organization that you wish to execute -# it against. You can also provider a logger which will receive useful text -# throughout the process outlining the current state of things. -api.export(tenant_id, export, logger: Logger.new(STDOUT)) +# the API instance. +export.execute(api) ``` diff --git a/lib/xero_exporter/api.rb b/lib/xero_exporter/api.rb index 8ffcc8f..e5b0b45 100644 --- a/lib/xero_exporter/api.rb +++ b/lib/xero_exporter/api.rb @@ -35,9 +35,10 @@ def to_s put: Net::HTTP::Put }.freeze - def initialize(access_token, tenant_id) + def initialize(access_token, tenant_id, **options) @access_token = access_token @tenant_id = tenant_id + @logger = options[:logger] end def get(path, params = {}) @@ -124,7 +125,7 @@ def handle_error(response) end def logger - XeroExporter.logger + @logger || XeroExporter.logger end end diff --git a/lib/xero_exporter/executor.rb b/lib/xero_exporter/executor.rb index 6ca32b9..62aace3 100644 --- a/lib/xero_exporter/executor.rb +++ b/lib/xero_exporter/executor.rb @@ -11,9 +11,10 @@ class Executor attr_accessor :state_reader attr_accessor :state_writer - def initialize(export, api) + def initialize(export, api, **options) @export = export @api = api + @logger = options[:logger] @proposal = Proposal.new(export) end @@ -338,7 +339,7 @@ def tax_rates # # @return [Logger] def logger - XeroExporter.logger + @logger || XeroExporter.logger end # Executes a named task if it is suitable to be executed diff --git a/lib/xero_exporter/export.rb b/lib/xero_exporter/export.rb index 1832c87..fdffe17 100644 --- a/lib/xero_exporter/export.rb +++ b/lib/xero_exporter/export.rb @@ -68,8 +68,8 @@ def add_refund refund end - def execute(api) - executor = Executor.new(self, api) + def execute(api, **options) + executor = Executor.new(self, api, **options) yield executor if block_given? executor.execute_all end