Sha256: eabe05e55adc7adb45255b9243c621df40759e4f82a9e5d67f7f5ce6eefc095a

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

require 'rakuten_web_service/string_support'

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.key?('RWS_SDK_DEBUG') || debug
    end

    private

    using RakutenWebService::StringSupport

    def convert_snake_key_to_camel_key(params)
      params.inject({}) do |h, (k, v)|
        k = k.to_s.to_camel
        h[k] = v
        h
      end
    end
  end

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

  def configuration(&block)
    warn 'Warning: RakutenWebService.configuration is deprecated. Use RakutenWebService.configure.' if block_given?
    configure(&block)
  end

  module_function :configure, :configuration
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rakuten_web_service-1.10.0 lib/rakuten_web_service/configuration.rb
rakuten_web_service-1.9.2 lib/rakuten_web_service/configuration.rb