`
wuhuizhong
  • 浏览: 668619 次
  • 性别: Icon_minigender_1
  • 来自: 中山
社区版块
存档分类
最新评论

汇率数据

 
阅读更多

 

外汇-人民币即时报价 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時出現以下錯誤:

Error message:
superclass mismatch for class ExchangeRate

原因:

此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:password@172.18.60.39: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:password@172.18.60.39: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
 

 

 

 

分享到:
评论
3 楼 zaikai 2011-10-18  
转为hash然后再写代码解析是吗?谢谢你百忙之中解答我的问题
2 楼 wuhuizhong 2011-10-18  
用to_hash將XML转换为hash.
1 楼 zaikai 2011-10-18  
我想请问下,webservice返回过来的XML怎么解析呢?是否是什么工具来解析的?
我是用sorp4r来调用XML的,虽然都说savon比它好,但是sorp4r生成的XML方法文件
看起来比较明确。

相关推荐

Global site tag (gtag.js) - Google Analytics