Class: RingCentral::Phone

Inherits:
Object
  • Object
show all
Defined in:
lib/ringcentral.rb

Constant Summary collapse

PATH =
'ringout.asp'
URL =
[RingCentral::URL, PATH].join('/')
SuccessResponse =
'OK'
STATUS_CODES =
{
  0 => 'Success',          # picked up, line open (gets set for the callback number before the destination)
  1 => 'In Progress',      # ringing (or waiting to be rung if it's the destination number)
  2 => 'Busy',             # appears in the "general call status" field after call has completed
  3 => 'No Answer',
  4 => 'Rejected',         # party hung up, line closed
  5 => 'Generic Error',
  6 => 'Finished',         # other party hung up, line closed
  7 => 'International calls disabled',
  8 => 'Destination number prohibited'
}

Class Method Summary collapse

Class Method Details

.call(username, password, extension, to, from, caller_id, prompt = 1) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ringcentral.rb', line 77

def self.call(username, password, extension, to, from, caller_id, prompt = 1)
  
  params = {
    :cmd => :call,
    :to => to,
    :from => from,
    :clid => caller_id,
    :prompt => prompt
  }
  response = RestClient.post(URL, params.merge(RingCentral.credentials_hash(username, password, extension)))
  body = response.body
  
  if body[0..1] == SuccessResponse # sucessful responses start with "OK "
    session_id, ws = body[3..-1].split(' ')
    return { :session_id => session_id, :ws => ws }
  else
    raise "RingCentral response: #{body}"
  end
end

.cancel(session_id, ws = nil) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ringcentral.rb', line 135

def self.cancel(session_id, ws = nil)
  
  params = {
    :cmd => :cancel,
    :sessionid => session_id,
    :ws => ws
  }
  response = RestClient.post(URL, params)
  body = response.body
  
  if body[0..1] == SuccessResponse # sucessful responses start with "OK "
    session_id = body[3..-1]
    return { :session_id => session_id }
  else
    raise "RingCentral response: #{body}"
  end
end

.list(username, password, extension) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ringcentral.rb', line 62

def self.list(username, password, extension)
  
  params = { :cmd => :list }
  response = RestClient.post(URL, params.merge(RingCentral.credentials_hash(username, password, extension)))
  body = response.body
  
  if body[0..1] == SuccessResponse # sucessful responses start with "OK "
    data = body[3..-1]
    return Hash[*data.split(';')].invert
  else
    raise "RingCentral response: #{body}"
  end
end

.status(session_id, ws = nil) ⇒ Object

Notes:

- WS param doesn't seem to do anything or even be required
- API always gives the "completed call" response regardless of whether the session ID is valid
- while call is running (and for a few seconds after it is disconnected), status string with both callback 
  and destination number are given, with statuses for both; after that, only the session ID is given


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ringcentral.rb', line 103

def self.status(session_id, ws = nil)
  
  params = {
    :cmd => :status,
    :sessionid => session_id,
    :ws => ws
  }
  response = RestClient.post(URL, params)
  body = response.body
  
  if body[0..1] == SuccessResponse # sucessful responses start with "OK "
    session_id, statuses = body[3..-1].split(' ')
    if statuses
      statuses = statuses.split(';')
      return {
        :general     => STATUS_CODES[statuses[0].to_i],
        :destination => STATUS_CODES[statuses[2].to_i],
        :callback    => STATUS_CODES[statuses[4].to_i]
      }
    else
      return {
        :general     => "Call Completed",
        :destination => "Call Completed",
        :callback    => "Call Completed"
      }
    end
  else
    raise "RingCentral response: #{body}"
  end
end