lib/smess/outputs/smsglobal.rb in smess-1.0.2 vs lib/smess/outputs/smsglobal.rb in smess-1.0.3
- old
+ new
@@ -1,72 +1,54 @@
require 'uri'
require 'httpi'
module Smess
- class Smsglobal
- include Smess::Logging
+ class Smsglobal < HttpBase
- def deliver_sms(sms_arg)
- return false unless sms_arg.kind_of? Sms
- @sms = sms_arg
-
+ def deliver
request.url = url
request.body = params
begin
- HTTPI.log_level = :debug
response = HTTPI.post request
result = normal_result(response)
rescue Exception => e
- puts logger.warn response
+ logger.warn response
# connection problem or some error
result = result_for_error(e)
end
result
end
private
- attr_reader :sms
+ def username
+ ENV["SMESS_SMSGLOBAL_USER"]
+ end
+ def password
+ ENV["SMESS_SMSGLOBAL_PASS"]
+ end
+ def sender_id
+ ENV["SMESS_SMSGLOBAL_SENDER_ID"]
+ end
def url
"https://www.smsglobal.com/http-api.php"
end
- def from
- sms.originator || ENV["SMESS_SMSGLOBAL_SENDER_ID"]
- end
-
def params
@params ||= {
action: "sendsms",
- user: ENV["SMESS_SMSGLOBAL_USER"],
- password: ENV["SMESS_SMSGLOBAL_PASS"],
+ user: username,
+ password: password,
from: from,
to: sms.to,
text: sms.message.strip_nongsm_chars,
maxsplit: "3"
}
end
- def request
- @request ||= HTTPI::Request.new
- end
-
- def result_for_error(e)
- code = e.code rescue "-1"
- {
- response_code: '-1',
- response: {
- temporaryError: 'true',
- responseCode: e.code,
- responseText: e.message
- },
- data: result_data
- }
- end
-
def normal_result(response)
first_response = response.body.split(/\r\n/).first.split(";")
response_code = first_response.first.split(':').last.to_i
message_id = first_response.last.split('SMSGlobalMsgID:').last
# Successful response
@@ -74,17 +56,9 @@
message_id: message_id,
response_code: response_code.to_s,
response: response.body,
destination_address: sms.to,
data: result_data
- }
- end
-
- def result_data
- {
- to: sms.to,
- text: sms.message.strip_nongsm_chars,
- from: from
}
end
end
end
\ No newline at end of file