Class: Voice

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

Set debug flag to to true to view response body



8
9
10
11
# File 'lib/AfricasTalking/Voice.rb', line 8

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

Instance Method Details

#call(options) ⇒ Object



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

def call options
  if validateParamsPresence?(options, ['from', 'to'])
    post_body = {
      'username'              => @username,
      'from'                  => options['from'],
      'to'                    => options['to'],
      'clientRequestId'       => options['clientRequestId']
    }
    # 
    response = sendNormalRequest(getVoiceHost() + "/call", post_body)
  end
  if(@response_code == HTTP_OK || @response_code == HTTP_CREATED)
    ob = JSON.parse(response, :quirky_mode => true)
    # 
    if (ob['entries'].length > 0)
      results = ob['entries'].collect{|result|
        CallEntries.new result['status'], result['phoneNumber']
      }
    end
    return CallResponse.new results, ob['errorMessage']
    # 
  else
    raise AfricasTalkingException, response
  end
end

#fetchQueuedCalls(options) ⇒ Object



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

def fetchQueuedCalls options
  if validateParamsPresence?(options, ['phoneNumber'])
    post_body = {
      'username'    => @username,
      'phoneNumbers' => options['phoneNumber'],
    }

    url = getVoiceHost() + "/queueStatus"
    response = sendNormalRequest(url, post_body)
  end
  # 
  if(@response_code == HTTP_OK || @response_code == HTTP_CREATED)
    ob = JSON.parse(response, :quirky_mode => true)
    results = []
    if (ob['entries'].length > 0)
      results = ob['entries'].collect{|result|
        QueuedCalls.new result['phoneNumber'], result['numCalls'], result['queueName']
      }
    end
    # 
    return QueuedCallsResponse.new ob['status'], ob['errorMessage'], results
  end
  
  raise AfricasTalkingException, response
  
end

#uploadMediaFile(options) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/AfricasTalking/Voice.rb', line 66

def uploadMediaFile options
  if validateParamsPresence?(options, ['url','phoneNumber'])
    
    post_body = {
            'username' => @username,
            'url'      => options['url'],
            'phoneNumber' => options['phoneNumber']
          }
    url = getVoiceHost() + "/mediaUpload"
    # 
    response = sendNormalRequest(url, post_body)
  end
  if(@response_code == HTTP_OK || @response_code == HTTP_CREATED)
    return UploadMediaResponse.new response
  end
  # 
  raise AfricasTalkingException, response
end