Page tree

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »

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

The code is on GitHub

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

How To Install It

System Requirements

In order to be able to use the SystemAPI Ruby Tool, you need to comply with the requirements below:

  • Download Ruby for Linux or Windows.
  • Download SOAP4R, a Ruby library that provides access the Web Services via SOAP.

Setup

Please follow these steps:

STEP 1: Start by downloading the example files:

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

 

STEP 2: Install gcc for Linux.

You may skip this step for Windows, since Ruby for Windows comes with an installation package.

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

yum install gcc

For Debian/Ubuntu:

apt-get install gcc

STEP 3: Install Ruby. You should use Ruby 1.8.6 or later for Linux and Ruby 1.8.7 or later for Windows.

It seems that Ruby 1.8.6 for Windows is not fully compatible with SOAP4R and throws some errors when running with our demo scripts. You may try it, but it's recommended that you use the installer specified above. In our demo, we used Ruby 1.8.6 for Linux and Ruby 1.8.7 for Windows.

On Linux, after download, you should extract the archive to a temporary folder, go to that folder and run:

./configure
make
make install

On Windows, just run the installer. You should check in the options to associate .rb files with Ruby and add Ruby to PATH.

STEP 4: You need rubygems in order to install SOAP4R. If rubygems was not installed with Ruby, you can download it from here. After downloading, extract the contents of the archive to a temporary folder, go to that folder and run:

ruby setup.rb

STEP 5: You need to install SOAP4R, a Ruby library for accessing Web Services via SOAP. Here is the command line to install SOAP4R:

gem install soap4r --include-dependencies

Although latest versions of Ruby come with SOAP4R in their standard library, it is highly recommended using the latest gem.

STEP 6: Create a directory for 4PSA VoipNow schemas on your local disk (for example, C:\schema or /home/your_user/schema).

STEP 7: Now you need to run the script that generates the Ruby library from the schema.

On Linux

cd /home/your_user/schema
./wsdl2ruby.sh . <IP_ADDRESS

On Windows

cd C:\schema
wsdl2ruby.bat . <IP_ADDRESS>
Example
cd C:\schema
wsdl2ruby.bat . voipnow2demo.4psa.com

This utility will generate four files inside the C:\schema folder:

VoipNowService.rb
VoipNowServiceMappingRegistry.rb
AccountPortClient.rb
VoipNowServiceDriver.rb

The main class used by your client program is VoipNowServiceDriver.rb.

STEP 8: At this point, you can start writing your applications for your VoipNow server.

How To Use It

Authentication

Depending on what you intend to do in the application, you will need to create an object. For example, if you want to add a new service provider, you should instantiate a new object, as follows:

driver = ServiceProviderInterface.new

You will need a custom header for the SystemAPI message, therefore you will create a new class called AuthHeader that will be customized for each authentication method. Please find it detailed below.

In order to add the new header to the recently created driver object, please write the following line of code:

driver.headerhandler << AuthHeader.new

Here is an explanation of how to create the AuthHeader class. The OAuth protocol requires that you to provide the access token generated for your application. In short, the code should look like this:

# 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

Examples

Add a Service Provider

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:

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:

driver = OrganizationInterface.new

Then, you need to adjust the message of the request. It should also contain a new parameter, the parentID, which represents either the service provider that owns the organization (when adding a new organization) or the organization that owns the user (when adding a new user) or the user that owns the extension (when adding a new extension).

The code for picking a random parent from the list of available accounts is presented below:

# We need a parent service provider for the new organization, so we make a request to
# fetch all the service providers.
driver = ServiceProviderInterface.new
driver.options['protocol.http.ssl_config.verify_mode'] = OpenSSL::SSL::VERIFY_NONE

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

#the getServiceProviders message
messagePart = GetServiceProviders.new

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

#send the SystemAPI message
serviceProviders = driver.getServiceProviders(messagePart)
serviceProviderID = nil

if serviceProviders.serviceProvider != nil
	if serviceProviders.serviceProvider.length != 0
		randServiceProvider = serviceProviders.serviceProvider[rand(serviceProviders.serviceProvider.length)]
		if randServiceProvider.iD != nil
			serviceProviderID = randServiceProvider.iD
			if randServiceProvider.name != nil
				puts "Using parent service provider " + randServiceProvider.name + "."
			else
				puts "Using parent service provider with id " + serviceProviderID + "."
			end
		end
	end
end

if serviceProviderID == nil
	puts "No service providers found on the server. Can not add an organization."
	exit
end

The code for the the AddOrganization request is shown below:

#the AddOrganization message
messagePart = AddOrganization.new
# Fill in Organization data
messagePart.name = 'OrgRuby' + rand(1000).to_s
messagePart.login = 'OrgRuby' + rand(1000).to_s
messagePart.password = SecureRandom.hex(16)
messagePart.country = 'us'
messagePart.parentID = serviceProviderID
if chargingPlanID != nil
	messagePart.chargingPlanID = chargingPlanID
end

Finally, you need to call the proper method (the last line of the above script). Instead of:

puts driver.addServiceProvider(messagePart)

You should have this for adding an organization:

puts driver.addOrganization(messagePart)

Similar modifications are made for the other 2 account types (user and extension).

CallCosts

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

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

 

 

#trackbackRdf ($trackbackUtils.getContentIdentifier($page) $page.title $trackbackUtils.getPingUrl($page))
  • No labels

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