lib/buckaruby/configuration.rb in buckaruby-1.7.0 vs lib/buckaruby/configuration.rb in buckaruby-2.0.0

- old
+ new

@@ -3,71 +3,66 @@ require 'logger' module Buckaruby # Configuration settings for the Buckaruby Gateway. class Configuration - TEST_URL = "https://testcheckout.buckaroo.nl/nvp/" - PRODUCTION_URL = "https://checkout.buckaroo.nl/nvp/" + TEST_URL = 'https://testcheckout.buckaroo.nl/nvp/' + LIVE_URL = 'https://checkout.buckaroo.nl/nvp/' - attr_reader :website, :secret, :mode, :hash_method, :logger - def initialize(options) - set_website(options) - set_secret(options) - set_buckaroo_mode(options) - set_hash_method(options) - set_logger(options) + @options = options end def test? - @mode == :test + @options.fetch(:test, false) end - def production? - @mode == :production + def live? + !test? end def api_url - test? ? TEST_URL : PRODUCTION_URL + live? ? LIVE_URL : TEST_URL end - private + def website + @website ||= begin + website = @options[:website] + raise ArgumentError, 'Missing required parameter: website' if website.to_s.empty? - def set_website(options) - @website = options[:website] - - raise ArgumentError, "Missing required parameter: website" if @website.to_s.empty? + website + end end - def set_secret(options) - @secret = options[:secret] + def secret + @secret ||= begin + secret = @options[:secret] + raise ArgumentError, 'Missing required parameter: secret' if secret.to_s.empty? - raise ArgumentError, "Missing required parameter: secret" if @secret.to_s.empty? - end - - # Set Buckaroo mode from options, class setting or the default (test). - def set_buckaroo_mode(options) - @mode = options.key?(:mode) ? options[:mode] : Gateway.mode - @mode ||= :test - - if @mode != :test && @mode != :production - raise ArgumentError, "Invalid Buckaroo mode provided: #{@mode} (expected :test or :production)" + secret end end - # Set the hash method from options or default (SHA-1). - def set_hash_method(options) - @hash_method = (options[:hash_method] || "SHA1").downcase.to_sym + # Use the hash method from options or default (SHA-1). + def hash_method + @hash_method ||= begin + hash_method = (@options[:hash_method] || 'SHA1').downcase.to_sym - unless [:sha1, :sha256, :sha512].include?(@hash_method) - raise ArgumentError, "Invalid hash method provided: #{@hash_method} (expected :sha1, :sha256 or :sha512)" + unless [:sha1, :sha256, :sha512].include?(hash_method) + raise ArgumentError, "Invalid hash method provided: #{hash_method} (expected :sha1, :sha256 or :sha512)" + end + + hash_method end end - # Set the logger from options, to Rails or to stdout. - def set_logger(options) - @logger = options[:logger] - @logger ||= Rails.logger if defined?(Rails) - @logger ||= Logger.new(STDOUT) + # Use the logger from options, to Rails or to stdout. + def logger + @logger ||= begin + logger = @options[:logger] + logger ||= Rails.logger if defined?(Rails) + logger ||= Logger.new($stdout) + logger + end end end end