Class: Sms
Constant Summary collapse
- HTTP_CREATED =
201- HTTP_OK =
200
Constants included from AfricasTalking
AfricasTalking::DEBUG, AfricasTalking::VERSION
Instance Method Summary collapse
- #createSubcription(options) ⇒ Object
- #deleteSubcription(options) ⇒ Object
- #fetchMessages(options) ⇒ Object
- #fetchSubscriptions(options) ⇒ Object
-
#initialize(username, apikey) ⇒ Sms
constructor
Set debug flag to to true to view response body.
- #send(options) ⇒ Object
- #sendPremium(options) ⇒ Object
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 post_body = { 'username' => @username, 'phoneNumber' => ['phoneNumber'], 'shortCode' => ['shortCode'], 'keyword' => ['keyword'] } if ['checkoutToken'] != nil post_body['checkoutToken'] = ['checkoutToken'] end url = getSmsSubscriptionUrl() + "/create" if validateParamsPresence?(, ['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 post_body = { 'username' => @username, 'phoneNumber' => ['phoneNumber'], 'shortCode' => ['shortCode'], 'keyword' => ['keyword'] } if ['checkoutToken'] != nil post_body['checkoutToken'] = ['checkoutToken'] end url = getSmsSubscriptionUrl() + "/delete" if validateParamsPresence?(, ['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 url = getSmsUrl() + "?username=#{@username}&lastReceivedId=#{['last_received_id']}" response = sendNormalRequest(url) if @response_code == HTTP_OK = 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 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 if validateParamsPresence?(, ['shortCode', 'keyword']) url = getSmsSubscriptionUrl() + "?username=#{@username}&shortCode=#{['shortCode']}&keyword=#{['keyword']}&lastReceivedId=#{['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 # post_body = { 'username' => @username, 'message' => ['message'], 'to' => ['to'] } if ['from'] != nil post_body['from'] = ['from'] end if ['enqueue'] === true post_body['enqueue'] = 1 end if ['bulkSMSMode'] != nil post_body['bulkSMSMode'] = ['bulkSMSMode'] end if ['retryDurationInHours'] != nil post_body['retryDurationInHours'] = ['retryDurationInHours'] end # if validateParamsPresence?(, ['message', 'to']) response = sendNormalRequest(getSmsUrl(), post_body) end if @response_code == HTTP_CREATED = JSON.parse(response,:quirks_mode=>true)["SMSMessageData"] recipients = ["Recipients"] if recipients.length > 0 reports = recipients.collect { |entry| StatusReport.new entry["number"], entry["status"], entry["cost"], entry["messageId"] } # return reports end raise AfricasTalkingException, ["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 post_body = { 'username' => @username, 'message' => ['message'], 'to' => ['to'], 'keyword' => ['keyword'], 'linkId' => ['linkId'], } if ['retryDurationInHours'] != nil post_body['retryDurationInHours'] = ['retryDurationInHours'] end if ['bulkSMSMode'] != nil post_body['bulkSMSMode'] = ['bulkSMSMode'] end if ['enqueue'] != nil post_body['enqueue'] = ['enqueue'] end if ['from'] != nil post_body['from'] = ['from'] end # if validateParamsPresence?(, ['message', 'to', 'keyword', 'linkId']) response = sendNormalRequest(getSmsUrl(), post_body) end # if @response_code == HTTP_CREATED = JSON.parse(response,:quirks_mode=>true)["SMSMessageData"] recipients = ["Recipients"] if recipients.length > 0 reports = recipients.collect { |entry| StatusReport.new entry["number"], entry["status"], entry["cost"], entry["messageId"] } return SendPremiumMessagesResponse.new reports, ["Message"] end raise AfricasTalkingException, ["Message"] else raise AfricasTalkingException, response end # end |