From c77d41d3f1e736e133b1cf58b0bbd0e4b04912c1 Mon Sep 17 00:00:00 2001 From: Dan Milne Date: Mon, 9 Sep 2019 13:26:11 +1000 Subject: [PATCH] Add a configure method to the class --- README.md | 19 +++++++++++++++++++ lib/paapi.rb | 15 +++++++++++++++ lib/paapi/client.rb | 8 +++++++- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8a071ce..a8e70b4 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,25 @@ Or install it yourself as: ## Usage +The library can be initialised with a Rails initializer such as + +```ruby +Paapi.configure do |config| + config.access_key = 'your-access-key' + config.secret_key = 'your-secret-key' + config.partner_tag = 'your-tag' +end +``` + +Configurable itemes: + * access_key + * secret_key + * partner_tag + * partner_type + * marketplace + * test_mode + + ```ruby require 'paapi' diff --git a/lib/paapi.rb b/lib/paapi.rb index aa4a89a..752760e 100644 --- a/lib/paapi.rb +++ b/lib/paapi.rb @@ -8,4 +8,19 @@ require 'paapi/response' module Paapi class Error < StandardError; end class NotImplemented < StandardError; end + + class << self + attr_accessor :access_key, + :secret_key, + :partner_tag, + :partner_type, + :marketplace, + :test_mode + + def configure + yield self + true + end + alias_method :config, :configure + end end diff --git a/lib/paapi/client.rb b/lib/paapi/client.rb index f2556ae..53c4495 100644 --- a/lib/paapi/client.rb +++ b/lib/paapi/client.rb @@ -6,7 +6,13 @@ module Paapi attr_accessor :marketplace, :partner_tag attr_reader :partner_type, :access_key, :secret_key - def initialize(access_key:, secret_key:, marketplace:, partner_tag: nil, resources: nil, partner_type: 'Associates') + def initialize(access_key: Paapi.access_key, + secret_key: Paapi.secret_key, + marketplace: Paapi.marketplace || :us, + partner_tag: Paapi.partner_tag, + resources: nil, + partner_type: 'Associates' + ) raise ArgumentError unless MARKETPLACES.keys.include?(marketplace.to_sym) @access_key = access_key @secret_key = secret_key