Page tree

Versions Compared

Key

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

...

Note
titleThe code is on GitHub

Download here.Do If you want to improve this example, do not hesitate to contribute in order to make this example better - we . We welcome Pull Requests!.

How To Install It

System Requirements

In order to To be able to use the SystemAPI Ruby Tool, you need to comply with the requirements belowmust meet the following requirements:

  • 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:

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

 

STEP 2: Install gcc GCC for Linux.

Tipnote

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

gcc GCC should come with your operating system, otherwise . In case it doesn't, simply run one of the commands below.

For CentOS/RHEL:

Code Block
yum install gcc

For Debian/Ubuntu:

Code Block
apt-get install gcc

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

Note

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.

3.

On Linux:

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

Code Block
./configure
make
make install

On Windows:

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

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

Code Block
ruby setup.rb

STEP 5: You need to install   Then install SOAP4R, a Ruby library for accessing that allows you to access Web Services via SOAP. Here is the command line to install SOAP4Rfor it:

Code Block
gem install soap4r --include-dependencies
Note

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

Note

SOAP4R is not updated anymore. To ensure that SOAP4R works with the latest versions of Ruby, you must go to <PATH_TO_RUBY_GEMS>/soap and patch the following files:

./lib/xsd/charset.rb

./lib/xsd/codegen/gensupport.rb

./bin/wsdl2ruby.rb

./lib/xsd/xmlparser.rb.

The patches are in the same repository as the example files.

STEP 6: Install logger-application, a Ruby library which replaces Logger for the latest Ruby versions.

Code Block
gem install logger-application

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

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

On Linux:

Code Block
none
none
cd /home/your_user/schema
./wsdl2ruby.sh . <IP_ADDRESS

On Windows:

Code Block
none
none
cd C:\schema
wsdl2ruby.bat . <IP_ADDRESS>
Code Block
titleExample
cd C:\schema
wsdl2ruby.bat . voipnow2demo.4psa.com

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

Code Block
none
none
VoipNowService.rb
VoipNowServiceMappingRegistry.rb
AccountPortClient.rb
VoipNowServiceDriver.rb

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

Note

If you are using self-signed certificates, the script will fail.

In this case, you must go to <PATH_TO_RUBY_GEMS>/httpclient/lib/httpclient/ssl_socket.rb and change the value of variable verify_mode into OpenSSL::SSL::VERIFY_NONE

Note

You must move the files generated by the script to one of the Ruby folders $LOAD_PATH

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

How To Use It

Authentication

Depending on what Whatever you intend to do in the application, you will need to create an object. For example, if

STEP 1: If you want to add a new service provider, you should instantiate a new object , as follows:

Code Block
ruby
ruby
driver = ServiceProviderInterface.new

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

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

Code Block
ruby
ruby
driver.headerhandler << AuthHeader.new

Here is an explanation of how to you can 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 something like this:

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

  # the namespace for the header data
  NAMESPACE = 'http://4psa.com/HeaderData.xsd/3.05.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 In DemoAddServiceProvider.rb, you have an example on how to add to add a new service provider. It The script also fetches the list of charging plans in order to pick a random one to use with you can choose from and use for the new service provider:

...

 

Add Other Account Types

If you wish want to add other account types (organizations, users or extensions), there are only a few changes that you need to make to simply tweak the previous Service Provider example . First of allas detailed below.

STEP 1: Depending on the type of account you want to add, you need to change the type of driver object type from ServiceProviderInterface to:

  • OrganizationInterface

...

  • UserInterface

...

  • ExtensionInterface

 It . It should look like this:

Code Block
languageruby
driver = OrganizationInterface.new

Then, you need to adjust STEP 2: Adjust the message of the request . It should also contain a new parameter, the parentID, which represents either the and ensure it contains the parentIDDepending on the account type you want to add, this ID can be:  

  • The service provider that owns the organization (when adding a new organization)

...

  • The organization that owns the user (when adding a new user)

...

  •  
  • The user that owns the extension (when adding a new extension)

...

  •  
  • The extension for which the report is generated (when getting call costs)

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

Code Block
languageruby
# 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:Here is the code for the AddOrganization request.

Code Block
languageruby
#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.firstName = 'FirstnameRuby' + rand(1000).to_s
messagePart.lastName = 'LastnameRuby' + rand(1000).to_s
messagePart.email = 'Email' + rand(1000).to_s + '@example.com'
messagePart.password = SecureRandom.hex(16)
messagePart.country = 'us'
messagePart.company = 'test_company'
messagePart.parentID = serviceProviderID
if chargingPlanID != nil
	messagePart.chargingPlanID = chargingPlanID
end

FinallySTEP 3:  At this point, you need to call the proper method (the last line of the above script). Instead . To add an organization, instead of:

Code Block
languageruby
puts driver.addServiceProvider(messagePart)

You should have this for adding an organizationRun the following:

Code Block
languageruby
puts driver.addOrganization(messagePart)

Do the same Similar modifications are made for the other 2 two account types (user and extension)., User and Extension. 

CallCosts

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

...

.

...

 

 

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