Sha256: d76c4f0070ae7d2a7a3b8cd0ffd256f4b5b7c444fffb98085d3b942b77637f58

Contents?: true

Size: 1.71 KB

Versions: 6

Compression:

Stored size: 1.71 KB

Contents

require 'singleton'
require 'nori'

module SmsTraffic
  class Configuration
    include Singleton

    attr_accessor :login, :password, :host, :originator, :debug, :logger, :xml_parser, :validate_phone

    def initialize
      self.debug          = default_configuration[:debug]
      self.logger         = default_configuration[:logger]
      self.xml_parser     = default_configuration[:xml_parser]
      self.validate_phone = default_configuration[:validate_phone]
      super
    end

    def reset!
      default_configuration.each do |key, value|
        send("#{key}=", value)
      end
    end

    def validate!
      validation_rules.each do |key, value|
        raise ArgumentError, "#{key} should be defined for #{self}." if value.nil?
      end
    end

    def to_h
      {
        login:      login,
        password:   password,
        host:       host,
        originator: originator,
        debug:      debug,
        logger:     logger,
        xml_parser: xml_parser
      }
    end

    def values_at(*keys)
      to_h.values_at(*keys)
    end

    private

    def default_configuration
      {
        login:          nil,
        password:       nil,
        host:           nil,
        originator:     nil,
        debug:          false,
        logger:         $stdout,
        xml_parser:     default_xml_parser,
        validate_phone: true
      }
    end

    def validation_rules
      {
        'Login'        => login,
        'Password'     => password,
        'Server'       => host,
        'Originator'   => originator,
        'Debug logger' => debug && !logger ? nil : true
      }
    end

    def default_xml_parser
      @default_xml_parser ||= Nori.new(advanced_typecasting: true).freeze
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sms_traffic_sdk-0.0.7 lib/sms_traffic/configuration.rb
sms_traffic_sdk-0.0.6 lib/sms_traffic/configuration.rb
sms_traffic_sdk-0.0.5 lib/sms_traffic/configuration.rb
sms_traffic_sdk-0.0.4 lib/sms_traffic/configuration.rb
sms_traffic_sdk-0.0.3 lib/sms_traffic/configuration.rb
sms_traffic_sdk-0.0.2 lib/sms_traffic/configuration.rb