Class: Moonshado::Sms
- Inherits:
-
Object
- Object
- Moonshado::Sms
- Defined in:
- lib/moonshado/sms.rb,
lib/moonshado/keywords.rb
Defined Under Namespace
Classes: Keywords, MoonshadoSMSException
Class Attribute Summary collapse
-
.configuration ⇒ Object
Returns the value of attribute configuration.
-
.sender ⇒ Object
Returns the value of attribute sender.
Instance Attribute Summary collapse
-
#message ⇒ Object
Returns the value of attribute message.
-
#number ⇒ Object
Returns the value of attribute number.
Class Method Summary collapse
Instance Method Summary collapse
- #deliver_sms ⇒ Object
- #format_number(number) ⇒ Object
-
#initialize(number = "", message = "") ⇒ Sms
constructor
A new instance of Sms.
- #is_message_valid?(message) ⇒ Boolean
- #is_number_valid?(number) ⇒ Boolean
Constructor Details
#initialize(number = "", message = "") ⇒ Sms
Returns a new instance of Sms.
29 30 31 32 |
# File 'lib/moonshado/sms.rb', line 29 def initialize(number = "", = "") @number = number @message = end |
Class Attribute Details
.configuration ⇒ Object
Returns the value of attribute configuration.
6 7 8 |
# File 'lib/moonshado/sms.rb', line 6 def configuration @configuration end |
.sender ⇒ Object
Returns the value of attribute sender.
7 8 9 |
# File 'lib/moonshado/sms.rb', line 7 def sender @sender end |
Instance Attribute Details
#message ⇒ Object
Returns the value of attribute message.
3 4 5 |
# File 'lib/moonshado/sms.rb', line 3 def @message end |
#number ⇒ Object
Returns the value of attribute number.
3 4 5 |
# File 'lib/moonshado/sms.rb', line 3 def number @number end |
Class Method Details
.configure {|configuration| ... } ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/moonshado/sms.rb', line 9 def configure self.configuration ||= Configuration.new yield(configuration) self.sender = Sender.new(configuration) if configuration.auto_register_keywords begin Moonshado::Sms::Keywords.register_keywords rescue Exception => e puts "Failed to auto register keywords: #{e.}" end end end |
.find(id) ⇒ Object
22 23 24 25 26 |
# File 'lib/moonshado/sms.rb', line 22 def find(id) response = sender.get(configuration.sms_uri + "/#{id}") JSON.parse(response.to_s) end |
Instance Method Details
#deliver_sms ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/moonshado/sms.rb', line 34 def deliver_sms raise MoonshadoSMSException.new("Invalid message") unless (@message) data = {:sms => {:device_address => format_number(@number), :message => @message.to_s}} if production_environment? begin response = sender.send_to_moonshado(data, configuration.sms_uri) rescue Exception => e response = RestClient::Response.create("{\"stat\":\"fail\",\"error\":\"#{e.}\"}", "", {}) end else response = RestClient::Response.create('{"stat":"ok","id":"sms_id_mock"}', "", {}) end parse(response.to_s) rescue MoonshadoSMSException => exception raise exception end |
#format_number(number) ⇒ Object
54 55 56 57 |
# File 'lib/moonshado/sms.rb', line 54 def format_number(number) formatted = number.scan(/\d+/i).join return is_number_valid?(formatted) ? formatted : (raise MoonshadoSMSException.new("Phone number (#{number}) is not formatted correctly")) end |
#is_message_valid?(message) ⇒ Boolean
63 64 65 66 67 68 69 |
# File 'lib/moonshado/sms.rb', line 63 def () if .include?(.to_s.size) else true end end |
#is_number_valid?(number) ⇒ Boolean
59 60 61 |
# File 'lib/moonshado/sms.rb', line 59 def is_number_valid?(number) number.length >= 10 && number[/^.\d+$/] end |