-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAfricasTalkingGateway.rb
106 lines (83 loc) · 2.83 KB
/
AfricasTalkingGateway.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
require 'curb'
require 'json'
class AfricasTalkingGatewayAuthenticationError < Exception
end
class AfricasTalkingGatewayUnexpectedError < Exception
end
class SMSMessage
attr_accessor :id, :text, :from, :date
def initialize(m_id, m_text,m_from, m_to ,m_date)
@id = m_id
@text = m_text
@from = m_from
@to = m_to
@date = m_date
end
end
class MessageStatusReport
attr_accessor :message, :total_number, :number_successful, :total_cost, :status_reports
def initialize(json_text)
json = JSON.parse(json_text)
status_text = json["SMSMessageData"]["Message"]
@total_cost = status_text.split(/KES /)[1].to_f
@number_successful = status_text.split(/\//)[0].split(/Sent to /)[1].to_i
@total_number = status_text.split(/\//)[1].split(/ Total/)[0].to_i
@message = status_text
@status_reports = json["SMSMessageData"]["Recipients"].collect { |status|
StatusReport.new(status["number"], status["status"], status["cost"])
}
end
end
class StatusReport
attr_accessor :number, :status, :cost
def initialize(m_number, m_status, m_cost)
@number = m_number
@status = m_status
@cost = m_cost
end
end
class AfricasTalkingGateway
#Constants
URL = 'https://api.africastalking.com/version1/messaging'
ACCEPT_TYPE = 'application/json'
def initialize(user_name,api_key)
@user_name = user_name
@api_key = api_key
end
def send_message(recipients, message)
data = nil
response_code = nil
post_body = {:username => @user_name, :message => message, :to => recipients }
http = Curl.post(URL, post_body) do |curl|
curl.headers['Accept'] = ACCEPT_TYPE
curl.headers['apiKey'] = @api_key
curl.verbose = true
curl.on_body { |body|
data = body
body.to_s.length
}
curl.on_complete { |resp| response_code = resp.response_code }
end
raise AfricasTalkingGatewayAuthenticationError if response_code == 401
raise AfricasTalkingGatewayUnexpectedError "Unexpected error occured" if response_code != 201 or data.nil?
end
def fetch_messages(last_received_id)
data = nil
response_code = nil
http = Curl.get("#{URL}?username=#{@user_name}&lastReceivedId=#{last_received_id}") do |curl|
curl.headers['Accept'] = ACCEPT_TYPE
curl.headers['apiKey'] = @api_key
curl.verbose = false
curl.on_body { |body|
data = body
body.to_s.length
}
curl.on_complete { |resp| response_code = resp.response_code }
end
raise AfricasTalkingGatewayAuthenticationError if response_code == 401
raise AfricasTalkingGatewayUnexpectedError "Data is nil for some unexpected reason" if response_code != 200 or data.nil?
messages = JSON.parse(data)["SMSMessageData"]["Messages"].collect { |msg|
SMSMessage.new msg["id"], msg["text"], msg["from"] , msg["to"], msg["date"]
}
end
end