Page tree

Versions Compared

Key

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

Excerpt

This example written in Ruby allows users to manage VoipNow accounts and fetch call statistics. Download.

Table of Contents
typelist

Note
titleThe code is on GitHub

Download here. Do not hesitate to contribute in order to make this example better - we welcome Pull Requests!This demonstrative code SHOULD NOT be used in production. It is designed to show how a client application interacts with the VoipNow SystemAPI. From this perspective, validations and error-checks aiming to demonstrate the most common mistakes are minimal and can be done easily.

How To Install It

System Requirements

...

STEP 1: Start by downloading the example files:

Code Block
git clone https://github.com/4psa/systemapi-example-ruby.git

 

STEP 2: Install gcc for Linux.

...

gcc should come with your operating system, otherwise simply run one of the commands below, depending on your Linux distribution.. For CentOS/RHEL:

Code Block
yum install gcc

For Debian/Ubuntu:

Code Block
apt-get install gcc

...

The following example shows how to add a new service provider. It also fetches the list of charging plans in order to pick a random one to use with the new service provider:

Code Block
rubyruby
=begin
4PSA VoipNow SystemAPI Client for Ruby
Copyright (c) 2013, Rack-Soft (www.4psa.com). All rights reserved.
VoipNow is a Trademark of Rack-Soft, Inc
4PSA is a Registered Trademark of Rack-Soft, Inc.
All rights reserved.
This script adds a service provider.
=end

require 'rubygems'
require 'securerandom'
gem 'soap4r'
require 'VoIpNowServiceDriver.rb'
require 'soap/header/simplehandler'

# Custom header for authentication
class AuthHeader < SOAP::Header::SimpleHandler

  # the namespace for the header data
  NAMESPACE = 'http://4psa.com/HeaderData.xsd/3.0.0'

  # Authentication data
  ACCESS_TOKEN = 'CHANGEME'

  #initializes an instance of this class
  def initialize()
    super(XSD::QName.new(NAMESPACE, 'userCredentials'))
  end

  #sets the user credentials with the authentication data
  def on_simple_outbound
    {"userCredentials" => {"accessToken" => ACCESS_TOKEN}}
  end

end

# We need a charging plan for the new account, so we make a request to
# fetch all the charging plans and then pick a random one from the response list.
driver = BillingInterface.new
driver.options['protocol.http.ssl_config.verify_mode'] = OpenSSL::SSL::VERIFY_NONE

# Add custom header
driver.headerhandler << AuthHeader.new

#the GetChargingPlans message
messagePart = GetChargingPlans.new

# Log for SystemAPI request and response
driver.wiredump_file_base = "log"

#send the SystemAPI message
chargingPlans = driver.getChargingPlans(messagePart)

# Get the id of a random charging plan
chargingPlanID = nil

if chargingPlans.chargingPlan != nil
	if chargingPlans.chargingPlan.length != 0
		randChargingPlan = chargingPlans.chargingPlan[rand(chargingPlans.chargingPlan.length)]
		if randChargingPlan.iD != nil
			chargingPlanID = randChargingPlan.iD
			if randChargingPlan.name != nil
				puts "Using charging plan " + randChargingPlan.name + "."
			else
				puts "Using charging plan with id " + chargingPlanID + "."
			end
		end
	end
end

driver = ServiceProviderInterface.new
driver.options['protocol.http.ssl_config.verify_mode'] = OpenSSL::SSL::VERIFY_NONE

# Add custom header
driver.headerhandler << AuthHeader.new

#the AddServiceProvider message
messagePart = AddServiceProvider.new

# Fill in ServiceProvider data
messagePart.name = 'SPRuby' + rand(1000).to_s
messagePart.login = 'SPRuby' + rand(1000).to_s
messagePart.password = SecureRandom.hex(16)
messagePart.country = 'us'
if chargingPlanID != nil
	messagePart.chargingPlanID = chargingPlanID
end

