Вопрос

I am trying to integrate a currency converter into my Ruby on Rails application using the Savon gem to fetch data from http://www.webservicex.net/CurrencyConvertor.asmx?WSDL. I was following the tutorial at http://www.mindfiresolutions.com/How-to-use-Savon-in-Ruby-on-Rails-Application-2367.php but I am not able to send a request when I click the submit button. I have exactly followed what the tutorial is saying. this is my code.

i have one controller,


class ConverterController < ApplicationController
  def index
  end

  def show
  end

  def create
    # creating a object of the CurrencyConverter model
    currency = CurrencyConverter.new(params[:fromCurrency],params[:toCurrency])
    render :json => currency.result
  end
end

model:


class CurrencyConverter < ActiveRecord::Base
  require 'savon'
    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

my view in index.html.erb


<h1>Public#index</h1>
<p>Find me in app/views/public/index.html.erb</p>

<div id="welcome">

    <label> From Currency Unit</label>
     <select name="from_currency" id="from_currency">
         <option value="">Select Currency</option>
        <option value="GBP">British Pound</option>
        <option value="INR">Indian Rupee</option>
        <option value="PKR">Pakistani Rupee</option>
        <option value="SGD">Singapore Dollar</option>
        <option value="ZAR">South African Rand</option>
        <option value="USD">U.S. Dollar</option>
    </select>
 <input type="text" id="from_amount" placeholder="Enter From Curreny Amount"/>
 <br>

 <label> To Currency Unit</label>
 <select name="to_currency" id="to_currency">
        <option value="">Select Currency</option>
        <option value="GBP">British Pound</option>
        <option value="INR">Indian Rupee</option>
        <option value="PKR">Pakistani Rupee</option>
        <option value="SGD">Singapore Dollar</option>
        <option value="ZAR">South African Rand</option>
        <option value="USD">U.S. Dollar</option>
  </select>
  <input type="text" readonly id="to_amount" placeholder="to Curreny Amount"/>
  <br>
  <input type="button" class="btn" id="get_conversion_rate" value="Get Conversion">

i am using a ajax call for submitting this fromCurrency And toCurrency unit. i placed the code under /assets/javascripts/CurrencyConverter.js then added the line <% javascript_include_tag :all %> in application.html.erb file to include them 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 initializer = function(){
            button.click(getConversionRate);
         };
        initializer();
});

please help.

Это было полезно?

Решение

Here you go. The smallest possible example, works for Savon v2.x:

require 'savon'

c=Savon.client(wsdl: "http://www.webservicex.net/CurrencyConvertor.asmx?WSDL")

r=c.call(:conversion_rate,
         message: {'FromCurrency' => 'EUR', 'ToCurrency' => 'CAD'})

print r.to_hash[:conversion_rate_response][:conversion_rate_result]
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top