外汇-人民币即时报价 WEB 服务
http://webservice.webxml.com.cn/WebServices/ForexRmbRateWebService.asmx
即时外汇汇率数据 WEB 服务
http://webservice.webxml.com.cn/WebServices/ExchangeRateWebService.asmx
ruby中调用webservice
$ gem install savon -v 0.9.1
require "savon" # create a client for your SOAP service client = Savon::Client.new("http://webservice.webxml.com.cn/WebServices/ForexRmbRateWebService.asmx?wsdl") client.wsdl.soap_actions Retrieving WSDL from: http://webservice.webxml.com.cn/WebServices/ForexRmbRateWebService.asmx?wsdl HTTPI executes HTTP GET using the net_http adapter => [:get_forex_rmb_rate, :get_forex_rmb_rate_pro] # execute a SOAP request to call the "getUser" action response = client.request(:get_forex_rmb_rate) response.to_hash[:get_forex_rmb_rate_response][:get_forex_rmb_rate_result][:diffgram][:get_forex_rmb_rate][:forex_rmb_rate].each do|r| #r.each {|k,v|puts "#{k}: #{v}"} if r[:base_price] then puts "#{r[:symbol]} : #{r[:base_price]}" end end
A Rails 3 app to test implementation of the SAVON gem in rails to handle SOAP calls
https://github.com/veldtmana/SOAPTest
Retrieve currency exchange rates from Yahoo! Finance.
https://github.com/scottbarr/yahoo_currency
使用此Gem時出現以下錯誤:
原因:
此Gem中有class ExchangeRate,
而app/model中有
class ExchangeRate < ActiveRecord::Base
兩處的class ExchangeRate 發生了衝突.
解決方法:
將/app/model中的class ExchangeRate改名.
Ruby Money::Bank interface for the Google Currency exchange data
https://github.com/RubyMoney/google_currency
每日8:00讀取匯率資料並儲存到Table中:
$
gem install whenever
soap.rake:
# encoding: utf-8 require 'savon' require 'yahoo_currency' def rmb_rate db = Sequel.connect('oracle://mis:[email protected]:1521/test') # create a client for your SOAP service client = Savon::Client.new("http://webservice.webxml.com.cn/WebServices/ForexRmbRateWebService.asmx?wsdl") # execute a SOAP request to call the "getUser" action response = client.request(:get_forex_rmb_rate) response.to_hash[:get_forex_rmb_rate_response][:get_forex_rmb_rate_result][:diffgram][:get_forex_rmb_rate][:forex_rmb_rate].each do|r| #r.each {|k,v|puts "#{k}: #{v}"} if r[:base_price] then #puts "#{r[:symbol]} : #{r[:base_price]}" #WorldCur.find(r[:symbol]).update_attribute(:whpj, r[:base_price]) db[:world_curs].filter('code = ?', r[:symbol]).update(:boc_whpj => r[:base_price], :boc_at => Time.new) end end end def yahoo_rate db = Sequel.connect('oracle://mis:[email protected]:1521/test') sql = 'select t.code from world_curs t' db[sql].each do |r| #puts r[:code] exchange_rate = YahooCurrency.get_rate!(r[:code], "CNY") db[:world_curs].filter('code = ?', r[:code]).update(:yahoo_rate => exchange_rate.rate, :yahoo_at => exchange_rate.timestamp) end end namespace :soap do desc "RMB Rate" task :rmb_rate do rmb_rate end desc "Yahoo Rate" task :yahoo_rate do yahoo_rate end end
schedule.rb
set :output, "#{path}/log/whenever.log" job_type :rake_no_env, "cd :path && rake :task :output" every 1.day,:at=>'08:00am' do rake_no_env "soap:rmb_rate" rake_no_env "soap:yahoo_rate" end