Class: Smess::Config

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/smess.rb', line 53

def initialize
  @nothing = false
  @default_output = nil
  @default_sender_id = "Smess"
  @output_types = %i{auto card_board_fish clickatell global_mouth link_mobility smsglobal twilio twilio_whatsapp}
  @configured_outputs = {}
  @output_by_country_code = {}

  if ENV["RAILS_ENV"] == "test"
    @configured_outputs = {test: {type: :test, config: nil}}
  end

  register_output({
    name: :auto,
    country_codes: [],
    type: :auto,
    config: {}
  })
end

Instance Attribute Details

#configured_outputsObject

Returns the value of attribute configured_outputs.



51
52
53
# File 'lib/smess.rb', line 51

def configured_outputs
  @configured_outputs
end

#default_outputObject

Returns the value of attribute default_output.



51
52
53
# File 'lib/smess.rb', line 51

def default_output
  @default_output
end

#default_sender_idObject

Returns the value of attribute default_sender_id.



51
52
53
# File 'lib/smess.rb', line 51

def default_sender_id
  @default_sender_id
end

#nothingObject

Returns the value of attribute nothing.



51
52
53
# File 'lib/smess.rb', line 51

def nothing
  @nothing
end

#output_by_country_codeObject

Returns the value of attribute output_by_country_code.



51
52
53
# File 'lib/smess.rb', line 51

def output_by_country_code
  @output_by_country_code
end

#output_typesObject

Returns the value of attribute output_types.



51
52
53
# File 'lib/smess.rb', line 51

def output_types
  @output_types
end

Instance Method Details

#add_country_code(cc, output = default_output) ⇒ Object

Raises:

  • (ArgumentError)


73
74
75
76
77
78
# File 'lib/smess.rb', line 73

def add_country_code(cc, output=default_output)
  raise ArgumentError.new("Invalid country code") unless cc.to_i.to_s == cc.to_s
  raise ArgumentError.new("Unknown output specified") unless outputs.include? output.to_sym
  output_by_country_code[cc.to_s] = output.to_sym
  true
end

#country_codesObject



99
100
101
# File 'lib/smess.rb', line 99

def country_codes
  output_by_country_code.keys
end

#outputsObject



95
96
97
# File 'lib/smess.rb', line 95

def outputs
  configured_outputs.keys
end

#register_output(options) ⇒ Object

Raises:

  • (ArgumentError)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/smess.rb', line 80

def register_output(options)
  name = options.fetch(:name).to_sym
  type = options.fetch(:type).to_sym
  countries = options.fetch(:country_codes)
  config = options.fetch(:config)

  raise ArgumentError.new("Duplicate output name") if outputs.include? name
  raise ArgumentError.new("Unknown output type specified") unless output_types.include? type

  configured_outputs[name] = {type: type, config: config}
  countries.each do |cc|
    add_country_code(cc, name)
  end
end