Class: Sms

Inherits:
Object
  • Object
show all
Includes:
AfricasTalking
Defined in:
lib/AfricasTalking/Sms.rb

Constant Summary collapse

HTTP_CREATED =
201
HTTP_OK =
200

Constants included from AfricasTalking

AfricasTalking::DEBUG, AfricasTalking::VERSION

Instance Method Summary collapse

Constructor Details

#initialize(username, apikey) ⇒ Sms

Set debug flag to to true to view response body



9
10
11
12
# File 'lib/AfricasTalking/Sms.rb', line 9

def initialize username, apikey
	@username    = username
	@apikey      = apikey
end

Instance Method Details

#createSubcription(options) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/AfricasTalking/Sms.rb', line 141

def createSubcription options
	post_body = {
					'username'    => @username,
					'phoneNumber' => options['phoneNumber'],
					'shortCode'   => options['shortCode'],
					'keyword'     => options['keyword']
				}
	if options['checkoutToken'] != nil
		post_body['checkoutToken'] = options['checkoutToken']
	end
	url = getSmsSubscriptionUrl() + "/create"
	if validateParamsPresence?(options, ['shortCode', 'keyword', 'phoneNumber'])
		response = sendNormalRequest(url, post_body)
	end
	if(@response_code == HTTP_CREATED)
		r = JSON.parse(response, :quirky_mode => true)
		return CreateSubscriptionResponse.new r['status'], r['description'] 
	else
		raise AfricasTalkingException, response
	end
end

#deleteSubcription(options) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/AfricasTalking/Sms.rb', line 163

def deleteSubcription options
	post_body = {
					'username'    => @username,
					'phoneNumber' => options['phoneNumber'],
					'shortCode'   => options['shortCode'],
					'keyword'     => options['keyword']
				}
	if options['checkoutToken'] != nil
		post_body['checkoutToken'] = options['checkoutToken']
	end
	url = getSmsSubscriptionUrl() + "/delete"
	if validateParamsPresence?(options, ['shortCode', 'keyword', 'phoneNumber'])
		response = sendNormalRequest(url, post_body)
	end
	if(@response_code == HTTP_CREATED)
		r = JSON.parse(response, :quirky_mode => true)
		return DeleteSubscriptionResponse.new r['status'], r['description'] 
	else
		raise AfricasTalkingException, response
	end
end

#fetchMessages(options) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/AfricasTalking/Sms.rb', line 108

def fetchMessages options
	url = getSmsUrl() + "?username=#{@username}&lastReceivedId=#{options['last_received_id']}"
	response = sendNormalRequest(url)
	if @response_code == HTTP_OK
		messages = JSON.parse(response, :quirky_mode => true)["SMSMessageData"]["Messages"].collect { |msg|
			SMSMessages.new msg["id"], msg["text"], msg["from"] , msg["to"], msg["linkId"], msg["date"]
		}
			# messages

		return FetchMessagesResponse.new messages

	else
		raise AfricasTalkingException, response
	end
end

#fetchSubscriptions(options) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/AfricasTalking/Sms.rb', line 124

def fetchSubscriptions options
	if validateParamsPresence?(options, ['shortCode', 'keyword'])
		url = getSmsSubscriptionUrl() + "?username=#{@username}&shortCode=#{options['shortCode']}&keyword=#{options['keyword']}&lastReceivedId=#{options['lastReceivedId']}"
		response = sendNormalRequest(url)
	end
	if(@response_code == HTTP_OK)
		# 
		subscriptions = JSON.parse(response)['responses'].collect{ |subscriber|
			PremiumSubscriptionNumbers.new subscriber['phoneNumber'], subscriber['id'], subscriber['date']
		}
		# 
		return subscriptions
	else
		raise AfricasTalkingException, response
	end
end

#send(options) ⇒ Object



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
# File 'lib/AfricasTalking/Sms.rb', line 19

def send options
	# 
	post_body = {

		'username'    => @username, 
		'message'     => options['message'], 
		'to'          => options['to']
	}	
	if options['from'] != nil
		post_body['from'] = options['from']
	end
	if options['enqueue'] === true
		post_body['enqueue'] = 1
	end
	if options['bulkSMSMode'] != nil
		post_body['bulkSMSMode'] = options['bulkSMSMode']
	end
	if options['retryDurationInHours'] != nil
		post_body['retryDurationInHours'] = options['retryDurationInHours']
	end
	# 
	if validateParamsPresence?(options, ['message', 'to'])
		response = sendNormalRequest(getSmsUrl(), post_body)
	end
	if @response_code == HTTP_CREATED
		messageData = JSON.parse(response,:quirks_mode=>true)["SMSMessageData"]
		recipients = messageData["Recipients"]
		
		if recipients.length > 0
			reports = recipients.collect { |entry|
				StatusReport.new entry["number"], entry["status"], entry["cost"], entry["messageId"]
			}
			# 
			return reports
		end
		
		raise AfricasTalkingException, messageData["Message"]
		
	else
		raise AfricasTalkingException, response
	end
end

#sendPremium(options) ⇒ Object



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
# File 'lib/AfricasTalking/Sms.rb', line 62

def sendPremium options
	post_body = {
		'username'    => @username, 
		'message'     => options['message'], 
		'to'          => options['to'],
		'keyword'     => options['keyword'],
		'linkId'      => options['linkId'],
	}
	if options['retryDurationInHours'] != nil
		post_body['retryDurationInHours'] = options['retryDurationInHours']
	end
	if options['bulkSMSMode'] != nil
		post_body['bulkSMSMode'] = options['bulkSMSMode']
	end
	if options['enqueue'] != nil
		post_body['enqueue'] = options['enqueue']
	end
	if options['from'] != nil
		post_body['from'] = options['from']
	end
	# 
	if validateParamsPresence?(options, ['message', 'to', 'keyword', 'linkId'])
		response = sendNormalRequest(getSmsUrl(), post_body)
	end
	
	# 
	if @response_code == HTTP_CREATED
		messageData = JSON.parse(response,:quirks_mode=>true)["SMSMessageData"]
		recipients = messageData["Recipients"]
		
		if recipients.length > 0
			reports = recipients.collect { |entry|
				StatusReport.new entry["number"], entry["status"], entry["cost"], entry["messageId"]
			}
			return SendPremiumMessagesResponse.new reports, messageData["Message"]
		end
		
		raise AfricasTalkingException, messageData["Message"]
		
	else
		raise AfricasTalkingException, response
	end

	# 
end