class Sms

Constants

HTTP_CREATED
HTTP_OK

Public Class Methods

new(username, apikey) click to toggle source

Set debug flag to to true to view response body

# File lib/AfricasTalking/Sms.rb, line 9
def initialize username, apikey
        @username    = username
        @apikey      = apikey
end

Public Instance Methods

createSubcription(options) click to toggle source
# File lib/AfricasTalking/Sms.rb, line 141
def createSubcription options
        post_body = {
                                        'username'    => @username,
                                        'phoneNumber' => options['phoneNumber'],
                                        'shortCode'   => options['shortCode'],
                                        'keyword'     => options['keyword']
                                }
        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) click to toggle source
# File lib/AfricasTalking/Sms.rb, line 160
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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

def initialize

super

end

# 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) click to toggle source
# 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

Private Instance Methods

getApiHost() click to toggle source
# File lib/AfricasTalking/Sms.rb, line 192
def getApiHost()
        if(@username == "sandbox")
                return "https://api.sandbox.africastalking.com"
        else
                return "https://api.africastalking.com"
        end
end
getSmsSubscriptionUrl() click to toggle source
# File lib/AfricasTalking/Sms.rb, line 188
def getSmsSubscriptionUrl()
        return getApiHost() + "/version1/subscription"
end
getSmsUrl() click to toggle source
# File lib/AfricasTalking/Sms.rb, line 184
def getSmsUrl()
        return  getApiHost() + "/version1/messaging"
end