Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Updated using 4PSA automated script
Excerpt

This page contains code snippets that demonstrate various Unified API requests that can be implemented in your App when using the Ruby language. Download.

Table of Contents
maxLevel2

Requirements

You need Ruby 1.9.3 + with SSL Support in order to run these examples.

...

or higher with SSL support is required.

Note
titleThe code is on GitHub

Download here. Do not hesitate to contribute in order to make this example better - we welcome Pull Requests!

Make a call between two public numbers

Code Block
languageruby
require "net/http"
require "json"
 
# Modify these values with the ones you intend to use
VN_SERVER_ADDRESS   = "CHANGEME"
API_ACCESS_TOKEN 	= "CHANGEME"
EXTENSION_NUMBER    = "CHANGEME"
PUBLIC_NUMBER1      = "CHANGEME"
PUBLIC_NUMBER2 		= "CHANGEME"

# URI that identifies the phone call
uri = URI("https://"+VN_SERVER_ADDRESS+"/uapi/phoneCalls/@me/simple")

# The parameters sent in the body of the request
data = {"extension" => EXTENSION_NUMBER,
        "phoneCallView" => [{
              "source" => PUBLIC_NUMBER1, 
              "destination" => PUBLIC_NUMBER2
       }]
}
 
# Initializes the request by setting the HTTP headers, query and body parameters and the URI path
req = Net::HTTP::Post.new(uri.path)
req.body = JSON.dump(data)
req['Authorization'] = 'Bearer ' + API_ACCESS_TOKEN
req['Content-type'] = 'application/json'

# Makes a HTTP POST request using SSL
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
  http.request(req)
end
 
# Parses the JSON response
objectResult = JSON.parse(res.body)
puts JSON.pretty_generate(objectResult)

...

curl "https://raw.githubusercontent.com/4psa/uapi-example-ruby/master/01_public_call.rb" -o 01_public_call.rb -L

Make a Sandbox call

Code Block
languageruby
require "net/http"
require "json"
 
# Modify these values with the ones you intend to use
VN_SERVER_ADDRESS 	= "CHANGEME"
API_ACCESS_TOKEN 	= "CHANGEME"
EXTENSION_NUMBER    = "CHANGEME"
SANDBOX_NUMBER      = "CHANGEME"

# URI that identifies the phone call
uri = URI("https://"+VN_SERVER_ADDRESS+"/uapi/phoneCalls/@me/simple")

# The parameters sent in the body of the request
data = {"extension" => EXTENSION_NUMBER,
			"phoneCallView" => [{
				"source" => EXTENSION_NUMBER, 
				"destination" => SANDBOX_NUMBER
		}]
}
 
# Initializes the request by setting the HTTP headers, query and body parameters and the URI path
req = Net::HTTP::Post.new(uri.path)
req.body = JSON.dump(data)
req['Authorization'] = 'Bearer ' + API_ACCESS_TOKEN
req['Content-type'] = 'application/json'
  
# Makes a HTTP POST request using SSL
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
  http.request(req)
end
 
# Parses the JSON response
objectResult = JSON.parse(res.body)
puts JSON.pretty_generate(objectResult)

...

curl "https://raw.githubusercontent.com/4psa/uapi-example-ruby/master/02_sandbox_call.rb" -o 02_sandbox_call.rb -L

Park a phone call

Anchor
demo-ph-park
demo-ph-park

The example below demonstrates how you can use Unified API to park a party of an ongoing phone call. It uses the Park PhoneCalls request.

Code Block
languageruby
require "net/http"
require "json"

# Modify these values with the ones you intend to use
VN_SERVER_ADDRESS 	= "CHANGEME"
API_ACCESS_TOKEN 	= "CHANGEME"
PHONECALL_ID 		= "CHANGEME"
PHONECALLVIEW_ID	= "CHANGEME"
EXTENSION_NUMBER	= "CHANGEME"

# URI that identifies the phone call
uri = URI("https://"+VN_SERVER_ADDRESS+"/uapi/phoneCalls/@me/"+ EXTENSION_NUMBER +"/"+PHONECALL_ID)

# The parameters sent in the body of the request
data = {
    "action" => "Park",
    "phoneCallViewId" => PHONECALLVIEW_ID
}
# Initializes the request by setting the HTTP headers, query and body parameters and the URI path
req = Net::HTTP::Put.new(uri.path)
req.body = JSON.dump(data)
req['Authorization'] = 'Bearer ' + API_ACCESS_TOKEN
req['Content-type'] = 'application/json'
  
# Makes a HTTP PUT request using SSL
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
  http.request(req)
end

# Parses the JSON response
objectResult = JSON.parse(res.body)
puts JSON.pretty_generate(objectResult)

...

curl "https://raw.githubusercontent.com/4psa/uapi-example-ruby/master/03_park_call.rb" -o 03_park_call.rb -L

Transfer a call to a public number

Anchor
demo-ph-xfer
demo-ph-xfer

The example below demonstrates how you can use Unified API to transfer a call to a public number, not registered with VoipNow. It uses the Transfer PhoneCalls request.

Code Block
languageruby
require "net/http"
require "json"

# Modify these values with the ones you intend to use
VN_SERVER_ADDRESS 	        = "CHANGEME"
API_ACCESS_TOKEN 	        = "CHANGEME"
PHONECALL_ID 		        = "CHANGEME"
PHONECALLVIEW_ID	        = "CHANGEME"
TRANSFER_DESTINATION_NUMBER = "CHANGEME"

