Class: SmsFactor

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

Defined Under Namespace

Classes: Configuration, Init, SmsResponse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, recipients, sender = nil) ⇒ SmsFactor

Returns a new instance of SmsFactor.



7
8
9
10
11
12
13
# File 'lib/sms_factor.rb', line 7

def initialize(message, recipients, sender = nil)
  raise "The configuration is not complete. Please define api_url, api_login, api_password and api_default_from" unless SmsFactor::Init.configuration.is_valid?
  
  @sender      ||= SmsFactor::Init.configuration.api_default_from
  @recipients    = recipients.kind_of?(Array) ? recipients : [recipients]
  @message       = message
end

Instance Attribute Details

#messageObject

Returns the value of attribute message.



5
6
7
# File 'lib/sms_factor.rb', line 5

def message
  @message
end

#recipientsObject

Returns the value of attribute recipients.



5
6
7
# File 'lib/sms_factor.rb', line 5

def recipients
  @recipients
end

#senderObject

Returns the value of attribute sender.



5
6
7
# File 'lib/sms_factor.rb', line 5

def sender
  @sender
end

Class Method Details

.sms(message, recipients, sender = nil) ⇒ Object



57
58
59
60
# File 'lib/sms_factor.rb', line 57

def self.sms(message, recipients, sender = nil)
  sms = SmsFactor.new(message, recipients, sender)
  sms.deliver
end

Instance Method Details

#auth_credentialsObject



26
27
28
29
30
31
32
33
# File 'lib/sms_factor.rb', line 26

def auth_credentials
  @auth_credentials ||= {
                          authentication: {
                            username: SmsFactor::Init.configuration.,
                            password: SmsFactor::Init.configuration.api_password
                          }
                        }
end

#buildObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/sms_factor.rb', line 15

def build
  {
    sms: auth_credentials.merge(
      message: {text: @message, sender: @sender, pushtype: 'alert'},
        recipients: {
          gsm: @recipients.collect{|recipient| {value: recipient}}
        }
    )
  }
end

#deliver(delay: :now, check: false) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sms_factor.rb', line 35

def deliver(delay: :now, check: false)
  data  =   case delay
              when nil, :now
                build
              else
                build[:sms].merge!({delay: delay})
              end
              
  url   =   "#{SmsFactor::Init.configuration.api_url}/send"
  url   =   "#{url}/simulate" if check
  
  SmsFactor::SmsResponse.new(
    RestClient.post(
      url,
      { data: data.to_json },
      content_type: :json,
      accept:       :json,
      verify_ssl:   false
    )
  )
end