Class: Mfms::SMS

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(phone, subject, message, translit = nil, account = nil) ⇒ SMS

Returns a new instance of SMS.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mfms/sms.rb', line 13

def initialize(phone, subject, message, translit = nil,  = nil)
  @phone = phone
  @subject = subject
  @message = message
  @status = 'not-sent'
   = "@@#{}".to_sym
   = if .present? && self.class.class_variables.include?()
                       
                     else
                       self.class.class_variables.select{|sym| sym.to_s.include?('revoup0')}.first
                     end
   = Mfms::SMS.class_variable_get()
  @login = [:login]
  @password = [:password]
  @ssl = [:ssl]
  @ssl_port = [:ssl_port]
  @port = [:port]
  @cert = [:cert]
  @server = [:server]
  @translit = translit.nil? ? [:translit] : translit
  @errors = []
  validate!
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



10
11
12
# File 'lib/mfms/sms.rb', line 10

def 
  @account
end

#certObject

Returns the value of attribute cert.



10
11
12
# File 'lib/mfms/sms.rb', line 10

def cert
  @cert
end

#errorsObject (readonly)

Returns the value of attribute errors.



11
12
13
# File 'lib/mfms/sms.rb', line 11

def errors
  @errors
end

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/mfms/sms.rb', line 11

def id
  @id
end

#loginObject

Returns the value of attribute login.



10
11
12
# File 'lib/mfms/sms.rb', line 10

def 
  @login
end

#messageObject

Returns the value of attribute message.



10
11
12
# File 'lib/mfms/sms.rb', line 10

def message
  @message
end

#passwordObject

Returns the value of attribute password.



10
11
12
# File 'lib/mfms/sms.rb', line 10

def password
  @password
end

#phoneObject

Returns the value of attribute phone.



10
11
12
# File 'lib/mfms/sms.rb', line 10

def phone
  @phone
end

#portObject

Returns the value of attribute port.



10
11
12
# File 'lib/mfms/sms.rb', line 10

def port
  @port
end

#serverObject

Returns the value of attribute server.



10
11
12
# File 'lib/mfms/sms.rb', line 10

def server
  @server
end

#sslObject

Returns the value of attribute ssl.



10
11
12
# File 'lib/mfms/sms.rb', line 10

def ssl
  @ssl
end

#ssl_portObject

Returns the value of attribute ssl_port.



10
11
12
# File 'lib/mfms/sms.rb', line 10

def ssl_port
  @ssl_port
end

#statusObject (readonly)

Returns the value of attribute status.



11
12
13
# File 'lib/mfms/sms.rb', line 11

def status
  @status
end

#subjectObject

Returns the value of attribute subject.



10
11
12
# File 'lib/mfms/sms.rb', line 10

def subject
  @subject
end

Class Method Details

.settings=(settings = []) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mfms/sms.rb', line 37

def self.settings=(settings=[])
  settings.each do |setting|
     = setting.keys.first
     = setting[]
    [:cert] = init_cert_store([:cert])
    [:ssl] = [:ssl].presence || true
    [:translit] = [:translit].presence || false
    class_variable_set("@@#{}", )
    validate_settings!()
  end
end

.status(id) ⇒ Object

> SMS status check response codes:

"ok"                           "Запрос успешно обработан"
"error-system"                 "Произошла системная ошибка"
"error-provider-id-unknown"    "Сообщение с таким идентификатором не найдено"


88
89
90
91
92
93
94
95
# File 'lib/mfms/sms.rb', line 88

def self.status(id)
  establish_connection.start do |http|
    request = Net::HTTP::Get.new(status_url id)
    response = http.request(request)
    body = response.body.split(';')
    return body[0], body[2] # code, status
  end
end

Instance Method Details

#sendObject

> SMS send status codes:

"ok"                     "Сообщения приняты на отправку"
"error-system"           "При обработке данного сообщения произошла системная ошибка"
"error-address-format"   "Ошибка формата адреса"
"error-address-unknown"  "Отправка по данному направлению не разрешена"
"error-subject-format"   "Ошибка формата отправителя"
"error-subject-unknown"  "Данный отправителть не разрешен на нашей платформе"


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mfms/sms.rb', line 57

def send
  #return stubbed_send if (defined?(Rails) && !Rails.env.production?)
  establish_connection.start do |http|
    request = Net::HTTP::Get.new(send_url)
    response = http.request(request)
    body = response.body.split(';')
    if body[0] == 'ok'
      @status = 'sent'
      @id = body[2]
      true
    else
      @errors << body[0]
      false
    end
  end
end

#update_statusObject



97
98
99
100
101
102
# File 'lib/mfms/sms.rb', line 97

def update_status
  return @status if @id.nil?
  code, status = self.class.status(@id)
  return code unless code == 'ok'
  @status = status
end

#validate!Object

Raises:

  • (ArgumentError)


104
105
106
107
108
109
# File 'lib/mfms/sms.rb', line 104

def validate!
  raise ArgumentError, "Phone should be assigned to #{self.class}." if @phone.nil?
  raise ArgumentError, "Phone number should contain only numbers. Minimum length is 10. #{@phone.inspect} is given." unless @phone =~ /^[0-9]{10,}$/
  raise ArgumentError, "Subject should be assigned to #{self.class}." if @subject.nil?
  raise ArgumentError, "Message should be assigned to #{self.class}." if @message.nil?
end