Sha256: 79bbd441d3aa2883477a508dadef7d564a70cc5a092d509f1a10975122039e70

Contents?: true

Size: 1.43 KB

Versions: 5

Compression:

Stored size: 1.43 KB

Contents

module RakutenWebService
  class Configuration
    attr_accessor :application_id, :affiliate_id, :max_retries, :debug

    def initialize
      @application_id = ENV['RWS_APPLICATION_ID']
      @affiliate_id = ENV['RWS_AFFILIATE_ID']
      @max_retries = 5
    end

    def generate_parameters(params)
      convert_snake_key_to_camel_key(default_parameters.merge(params))
    end

    def default_parameters
      raise "Application ID is not defined" unless has_required_options?
      { application_id: application_id, affiliate_id: affiliate_id, format_version: '2' }
    end

    def has_required_options?
      application_id && application_id != ''
    end

    def debug_mode?
      ENV.has_key?('RWS_SDK_DEBUG') || debug
    end

    private
    def convert_snake_key_to_camel_key(params)
      params.inject({}) do |h, (k, v)|
        k = k.to_s.gsub(/([a-z]{1})_([a-z]{1})/) { "#{$1}#{$2.capitalize}" }
        h[k] = v
        h
      end
    end
  end

  def configure(&block)
    @configuration ||= Configuration.new
    if block
      if block.arity != 1
        raise ArgumentError, 'Block is required to have one argument' 
      end
      block.call(@configuration)
    end
    return @configuration
  end

  def configuration(&block)
    $stderr.puts "Warning: RakutenWebService.configuration is deprecated. Use RakutenWebService.configure." if block_given?
    self.configure(&block)
  end

  module_function :configure, :configuration
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rakuten_web_service-1.9.0 lib/rakuten_web_service/configuration.rb
rakuten_web_service-1.8.0 lib/rakuten_web_service/configuration.rb
rakuten_web_service-1.7.0 lib/rakuten_web_service/configuration.rb
rakuten_web_service-1.6.1 lib/rakuten_web_service/configuration.rb
rakuten_web_service-1.6.0 lib/rakuten_web_service/configuration.rb