Class: Glabssms::Configuration

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

Constant Summary collapse

READABLE_ATTRIBUTES =
%i[app_id app_secret cross_telco_short_code short_code]
WRITABLE_ATTRIBUTES =
%i[app_id app_secret cross_telco_short_code short_code logger]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Returns a new instance of Configuration.



45
46
47
48
49
50
# File 'lib/glabssms/configuration.rb', line 45

def initialize(options = {})
  WRITABLE_ATTRIBUTES.each do |attr|
    instance_variable_set "@#{attr}",
      options[attr] || Glabssms.configuration.send(attr)
  end
end

Class Method Details

._default_loggerObject



56
57
58
59
60
# File 'lib/glabssms/configuration.rb', line 56

def self._default_logger
  logger = Logger.new(STDOUT)
  logger.level = Logger::INFO
  logger
end

.clientObject



33
34
35
# File 'lib/glabssms/configuration.rb', line 33

def self.client
  Glabssms::Client.new(instantiate)
end

.expectant_reader(*attributes) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/glabssms/configuration.rb', line 13

def self.expectant_reader(*attributes)
  attributes.each do |attribute|
    (
      class << self
        self
      end
    )
      .send(:define_method, attribute) do
      attribute_value = instance_variable_get("@#{attribute}")
      if attribute_value.nil? || attribute_value.to_s.empty?
        raise ConfigurationError.new(
          "Glabssms::Configuration.#{attribute} needs to be set"
        )
      end
      attribute_value
    end
  end
end

.instantiateObject



37
38
39
# File 'lib/glabssms/configuration.rb', line 37

def self.instantiate
  config = new
end

.loggerObject



41
42
43
# File 'lib/glabssms/configuration.rb', line 41

def self.logger
  @logger ||= _default_logger
end

Instance Method Details

#assert_has_keysObject



66
67
68
69
70
71
72
# File 'lib/glabssms/configuration.rb', line 66

def assert_has_keys
  if app_id.nil? || app_secret.nil? || cross_telco_short_code.nil? || short_code.nil?
    raise ConfigurationError.new(
      'Glabssms::Configuration app_id, app_secret, cross_telco_short_code, short_code and are required.'
    )
  end
end

#inspectObject



62
63
64
# File 'lib/glabssms/configuration.rb', line 62

def inspect
  super.gsub(/@app_secret=\".*\"/, '@app_secret="[FILTERED]"')
end

#loggerObject



52
53
54
# File 'lib/glabssms/configuration.rb', line 52

def logger
  @logger ||= self.class._default_logger
end