In this tips I am going to introduce you to web service API call. I am using http://webservicex.net/
“WebserviceX.NET provides programmable business logic components and standing data that serve as “black boxes” to provide access to functionality and data via web services”.
I am going to use one of its web service which is “Currency Convertor “ and wsdl schema location is http://www.webservicex.net/CurrencyConvertor.asmx?WSDL We have a very good gem for SOAP API call in Ruby on Rails which is Savon. I am going to use it and show you how to use it.
First let’s create a new rails application and name it CurrencyConverter (or any other name you like). Then add this “gem ‘savon’ “ to gem file and bundle it.
Create a controller with name public with index and create action and model CurrencyConverter.
Code for controller is below and it is self explanatory.
class PublicsController < ApplicationController
  #purpose: for showing the home page for the application
  def index
  end
 #purpose: for calculating the currency convertion rate
# params : params[:fromCurrency],params[:toCurrency]
# return :  convertion rate
  def create
    # creating a object of the CurrencyConverter model
    currency = CurrencyConverter.new(params[:fromCurrency],params[:toCurrency])
    render :json => currency.result
  end
end
Code for model is below
class CurrencyConverter
  attr_reader :result
  # purpose :  for initializing the object of currency converter
  # params : fromCurrecny(unit),toCurrency(unit)
  # return : none
  def initialize(fromCurrency , toCurrency)
    # creating a client from the wsdl schema
    client = Savon::Client.new("http://webservicex.net/currencyconvertor.asmx?wsdl")
    # calling the api with fromCurrecny and toCurrency unit
    response = client.request :web, :conversion_rate, body: {
                       "FromCurrency" =>     fromCurrency , "ToCurrency" => toCurrency
                      }
    #checking for success of api call
    if response.success?
      data = response.to_array(:conversion_rate_response).first
      if data
        # setting convertion rate to result   
        @result = data[:conversion_rate_result]
      end
    end
  end
end
The index.html.erb view is like
     From Currency Unit
     
          Select Currency
 
British Pound
Indian Rupee
Pakistani Rupee
Singapore Dollar
South African Rand
U.S. Dollar
               
  
 
  To Currency Unit
 
          Select Currency
British Pound
Indian Rupee
Pakistani Rupee
Singapore Dollar
South African Rand
U.S. Dollar
  
  
  
I have used ajax call for submitting this fromCurrency And toCurrency unit.The code for it is
$(document).ready(function(){
    var rate = 0.0;
    var fromCurrency = $("#from_currency");
    var toCurrency= $("#to_currency");
    var fromAmount = $("#from_amount");
    var toAmount = $("#to_amount");
    var  button = $(“# get_conversion_rate”);
 
    var getConversionRate = function(){
        $.post('/publics',{
            fromCurrency : fromCurrency.val(),
            toCurrency : toCurrency.val()
          },function(data) {
            rate = data;
            toAmount.val(fromAmount.val()*rate);
       });
    };
var initialize = function(){
button.click(getConversionRate);
 };
initializer();
 
});