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
- .default_account ⇒ Object
- .settings=(settings = []) ⇒ Object
-
.status(id) ⇒ Object
> SMS status check response codes: “ok” “Запрос успешно обработан” “error-system” “Произошла системная ошибка” “error-provider-id-unknown” “Сообщение с таким идентификатором не найдено”.
Instance Method Summary collapse
-
#initialize(data) ⇒ 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(data) ⇒ 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 36 37 38 39 40 41 42 |
# File 'lib/mfms/sms.rb', line 13 def initialize(data) @phone = data.fetch(:phone, nil) @subject = data.fetch(:subject, nil) @message = data.fetch(:message, nil) @status = 'not-sent' account_name = data[:account] account_variable = if account_name.present? var_name = "@@#{account_name}".to_sym if self.class.class_variables.include?(var_name) var_name else raise ArgumentError, "Account with name '#{account_name}' is not defined" end else self.class.default_account end account_settings = self.class.class_variable_get(account_variable) @login = account_settings[:login] @connector = account_settings[:connector] @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 = data.fetch(:translit, account_settings[:translit]) @priority = data.fetch(:priority, account_settings[:priority]) @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
.default_account ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/mfms/sms.rb', line 59 def self.default_account default_account = nil Array.wrap(class_variables).each do |account| if class_variable_get(account.to_s).has_key?(:default) default_account = account break end end default_account || raise(ArgumentError, 'One of the accounts should be specified by default') end |
.settings=(settings = []) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/mfms/sms.rb', line 44 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.fetch(:ssl, true) account_settings[:translit] = account_settings.fetch(:translit, false) account_settings[:additional_args] = account_settings.fetch(:additional_args, nil) account_settings[:priority] = account_settings.fetch(:priority, nil) if settings_valid?(account_settings) self.class_variable_set("@@#{account}", account_settings) end end end |
.status(id) ⇒ Object
> SMS status check response codes:
"ok" "Запрос успешно обработан"
"error-system" "Произошла системная ошибка"
"error-provider-id-unknown" "Сообщение с таким идентификатором не найдено"
109 110 111 112 113 114 115 116 |
# File 'lib/mfms/sms.rb', line 109 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" "Данный отправителть не разрешен на нашей платформе"
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/mfms/sms.rb', line 78 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
118 119 120 121 122 123 |
# File 'lib/mfms/sms.rb', line 118 def update_status return @status if @id.nil? code, status = self.class.status(@id) return code unless code == 'ok' @status = status end |
#validate! ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/mfms/sms.rb', line 125 def validate! raise ArgumentError, "Phone should be assigned to #{self.class}." if @phone.nil? unless @phone =~ /^[0-9]{10,}$/ raise ArgumentError, 'Phone number should contain only numbers. Minimum'+ "length is 10. #{@phone.inspect} is given." end raise ArgumentError, "Subject should be assigned to #{self.class}." if @subject.nil? raise ArgumentError, "Message should be assigned to #{self.class}." if @message.nil? if @priority && !%w(low normal high realtime).include?(@priority) raise ArgumentError, 'Priority is not valid.' end end |