class TaiwanBank

bank class end

Public Class Methods

Exchange_CN_TO_TW(money, isCash = true) click to toggle source
# File lib/exchange_rate.rb, line 202
def self.Exchange_CN_TO_TW(money, isCash = true)
  if money.is_a? Numeric 
    isCash ? money * get_CN[:buying_rate] : money * get_CN[:cash_buy_rate]
  else
    raise "請輸入數字"
  end
end
Exchange_JP_TO_TW(money,isCash = true) click to toggle source
# File lib/exchange_rate.rb, line 168
def self.Exchange_JP_TO_TW(money,isCash = true)
  if money.is_a? Numeric
    isCash ? money * get_JP[:buying_rate] :  money * get_JP[:cash_buy_rate]
   else
    raise "請輸入數字"
   end
end
Exchange_TW_TO_CN(money, isCash = true) click to toggle source

人民幣換算

# File lib/exchange_rate.rb, line 194
def self.Exchange_TW_TO_CN(money, isCash = true)
  if money.is_a? Numeric 
    isCash ? money / get_CN[:selling_rate] : money / get_CN[:cash_sell_rate]
  else
    raise "請輸入數字"
  end
end
Exchange_TW_TO_JP(money,isCash = true) click to toggle source

日幣換算

# File lib/exchange_rate.rb, line 160
def self.Exchange_TW_TO_JP(money,isCash = true)
   if money.is_a? Numeric
    isCash ? money / get_JP[:selling_rate] :  money / get_JP[:cash_sell_rate]
   else
    raise "請輸入數字"
   end
end
Exchange_TW_TO_US(money, isCash = true) click to toggle source

美金換算

# File lib/exchange_rate.rb, line 177
def self.Exchange_TW_TO_US(money, isCash = true)
  if money.is_a? Numeric 
    isCash ? money / get_US[:selling_rate] : money / get_US[:cash_sell_rate]
  else
    raise "請輸入數字"
  end
end
Exchange_US_TO_TW(money, isCash = true) click to toggle source
# File lib/exchange_rate.rb, line 185
def self.Exchange_US_TO_TW(money, isCash = true)
  if money.is_a? Numeric
    isCash ? money * get_US[:buying_rate] : money * get_US[:cash_buy_rate]
  else
    raise "請輸入數字"
  end
end
async_get_CN() click to toggle source
# File lib/exchange_rate.rb, line 151
def self.async_get_CN
  Async do 
    @cn = get_CN
  end
  @cn
end
async_get_JP() click to toggle source

using Async 取得日幣

# File lib/exchange_rate.rb, line 111
def self.async_get_JP 
  Async do 
    @jp = get_JP
  end
  @jp
end
async_get_US() click to toggle source
# File lib/exchange_rate.rb, line 131
def self.async_get_US
  Async do 
    @us = get_US
  end
  @us
end
get_CN() click to toggle source

取得人民幣

# File lib/exchange_rate.rb, line 139
def self.get_CN
  rates = refresh_bank_rate_json
  bank_result = {
   cash_buy_rate:rates['results']['CNY']['cash_buy_rate'].to_f,
   cash_sell_rate:rates['results']['CNY']['cash_sell_rate'].to_f,
   buying_rate:rates['results']['CNY']['buying_rate'].to_f,
   selling_rate:rates['results']['CNY']['selling_rate'].to_f,
   bank_name:"Taiwan Bank CN"
 }
 bank_result
end
get_JP() click to toggle source

取得日幣,cash_buy_rate,cash_sell_rate,buying_rate,selling_rate 取得日幣

# File lib/exchange_rate.rb, line 99
def self.get_JP
  rates = refresh_bank_rate_json
  bank_result = {
    cash_buy_rate:rates['results']['JPY']['cash_buy_rate'].to_f,
    cash_sell_rate:rates['results']['JPY']['cash_sell_rate'].to_f,
    buying_rate:rates['results']['JPY']['buying_rate'].to_f,
    selling_rate:rates['results']['JPY']['selling_rate'].to_f,
    bank_name:"Taiwan Bank JP"
  }
  bank_result
end
get_US() click to toggle source

取得美金

# File lib/exchange_rate.rb, line 119
def self.get_US
   rates = refresh_bank_rate_json
   bank_result = {
    cash_buy_rate:rates['results']['USD']['cash_buy_rate'].to_f,
    cash_sell_rate:rates['results']['USD']['cash_sell_rate'].to_f,
    buying_rate:rates['results']['USD']['buying_rate'].to_f,
    selling_rate:rates['results']['USD']['selling_rate'].to_f,
    bank_name:"Taiwan Bank US"
  }
  bank_result
end

Private Class Methods

parseNode(node) click to toggle source
# File lib/exchange_rate.rb, line 211
def self.parseNode(node)
  name = node.css("div.print_show").text 
  symbol = name.match(/[A-Z]+/).to_s 
  rates = {
  cash_buy_rate: node.css("td[data-table=本行現金買入]")[0].text,
  cash_sell_rate: node.css("td[data-table=本行現金賣出]")[0].text,
  buying_rate: node.css("td[data-table=本行即期買入]")[0].text,
  selling_rate: node.css("td[data-table=本行即期賣出]")[0].text,
  name: name.strip
}
data = { symbol.to_sym => rates }
end
refresh_bank_rate_json() click to toggle source
# File lib/exchange_rate.rb, line 224
def self.refresh_bank_rate_json
  url = "https://rate.bot.com.tw/xrt?Lang=zh-TW"
  html = Nokogiri::HTML(open(url)) 
  datetime = html.css("span.time").text 
  tableRows = html.css("table > tbody > tr") 
  rates = {
     update: datetime,  #自定義一個變數名稱為update抓取上面所設定的datetime
     results: tableRows.reduce({}) { |accumulator, node| accumulator.merge parseNode(node) }
  }

  rates= rates.to_json
  return json = JSON.parse(rates)
end