Skip to content

Commit

Permalink
feat: support for custom loggers for API & execution
Browse files Browse the repository at this point in the history
  • Loading branch information
adamcooke committed Oct 15, 2020
1 parent 1bf2f22 commit 2d9f770
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
5 changes: 3 additions & 2 deletions lib/xero_exporter/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {})
Expand Down Expand Up @@ -124,7 +125,7 @@ def handle_error(response)
end

def logger
XeroExporter.logger
@logger || XeroExporter.logger
end

end
Expand Down
5 changes: 3 additions & 2 deletions lib/xero_exporter/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/xero_exporter/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2d9f770

Please sign in to comment.