Class: Mfms::SMS
- Inherits:
-
Object
- Object
- Mfms::SMS
- Defined in:
- lib/mfms/sms.rb
Instance Attribute Summary collapse
-
#account ⇒ Object
Returns the value of attribute account.
-
#cert ⇒ Object
Returns the value of attribute cert.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#login ⇒ Object
Returns the value of attribute login.
-
#message ⇒ Object
Returns the value of attribute message.
-
#password ⇒ Object
Returns the value of attribute password.
-
#phone ⇒ Object
Returns the value of attribute phone.
-
#port ⇒ Object
Returns the value of attribute port.
-
#server ⇒ Object
Returns the value of attribute server.
-
#ssl ⇒ Object
Returns the value of attribute ssl.
-
#ssl_port ⇒ Object
Returns the value of attribute ssl_port.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#subject ⇒ Object
Returns the value of attribute subject.
Class Method Summary collapse
- .settings=(settings = []) ⇒ Object
-
.status(id) ⇒ Object
> SMS status check response codes: “ok” “Запрос успешно обработан” “error-system” “Произошла системная ошибка” “error-provider-id-unknown” “Сообщение с таким идентификатором не найдено”.
Instance Method Summary collapse
-
#initialize(phone, subject, message, translit = nil, account = nil) ⇒ SMS
constructor
A new instance of SMS.
-
#send ⇒ Object
> SMS send status codes: “ok” “Сообщения приняты на отправку” “error-system” “При обработке данного сообщения произошла системная ошибка” “error-address-format” “Ошибка формата адреса” “error-address-unknown” “Отправка по данному направлению не разрешена” “error-subject-format” “Ошибка формата отправителя” “error-subject-unknown” “Данный отправителть не разрешен на нашей платформе”.
- #update_status ⇒ Object
- #validate! ⇒ Object
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, , translit = nil, account = nil) @phone = phone @subject = subject @message = @status = 'not-sent' account = "@@#{account}".to_sym account_variable = if account.present? && self.class.class_variables.include?(account) account else self.class.class_variables.select{|sym| sym.to_s.include?('revoup0')}.first end account_settings = Mfms::SMS.class_variable_get(account_variable) @login = account_settings[:login] @password = account_settings[:password] @ssl = account_settings[:ssl] @ssl_port = account_settings[:ssl_port] @port = account_settings[:port] @cert = account_settings[:cert] @server = account_settings[:server] @translit = translit.nil? ? account_settings[:translit] : translit @errors = [] validate! end |
Instance Attribute Details
#account ⇒ Object
Returns the value of attribute account.
10 11 12 |
# File 'lib/mfms/sms.rb', line 10 def account @account end |
#cert ⇒ Object
Returns the value of attribute cert.
10 11 12 |
# File 'lib/mfms/sms.rb', line 10 def cert @cert end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
11 12 13 |
# File 'lib/mfms/sms.rb', line 11 def errors @errors end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
11 12 13 |
# File 'lib/mfms/sms.rb', line 11 def id @id end |
#login ⇒ Object
Returns the value of attribute login.
10 11 12 |
# File 'lib/mfms/sms.rb', line 10 def login @login end |
#message ⇒ Object
Returns the value of attribute message.
10 11 12 |
# File 'lib/mfms/sms.rb', line 10 def @message end |
#password ⇒ Object
Returns the value of attribute password.
10 11 12 |
# File 'lib/mfms/sms.rb', line 10 def password @password end |
#phone ⇒ Object
Returns the value of attribute phone.
10 11 12 |
# File 'lib/mfms/sms.rb', line 10 def phone @phone end |
#port ⇒ Object
Returns the value of attribute port.
10 11 12 |
# File 'lib/mfms/sms.rb', line 10 def port @port end |
#server ⇒ Object
Returns the value of attribute server.
10 11 12 |
# File 'lib/mfms/sms.rb', line 10 def server @server end |
#ssl ⇒ Object
Returns the value of attribute ssl.
10 11 12 |
# File 'lib/mfms/sms.rb', line 10 def ssl @ssl end |
#ssl_port ⇒ Object
Returns the value of attribute ssl_port.
10 11 12 |
# File 'lib/mfms/sms.rb', line 10 def ssl_port @ssl_port end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
11 12 13 |
# File 'lib/mfms/sms.rb', line 11 def status @status end |
#subject ⇒ Object
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| account = setting.keys.first account_settings = setting[account] account_settings[:cert] = init_cert_store(account_settings[:cert]) account_settings[:ssl] = account_settings[:ssl].presence || true account_settings[:translit] = account_settings[:translit].presence || false class_variable_set("@@#{account}", account_settings) validate_settings!(account_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
#send ⇒ Object
> 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_status ⇒ Object
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
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 |