Class: PostageApp::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/postageapp/configuration.rb

Overview

Advanced Options


:port - The port to make HTTP/HTTPS requests (default based on secure option) :scheme - Set to either ‘http` or `https` (default based on secure option) :requests_to_resend - List of API calls that should be replayed if they fail.

(default: send_message)

Constant Summary collapse

SOCKS5_PORT_DEFAULT =

Constants ============================================================

1080
HTTP_PORT_DEFAULT =
80
HTTPS_PORT_DEFAULT =
443
SCHEME_FOR_SECURE =
{
  true => 'https'.freeze,
  false => 'http'.freeze
}.freeze
CONFIG_PARAMS =
{
  api_key: {
    default: nil,
    desc: 'Project API key to use',
    required: 'for project API functions'
  },
  account_api_key: {
    default: nil,
    desc: 'Account API key to use',
    required: 'for account API functions'
  },
  postback_secret: {
    default: nil,
    desc: 'Secret to use for validating ActionMailbox requests'
  },
  project_root: {
    default: -> {
      if (defined?(Rails) and Rails.respond_to?(:root))
        Rails.root
      else
        Dir.pwd
      end
    },
    desc: 'Project root for logging purposes'
  },
  recipient_override: {
    default: nil,
    interrogator: true,
    desc: 'Override sender on `send_message` calls'
  },
  logger: {
    default: nil,
    env: false,
    desc: 'Logger instance to use'
  },
  secure: {
    default: true,
    interrogator: true,
    env: false,
    after_set: -> (config) {
      if (config.secure?)
        config.protocol = 'https'
        if (config.port == 80)
          config.port = 443
        end
      else
        config.protocol = 'http'
        if (config.port == 443)
          config.port = 80
        end
      end
    },
    desc: 'Enable verifying TLS connections'
  },
  verify_tls: {
    default: true,
    aliases: [ :verify_certificate ],
    interrogator: true,
    parse: -> (v) {
      case (v)
      when 'true', 'yes', 'on'
        true
      when String
        v.to_i != 0
      else
        !!v
      end
    },
    desc: 'Enable TLS certificate verification'
  },
  host: {
    default: 'api.postageapp.com'.freeze,
    desc: 'API host to contact'
  },
  port: {
    default: 443,
    desc: 'API port to contact'
  },
  scheme: {
    default: 'https'.freeze,
    aliases: [ :protocol ],
    desc: 'HTTP scheme to use'
  },
  proxy_username: {
    default: nil,
    aliases: [ :proxy_user ],
    desc: 'SOCKS5 proxy username'
  },
  proxy_password: {
    default: nil,
    aliases: [ :proxy_pass ],
    desc: 'SOCKS5 proxy password'
  },
  proxy_host: {
    default: nil,
    desc: 'SOCKS5 proxy host'
  },
  proxy_port: {
    default: 1080,
    parse: -> (v) { v.to_i },
    desc: 'SOCKS5 proxy port'
  },
  open_timeout: {
    default: 5,
    aliases: [ :http_open_timeout ],
    parse: -> (v) { v.to_i },
    desc: 'Timeout in seconds when initiating requests'
  },
  read_timeout: {
    default: 10,
    aliases: [ :http_read_timeout ],
    parse: -> (v) { v.to_i },
    desc: 'Timeout in seconds when awaiting responses'
  },
  retry_methods: {
    default: %w[ send_message ].freeze,
    aliases: [ :requests_to_resend ],
    parse: -> (v) {
      case (v)
      when String
        v.split(/\s*(?:,|\s)\s*/).grep(/\S/)
      else
        v
      end
    },
    desc: 'Which API calls to retry, comma and/or space separated'
  },
  framework: {
    default: -> {
      if (defined?(Rails) and Rails.respond_to?(:version))
        'Ruby %s / Ruby on Rails %s' % [
          RUBY_VERSION,
          Rails.version
        ]
      else
        'Ruby %s' % RUBY_VERSION
      end
    },
    desc: 'Framework used'
  },
  environment: {
    default: 'production',
    desc: 'Environment to use'
  }
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Instance Methods =====================================================



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/postageapp/configuration.rb', line 268

def initialize
  credentials = self.rails_credentials

  CONFIG_PARAMS.each do |param, config|
    value = (
      config[:sources]&.map { |s| credentials[s] }&.compact&.first ||
      config[:env_vars]&.map { |v| ENV[v] }&.compact&.first
    )

    if (value)
      if (config[:parse])
        instance_variable_set(config[:ivar], config[:parse].call(value))
      else
        instance_variable_set(config[:ivar], value)
      end
    else
      case (config[:default])
      when Proc
        instance_variable_set(config[:ivar], config[:default].call)
      else
        instance_variable_set(config[:ivar], config[:default])
      end
    end
  end
end

Class Method Details

.paramsObject

Class Methods ========================================================



262
263
264
# File 'lib/postageapp/configuration.rb', line 262

def self.params
  CONFIG_PARAMS
end

Instance Method Details

#httpObject

Returns a connection aimed at the API endpoint



315
316
317
# File 'lib/postageapp/configuration.rb', line 315

def http
  PostageApp::HTTP.connect(self)
end

#port_default?Boolean

Returns true if the port used for the API is the default port, otherwise false. 80 for HTTP, 443 for HTTPS.

Returns:

  • (Boolean)


296
297
298
# File 'lib/postageapp/configuration.rb', line 296

def port_default?
  self.port == (self.secure? ? HTTPS_PORT_DEFAULT : HTTP_PORT_DEFAULT)
end

#proxy?Boolean

Returns true if a proxy is defined, otherwise false.

Returns:

  • (Boolean)


301
302
303
# File 'lib/postageapp/configuration.rb', line 301

def proxy?
  self.proxy_host and self.proxy_host.match(/\A\S+\z/)
end

#urlObject

Returns the endpoint URL to make API calls



306
307
308
309
310
311
312
# File 'lib/postageapp/configuration.rb', line 306

def url
  '%s://%s%s' % [
    self.scheme,
    self.host,
    self.port_default? ? '' : (':%d' % self.port)
  ]
end