Class: Sently::Sms
- Inherits:
-
Object
- Object
- Sently::Sms
- Defined in:
- lib/sently/sms.rb
Defined Under Namespace
Classes: SentlySMSException
Class Attribute Summary collapse
-
.configuration ⇒ Object
Returns the value of attribute configuration.
-
.sender ⇒ Object
Returns the value of attribute sender.
Instance Attribute Summary collapse
-
#hid ⇒ Object
Returns the value of attribute hid.
-
#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 = "", hid = "") ⇒ Sms
constructor
A new instance of Sms.
- #is_number_valid?(number) ⇒ Boolean
Constructor Details
#initialize(number = "", message = "", hid = "") ⇒ Sms
Returns a new instance of Sms.
22 23 24 25 26 |
# File 'lib/sently/sms.rb', line 22 def initialize(number = "", = "", hid = "") @number = number @message = @hid = hid end |
Class Attribute Details
.configuration ⇒ Object
Returns the value of attribute configuration.
6 7 8 |
# File 'lib/sently/sms.rb', line 6 def configuration @configuration end |
.sender ⇒ Object
Returns the value of attribute sender.
7 8 9 |
# File 'lib/sently/sms.rb', line 7 def sender @sender end |
Instance Attribute Details
#hid ⇒ Object
Returns the value of attribute hid.
3 4 5 |
# File 'lib/sently/sms.rb', line 3 def hid @hid end |
#message ⇒ Object
Returns the value of attribute message.
3 4 5 |
# File 'lib/sently/sms.rb', line 3 def @message end |
#number ⇒ Object
Returns the value of attribute number.
3 4 5 |
# File 'lib/sently/sms.rb', line 3 def number @number end |
Class Method Details
.configure {|configuration| ... } ⇒ Object
9 10 11 12 13 |
# File 'lib/sently/sms.rb', line 9 def configure self.configuration ||= Configuration.new yield(configuration) self.sender = Sender.new(configuration) end |
Instance Method Details
#deliver_sms ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/sently/sms.rb', line 28 def deliver_sms #raise SentlySMSException.new("Invalid message") unless is_message_valid?(@message) params = { :text => @message.to_s, :to => format_number(@number), :hid => @hid } if production_environment? begin response = sender.get(configuration.sms_uri, params) rescue Exception => e response = RestClient::Response.create("Error:#{e.}", "", {}) end else response = RestClient::Response.create('Id:12345', "", {}) end parse(response.to_s) rescue SentlySMSException => exception raise exception end |
#format_number(number) ⇒ Object
48 49 50 51 |
# File 'lib/sently/sms.rb', line 48 def format_number(number) formatted = number.gsub(/[^0-9+]/, '') return is_number_valid?(formatted) ? formatted : (raise SentlySMSException.new("Phone number (#{number}) is not formatted correctly")) end |
#is_number_valid?(number) ⇒ Boolean
53 54 55 |
# File 'lib/sently/sms.rb', line 53 def is_number_valid?(number) number.length >= 4 && number[/^.\d+$/] end |