# URI that identifies the phone call
uri = URI(curl "https://"+VN_SERVER_ADDRESS+"/uapi/phoneCalls/@me/@self/"+PHONECALL_ID)

# The parameters sent in the body of the request
data = {
    "action" => "Transfer",
    "sendCallTo" => TRANSFER_DESTINATION_NUMBER,
    "phoneCallViewId" => PHONECALLVIEW_ID
}

# Initializes the request by setting the HTTP headers, query and body parameters and the URI path
req = Net::HTTP::Put.new(uri.path)
req.body = JSON.dump(data)
req['Authorization'] = 'Bearer ' + API_ACCESS_TOKEN
req['Content-type'] = 'application/json'
  
# Makes a HTTP PUT request using SSL
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
  http.request(req)
end

# Parses the JSON response
objectResult = JSON.parse(res.body)
puts JSON.pretty_generate(objectResult)

...

raw.githubusercontent.com/4psa/uapi-example-ruby/master/04_transfer_call.rb" -o 04_transfer_call.rb -L

Record an ongoing conversation

Anchor
demo-ph-rec
demo-ph-rec

The example below demonstrates how you can use Unified API to record an ongoing conversation. This action is possible for phone numbers that are registered with VoipNow. The recording is saved in the wav format and uses the StartRecording PhoneCalls request.

Code Block
languageruby
require "net/http"
require "json"

# Modify these values with the ones you intend to use
VN_SERVER_ADDRESS    = "CHANGEME"
API_ACCESS_TOKEN     = "CHANGEME"
PHONECALL_ID         = "CHANGEME"
PHONECALLVIEW_ID     = "CHANGEME"

# URI that identifies the phone call
uri = URI("https://"+VN_SERVER_ADDRESS+"/uapi/phoneCalls/@me/@self/"+PHONECALL_ID)

# The parameters sent in the body of the request
data = {
    "action" => "StartRecording",
    "format" => "wav",
    "phoneCallViewId" => PHONECALLVIEW_ID
}

# Initializes the request by setting the HTTP headers, query and body parameters and the URI path
req = Net::HTTP::Put.new(uri.path)
req.body = JSON.dump(data)
req['Authorization'] = 'Bearer ' + API_ACCESS_TOKEN
req['Content-type'] = 'application/json'
  
# Makes a HTTP PUT request using SSL
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
  http.request(req)
end

# Parses the JSON response
objectResult = JSON.parse(res.body)
puts JSON.pretty_generate(objectResult)

...

curl "https://raw.githubusercontent.com/4psa/uapi-example-ruby/master/05_record_call.rb" -o 05_record_call.rb -L

Log in an agent to a queue

Anchor
demo-queue-ag
demo-queue-ag

The example below demonstrates how you can use Unified API to log in an agent to a queue. It uses the Update QueueAgents request.

Code Block
languageruby
require "net/http"
require "json"

# Modify these values with the ones you intend to use
VN_SERVER_ADDRESS   = "CHANGEME"
API_ACCESS_TOKEN    = "CHANGEME"
QUEUE_NUMBER        = "CHANGEME"
AGENT_NUMBER        = "CHANGEME"

# URI that identifies the queue agent
uri = URI("https://"+VN_SERVER_ADDRESS+"/uapi/extensions/@me/"+QUEUE_NUMBER+"/queue/agents/"+AGENT_NUMBER)

# The parameters sent in the body of the request
data = {"status" => "1"}

# Initializes the request by setting the HTTP headers, query and body parameters and the URI path
req = Net::HTTP::Put.new(uri.path)
req.body = JSON.dump(data)
req['Authorization'] = 'Bearer ' + API_ACCESS_TOKEN
req['Content-type'] = 'application/json'
 
# Makes a HTTP PUT request using SSL 
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
  http.request(req)
end

# Parses the JSON response
objectResult = JSON.parse(res.body)
puts JSON.pretty_generate(objectResult)

...

curl "https://raw.githubusercontent.com/4psa/uapi-example-ruby/master/06_agent_login.rb" -o 06_agent_login.rb -L

List the registration status of an extension

Anchor
demo-ext-pres
demo-ext-pres

The example below demonstrates how you can use Unified API to list the status of an extension. It uses the List Presence request.

Code Block
languageruby
require "net/http"
require "json"

# Modify these values with the ones you intend to use
VN_SERVER_ADDRESS   = "CHANGEME"
API_ACCESS_TOKEN    = "CHANGEME"
EXTENSION_NUMBER    = "CHANGEME"

# URI that identifies the presence of an extension
uri = URI("https://"+VN_SERVER_ADDRESS+"/uapi/extensions/@me/"+EXTENSION_NUMBER+"/presence")

# Initializes the request by setting the HTTP headers, query and body parameters and the URI path
req = Net::HTTP::Get.new(uri.path)
req['Authorization'] = 'Bearer ' + API_ACCESS_TOKEN
req['Content-type'] = 'application/json'

# Makes a HTTP GET request using SSL
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
  http.request(req)
end

# Parses the JSON response
objectResult = JSON.parse(res.body)
puts JSON.pretty_generate(objectResult)curl "https://raw.githubusercontent.com/4psa/uapi-example-ruby/master/07_list_registration.rb" -o 07_list_registration.rb -L

Except where otherwise noted, content in this space is licensed under a Creative Commons Attribution 4.0 International.