Class: Airtime

Inherits:
Object
  • Object
show all
Includes:
AfricasTalking
Defined in:
lib/AfricasTalking/Airtime.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) ⇒ Airtime

Set debug flag to to true to view response body



7
8
9
10
# File 'lib/AfricasTalking/Airtime.rb', line 7

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

Instance Method Details

#send(options) ⇒ Object



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

def send options
  url      = getAirtimeUrl() + "/send"

  recipients = options.each{|item| 
    validateParamsPresence? item, %w(phoneNumber currencyCode amount)
    item['amount'].to_s.prepend(item['currencyCode'].to_s + " ") 
    item.delete('currencyCode')
  }
  post_body = {
        'username'   => @username,
        'recipients' => recipients.to_json,
      }
  response = sendNormalRequest(url, post_body) 
  if (@response_code == HTTP_CREATED)
    responses = JSON.parse(response, :quirky_mode =>true)
    if (responses['responses'].length > 0)
      # 
      results = responses['responses'].collect{ |result|
        # 
        AirtimeResponse.new result['status'], result['phoneNumber'],result['amount'],result['requestId'], result['errorMessage'], result['discount']
      }
      # 
      return SendAirtimeResult.new responses["errorMessage"], responses["numSent"], responses["totalAmount"], responses["totalDiscount"], results
    else
      raise AfricasTalkingException, responses['errorMessage']
    end
    raise AfricasTalkingException, response
  end
end