lib/smess.rb in smess-1.0.10 vs lib/smess.rb in smess-2.0
- old
+ new
@@ -7,60 +7,101 @@
require 'active_support'
require 'active_support/core_ext'
require "smess/version"
require 'smess/logging'
-require 'smess/country_code_registry'
+require 'smess/output'
require 'smess/utils'
require 'smess/sms'
require 'smess/outputs/http_base'
require 'smess/outputs/auto'
require 'smess/outputs/ipx'
require 'smess/outputs/ipxus'
require 'smess/outputs/card_board_fish'
require 'smess/outputs/clickatell'
-require 'smess/outputs/etisalatdemo'
require 'smess/outputs/smsglobal'
require 'smess/outputs/global_mouth'
require 'smess/outputs/mblox'
require 'smess/outputs/twilio'
require 'smess/outputs/iconectiv'
require 'smess/outputs/test'
-require 'string'
+require 'string_ext'
module Smess
- # Move to config?
- OUTPUTS = %w{auto card_board_fish clickatell etisalatdemo global_mouth iconectiv mblox ipxus smsglobal twilio}
- COUNTRY_CODES = [1, 20, 212, 33, 34, 44, 46, 49, 966, 971]
-
def self.new(*args)
Sms.new(*args)
end
+ def self.named_output_instance(name)
+ output_class_name = config.configured_outputs.fetch(name)[:type].to_s.camelize
+ conf = config.configured_outputs[name][:config]
+ "Smess::#{output_class_name}".constantize.new(conf)
+ end
+
def self.config
- @config ||=Config.new
+ @config ||= Config.new
end
+ def self.reset_config
+ @config = Config.new
+ end
+
+ def self.configure
+ yield(config)
+ end
+
class Config
+ attr_accessor :nothing, :default_output, :default_sender_id, :default_sender_id, :output_types, :configured_outputs, :output_by_country_code
+
def initialize
- @config=Hash.new
+ @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 = {test: {type: :test, config: nil}}
+ @output_by_country_code = {}
+
+ register_output({
+ name: :auto,
+ country_codes: [],
+ type: :auto,
+ config: {}
+ })
end
- def method_missing(method,*args,&block)
- method = method.to_s.gsub(/[=]/,'')
- if args.length>0
- @config[method] = args.first
+
+ 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
+
+ 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
- @config[method]
end
- end
+ def outputs
+ configured_outputs.keys
+ end
+
+ def country_codes
+ output_by_country_code.keys
+ end
+
+ end
end
# httpclient does not send basic auth correctly, or at all.
HTTPI.adapter = :net_http
-
-# Setting config defaults
-# there is probably a better way and better place
-# Unused and deprecated
-Smess.config.debug = Smess.booleanize(ENV["SMESS_DEBUG"])