Class: Sently::Sms

Inherits:
Object
  • Object
show all
Defined in:
lib/sently/sms.rb

Defined Under Namespace

Classes: SentlySMSException

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 = "", message = "", hid = "")
  @number = number
  @message = message
  @hid = hid
end

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



6
7
8
# File 'lib/sently/sms.rb', line 6

def configuration
  @configuration
end

.senderObject

Returns the value of attribute sender.



7
8
9
# File 'lib/sently/sms.rb', line 7

def sender
  @sender
end

Instance Attribute Details

#hidObject

Returns the value of attribute hid.



3
4
5
# File 'lib/sently/sms.rb', line 3

def hid
  @hid
end

#messageObject

Returns the value of attribute message.



3
4
5
# File 'lib/sently/sms.rb', line 3

def message
  @message
end

#numberObject

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

Yields:



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_smsObject



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.message}", "", {})
    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

Returns:

  • (Boolean)


53
54
55
# File 'lib/sently/sms.rb', line 53

def is_number_valid?(number)
  number.length >= 4 && number[/^.\d+$/]
end