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.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/smess.rb', line 57

def initialize
  @nothing = false
  @default_output = nil
  @default_sender_id = "Smess"
  @output_types = %i{auto card_board_fish clickatell global_mouth iconectiv mblox smsglobal twilio}
  @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.



55
56
57
# File 'lib/smess.rb', line 55

def configured_outputs
  @configured_outputs
end

#default_outputObject

Returns the value of attribute default_output.



55
56
57
# File 'lib/smess.rb', line 55

def default_output
  @default_output
end

#default_sender_idObject

Returns the value of attribute default_sender_id.



55
56
57
# File 'lib/smess.rb', line 55

def default_sender_id
  @default_sender_id
end

#nothingObject

Returns the value of attribute nothing.



55
56
57
# File 'lib/smess.rb', line 55

def nothing
  @nothing
end

#output_by_country_codeObject

Returns the value of attribute output_by_country_code.



55
56
57
# File 'lib/smess.rb', line 55

def output_by_country_code
  @output_by_country_code
end

#output_typesObject

Returns the value of attribute output_types.



55
56
57
# File 'lib/smess.rb', line 55

def output_types
  @output_types
end

Instance Method Details

#add_country_code(cc, output = default_output) ⇒ Object

Raises:

  • (ArgumentError)


77
78
79
80
81
82
# File 'lib/smess.rb', line 77

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



103
104
105
# File 'lib/smess.rb', line 103

def country_codes
  output_by_country_code.keys
end

#outputsObject



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

def outputs
  configured_outputs.keys
end

#register_output(options) ⇒ Object

Raises:

  • (ArgumentError)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/smess.rb', line 84

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