Sha256: cf1ea1b892b6570f0c6a7cca8eb5db06aa8c0e999923666767cee342506f9088

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module PaypalAPI
  #
  # Stores configuration for PaypalAPI Client
  #
  class Config
    # Live PayPal URL
    LIVE_URL = "https://api-m.paypal.com"

    # Sandbox PayPal URL
    SANDBOX_URL = "https://api-m.sandbox.paypal.com"

    # Default config options
    DEFAULTS = {
      live: false,
      http_opts: {}.freeze,
      retries: {enabled: true, count: 3, sleep: [0.25, 0.75, 1.5].freeze}.freeze
    }.freeze

    attr_reader :client_id, :client_secret, :live, :http_opts, :retries

    # Initializes Config
    #
    # @param client_id [String] PayPal client id
    # @param client_secret [String] PayPal client secret
    # @param live [Boolean] PayPal live/sandbox mode
    # @param http_opts [Hash] Net::Http opts for all requests
    # @param retries [Hash] Retries configuration
    #
    # @return [Client] Initialized config object
    #
    def initialize(client_id:, client_secret:, live: nil, http_opts: nil, retries: nil)
      @client_id = client_id
      @client_secret = client_secret
      @live = with_default(:live, live)
      @http_opts = with_default(:http_opts, http_opts)
      @retries = with_default(:retries, retries)
      freeze
    end

    # @return [String] PayPal live or sandbox URL
    def url
      live ? LIVE_URL : SANDBOX_URL
    end

    #
    # Instance representation string. Default was overwritten to hide secrets
    #
    def inspect
      "#<#{self.class.name} live: #{live}>"
    end

    alias_method :to_s, :inspect

    private

    def with_default(option_name, value)
      default = DEFAULTS.fetch(option_name)

      case value
      when NilClass then default
      when Hash then default.merge(value)
      else value
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
paypal-rest-api-0.0.4 lib/paypal-api/config.rb
paypal-rest-api-0.0.3 lib/paypal-api/config.rb
paypal-rest-api-0.0.2 lib/paypal-api/config.rb