Class: Moonshado::Sms

Inherits:
Object
  • Object
show all
Defined in:
lib/moonshado/sms.rb,
lib/moonshado/keywords.rb

Defined Under Namespace

Classes: Keywords, MoonshadoSMSException

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



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

def configuration
  @configuration
end

.senderObject

Returns the value of attribute sender.



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

def sender
  @sender
end

Instance Attribute Details

#messageObject

Returns the value of attribute message.



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

def message
  @message
end

#numberObject

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

Yields:



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.message}"
    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_smsObject



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 is_message_valid?(@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.message}\"}", "", {})
    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

Returns:

  • (Boolean)


63
64
65
66
67
68
69
# File 'lib/moonshado/sms.rb', line 63

def is_message_valid?(message)
  if message_length_check?
    message_length_range.include?(message.to_s.size)
  else
    true
  end
end

#is_number_valid?(number) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/moonshado/sms.rb', line 59

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