Class: Mfms::SMS
- Inherits:
-
Object
- Object
- Mfms::SMS
- Defined in:
- lib/mfms/sms.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#message ⇒ Object
Returns the value of attribute message.
-
#phone ⇒ Object
Returns the value of attribute phone.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#subject ⇒ Object
Returns the value of attribute subject.
Class Method Summary collapse
- .settings=(settings = {}) ⇒ Object
- .ssl ⇒ Object
- .ssl=(flag) ⇒ 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 = @@translit) ⇒ 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 = @@translit) ⇒ SMS
Returns a new instance of SMS.
13 14 15 16 17 18 19 20 21 |
# File 'lib/mfms/sms.rb', line 13 def initialize(phone, subject, , translit = @@translit) @phone = phone @subject = subject = @status = 'not-sent' @translit = translit validate! end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
11 12 13 |
# File 'lib/mfms/sms.rb', line 11 def id @id end |
#message ⇒ Object
Returns the value of attribute message.
10 11 12 |
# File 'lib/mfms/sms.rb', line 10 def end |
#phone ⇒ Object
Returns the value of attribute phone.
10 11 12 |
# File 'lib/mfms/sms.rb', line 10 def phone @phone 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
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/mfms/sms.rb', line 23 def self.settings=(settings={}) @@login = settings[:login] @@password = settings[:password] @@server = settings[:server] @@port = settings[:port] @@ssl_port = settings[:ssl_port] @@cert_store = init_cert_store settings[:cert] @@ssl = !settings[:ssl].nil? ? settings[:ssl] : true # connect using ssl by default @@translit = !settings[:translit].nil? ? settings[:translit] : false # use translit or not validate_settings! end |
.ssl ⇒ Object
39 40 41 |
# File 'lib/mfms/sms.rb', line 39 def self.ssl @@ssl end |
.ssl=(flag) ⇒ Object
35 36 37 |
# File 'lib/mfms/sms.rb', line 35 def self.ssl=(flag) @@ssl = flag end |
.status(id) ⇒ Object
> SMS status check response codes:
"ok" "Запрос успешно обработан"
"error-system" "Произошла системная ошибка"
"error-provider-id-unknown" "Сообщение с таким идентификатором не найдено"
77 78 79 80 81 82 83 84 |
# File 'lib/mfms/sms.rb', line 77 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" "Данный отправителть не разрешен на нашей платформе"
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mfms/sms.rb', line 51 def send #return stubbed_send if (defined?(Rails) && !Rails.env.production?) self.class.establish_connection.start do |http| request = Net::HTTP::Get.new(send_url) response = http.request(request) body = response.body.split(';') return body[0] unless body[0] == 'ok' @status = 'sent' @id = body[2] end end |
#update_status ⇒ Object
86 87 88 89 90 91 |
# File 'lib/mfms/sms.rb', line 86 def update_status return @status if @id.nil? code, status = self.class.status(@id) return code unless code == 'ok' @status = status end |
#validate! ⇒ Object
93 94 95 96 97 98 |
# File 'lib/mfms/sms.rb', line 93 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 .nil? end |