Ruby class DidwwAPI

From open.didww.com

This is the approved revision of this page, as well as being the most recent.
Jump to: navigation, search

Contents

Before you start

In order to use DIDWW API with the following code example, you need to install a SOAP client library Savon.

It can be downloaded from here

Savon can be installed via:

$ gem install savon

Class DidwwAPI (source code)

Please include this class in your ruby application or just place in app/models/ folder for Ruby on Rails application.

class DidwwAPI

  # Constructor
  def initialize(api_username = nil, api_key = nil, test_mode = nil)    
    @api_username = 'your@username.com'
    @api_key = "YOURAPIKEY"
    @auth_string = auth_string
 
    @test_mode = test_mode.nil? ? @settings.test_mode : test_mode
    Savon::Response.raise_errors = false
 
    @api_url = (@test_mode == true || @test_mode == "true") ? "http://sandbox2.didww.com/api2/?wsdl" : "http://api.didww.com/api2/?wsdl"
     @client = Savon::Client.new @api_url
  end
 
  attr_accessor :api_username, :api_key, :api_url    
 
  def auth_string
    require 'digest/sha1'
    Digest::SHA1.hexdigest(@api_username + @api_key + "sandbox")
  end
 
  # didww_getdidwwregions
  def didww_getdidwwregions(country_iso = nil, city_prefix = nil, last_request_gmt = nil)
    @response = @client.didww_getdidwwregions do |soap|
      soap.body = { 
        :auth_string => @auth_string,
        :country_iso => country_iso,
        :city_prefix => city_prefix,
        :last_request_gmt => last_request_gmt        
      }
    end
    return @response.to_hash[:didww_getdidwwregions_response][:return]
  end
 
  def didww_getdidwwpstnrates(country_iso = nil, pstn_prefix = nil, last_request_gmt = nil)
    @response = @client.didww_getdidwwpstnrates do |soap|
      soap.body = { 
        :auth_string => @auth_string,
        :country_iso => country_iso,
        :pstn_prefix => pstn_prefix,
        :last_request_gmt => last_request_gmt
      }
    end
    return @response    
  end
    
 def didww_updatepstnrates(rates=nil)
    @response = @client.didww_updatepstnrates do |soap|
      soap.body = {
        :auth_string=>@auth_string,
        :rates=> rates
      }
    end
    @response
  end 
 
 def didww_checkpstnnumber(pstn_number = nil)
    @response = @client.didww_checkpstnnumber do |soap|
      soap.body = { 
        :auth_string => @auth_string,
        :pstn_number => pstn_number
      }
    end
    #return @response.soap_fault? ? @response.soap_fault : @response.to_s
    return @response
  end
 
 def didww_ordercreate(customer_id = nil, country_iso = nil, city_prefix = nil, period = nil, map_data = nil, prepaid_funds = nil)
    require 'digest/sha1'
 
    uniq_hash = Digest::SHA1.hexdigest(Time.now.to_i.to_s + @auth_string)
 
    @response = @client.didww_ordercreate do |soap|
      soap.body = { 
        :auth_string => @auth_string,
        :customer_id  => customer_id,
        :country_iso => country_iso,
        :city_prefix => city_prefix,
        :period => period,
        :map_data => map_data,
        :prepaid_funds => prepaid_funds,
        :uniq_hash => uniq_hash
      }
    end
    return @response
  end
 
 def didww_ordercancel(customer_id = nil, did_number = nil)
    @response = @client.didww_ordercancel do |soap|
      soap.body = { 
        :auth_string => @auth_string,
        :customer_id => customer_id,
        :did_number => did_number
      }
    end
    return @response    
  end
 
 def  didww_orderautorenew(customer_id, did_number, period, uniq_hash = nil)
    if(uniq_hash.nil?)
      require 'digest/sha1'
      uniq_hash = Digest::SHA1.hexdigest(Time.now.to_i.to_s + @auth_string)
    end
    @response = @client.didww_orderautorenew do |soap|
      soap.body = {
          :auth_string => @auth_string,
          :customer_id => customer_id,
          :did_number => did_number,
          :period => period,
          :uniq_hash => uniq_hash
      }
    end
    @response
  end
 
 def didww_updatemapping(customer_id = nil, did_number = nil, map_data = nil)
    @response = @client.didww_updatemapping do |soap|
      soap.body = { 
        :auth_string => @auth_string,
        :customer_id => customer_id,
        :did_number => did_number,
        :map_data => map_data
      }
    end
    return @response    
  end
 
 def didww_getprepaidbalance(customer_id = nil)
    @response = @client.didww_getprepaidbalance do |soap|
      soap.body = {
        :auth_string=> @auth_string,
        :customer_id => customer_id
      }
    end
    @response
  end
 
  def didww_updateprepaidbalance(customer_id , prepaid_funds , operation_type ,uniq_hash  = nil)
    if(uniq_hash.nil?)
      require 'digest/sha1'
      uniq_hash = Digest::SHA1.hexdigest(Time.now.to_i.to_s + @auth_string) 
    end
 
    @response = @client.didww_updateprepaidbalance do |soap|
      soap.body = {
        :auth_string => @auth_string,
        :customer_id=> customer_id,
        :prepaid_funds => prepaid_funds,
        :operation_type => operation_type,
        :uniq_hash => uniq_hash
      }
    end
    @response
  end
 
 def didww_getfullcdrlog(customer_id = nil, last_requested_date  = nil)
    @response = @client.didww_getfullcdrlog do |soap|
      soap.body = { 
        :auth_string => @auth_string,
        :customer_id => customer_id,
        :last_requested_date => last_requested_date
      }
    end
    return @response
  end  
  
def didww_getdidwwapidetails
    @response = @client.didww_getdidwwapidetails do |soap|
      soap.body = { :auth_string => @auth_string }
    end
    return @response.soap_fault? ? @response.soap_fault : @response.to_hash[:didww_getdidwwapidetails_response][:return]
  end 
end

                  
def didww_getservicedetails(customer_id = nil, api_order_id=nil)
    @response = @client.didww_getservicedetails do |soap|
      soap.body = { 
        :auth_string => @auth_string,
        :customer_id => customer_id,
        :api_order_id => api_order_id
      }
    end
    return @response
  end    
Personal tools
Namespaces
Variants
Actions
Main Menu
DIDWW API
Open Source
Open Projects
User contributions
Toolbox