Module: Smesser
- Defined in:
- lib/smesser.rb,
lib/smesser/version.rb,
lib/smesser/provider.rb
Defined Under Namespace
Classes: Provider
Constant Summary collapse
- VERSION =
"0.0.2"
Class Method Summary collapse
- .config_files ⇒ Object
- .configuration ⇒ Object
- .load_config! ⇒ Object
- .log ⇒ Object
- .lookup_contacts(recipients) ⇒ Object
- .print_providers(io = STDOUT) ⇒ Object
- .providers ⇒ Object
-
.send_message(options = {}) ⇒ Object
The easiest way to send an SMS.
Class Method Details
.config_files ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/smesser.rb', line 9 def self.config_files @configfiles ||= [ "/etc/smesserrc", "/usr/local/etc/smesserrc", "~/.smesserrc" ] end |
.configuration ⇒ Object
83 84 85 |
# File 'lib/smesser.rb', line 83 def self.configuration @configuration ||= load_config! end |
.load_config! ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/smesser.rb', line 92 def self.load_config! config = {} config_files.each do |f| if File.exists?(path = File.(f)) config.merge!(YAML.load(File.read(path))) end end config.dup.each { |k, v| config[k.to_sym] = v if k.is_a?(String) } config end |
.log ⇒ Object
76 77 78 79 80 81 |
# File 'lib/smesser.rb', line 76 def self.log return @log if @log @log = Logger.new(STDOUT) @log.level = Logger::WARN @log end |
.lookup_contacts(recipients) ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/smesser.rb', line 103 def self.lookup_contacts(recipients) contacts = configuration[:contacts] return recipients unless contacts recipients.map do |r| log.debug "#{r} => #{contacts[r]}" if contacts[r] contacts[r] || r end end |
.print_providers(io = STDOUT) ⇒ Object
112 113 114 115 116 |
# File 'lib/smesser.rb', line 112 def self.print_providers(io = STDOUT) providers.each do |k, v| io.puts "#{k}: #{v.description || 'No description'}" end end |
.providers ⇒ Object
87 88 89 90 |
# File 'lib/smesser.rb', line 87 def self.providers Provider.load_all @providers ||= {} end |
.send_message(options = {}) ⇒ Object
The easiest way to send an SMS.
Accepts the following options:
- provider
-
The name of the provider to use (see ‘providers`)
- username
-
The username to log in with
- password
-
The password to log in with
- message
-
The message
- recipients
-
An array of recipients. If Smesser has ‘contacts` configured, then you can use aliases here.
- retry
-
The number of times to attempt sending. Default is 1.
If any are missing, Smesser’s configuration will be checked for a suitable value.
Returns a hash, with (at least)
- status
-
‘OK’ if all went well, ‘Failed’ otherwise
…and any additional provider specific return values. A common one might be ‘:remaining`, to indicate the number of free SMSs left with the provider.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/smesser.rb', line 38 def self.( = {}) .dup.each { |k, v| [k.to_sym] = .delete(k) if k.is_a?(String) } args = configuration.merge() log.debug("Sending message: args = #{args.inspect}") provider = providers[args[:provider]].new(args[:username], args[:password]) log.debug("Provider: #{provider.inspect}") recipients = args[:recipients].is_a?(String) ? [args[:recipients]] : args[:recipients] recipients = lookup_contacts(recipients) log.debug("Recipients: #{recipients.inspect}") unless provider.logged_in? log.debug("Logging in... (#{args[:username]})") provider.login unless args[:dryrun] end log.info("Message: #{args[:message].inspect}") result = {} if args[:dryrun] or provider.send(args[:message], *recipients) log.info "Message sent." result[:code] = "OK" result[:remaining] = provider.remaining if provider.respond_to?(:remaining) elsif args[:retry] and args[:retry] > 0 log.info "Failed, trying again... (#{args[:retry]})" args[:retry] -= 1 return (args) else log.info "Failed!" result[:code] = "Failed" end result end |