# Log for SystemAPI request and response
driver.wiredump_file_base = "log"

#send the SystemAPI message
begin
	driver.addServiceProvider(messagePart)
	puts "Service provider created successfully."
rescue Exception => ex
	# Catch exception, for situations when the service provider could not be added
	puts "Error: " + ex.message
end 

Add Other Account Types

Tip

These examples can be found in the downloaded package.

curl "https://raw.githubusercontent.com/4psa/systemapi-example-ruby/Demo/DemoAddServiceProvider.rb" -o DemoAddServiceProvider.rb -L

 

Add Other Account Types

If you wish to add other account types (organizations, users or extensions), there are only a few changes that you need to make to the previous Service Provider example. First of all, you need to change the type of driver object from ServiceProviderInterface to OrganizationInterface or UserInterface or ExtensionInterface. It should look like this:

...

The DEMO version also provides a CallCosts request example. Here is the code:

=begin 4PSA VoipNow SystemAPI Client for Ruby Copyright (c) 2013, Rack-Soft (www.4psa.com). All rights reserved. VoipNow is a Trademark of Rack-Soft, Inc 4PSA is a Registered Trademark of Rack-Soft, Inc. All rights reserved. This script fetches the call costs of an extension. =end require 'rubygems' require 'securerandom' gem 'soap4r' require 'VoIpNowServiceDriver.rb' require 'soap/header/simplehandler' # Custom header for authentication class AuthHeader < SOAP::Header::SimpleHandler # the namespace for the header data NAMESPACE = 'http://4psa.com/HeaderData.xsd/3.0.0' # authentication data ACCESS_TOKEN = 'CHANGEME' #initializes an instance of this class def initialize() super(XSD::QName.new(NAMESPACE, 'userCredentials')) end #sets the user credentials with the authentication data def on_simple_outbound {"userCredentials" => {"accessToken" => ACCESS_TOKEN}} end end # We need an extension to check its call costs, so we make a request to # fetch all the extensions. driver = ExtensionInterface.new driver.options['protocol.http.ssl_config.verify_mode'] = OpenSSL::SSL::VERIFY_NONE # Add custom header driver.headerhandler << AuthHeader.new #the getExtensions message messagePart = GetExtensions.new # Log for SystemAPI request and response driver.wiredump_file_base = "log" #send the SystemAPI message extensions = driver.getExtensions(messagePart) extensionIdentifier = nil if extensions.extension != nil if extensions.extension.length != 0 randExtension = extensions.extension[rand(extensions.extension.length)] if randExtension.identifier != nil extensionIdentifier = randExtension.identifier if randExtension.name != nil puts "Fetching call costs for extension " + randExtension.name + "." else puts "Fetching call costs for extension with identifier " + extensionIdentifier + "." end end end end if extensionIdentifier == nil puts "No extensions found on the server. Can not make the call costs request." exit end driver = ReportInterface.new driver.options['protocol.http.ssl_config.verify_mode'] = OpenSSL::SSL::VERIFY_NONE # Add custom header driver.headerhandler << AuthHeader.new #the CallCosts message messagePart = CallCosts.new # Fill in Request data messagePart.userIdentifier = extensionIdentifier interval = Interval.new interval.startDate = "2012-01-01" interval.endDate = DateTime.now.strftime('%Y-%m-%d') messagePart.interval = interval # Log for SystemAPI request and response driver.wiredump_file_base = "log" #send the SystemAPI message begin callCosts = driver.callCosts(messagePart) puts callCosts.totalCalls + " calls have been made between " + interval.startDate + " and " + interval.endDate + " with a total cost of " + callCosts.cost + " " + callCosts.currency rescue Exception => ex # Catch exception, for situations when the call costs could not be fetched puts "Error: " + ex.message end 
Code Block
languageruby
curl "https://raw.githubusercontent.com/4psa/systemapi-example-ruby/Demo/CallCosts.rb" -o CallCosts.rb -L

 

 

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