class ESun

玉山銀行的匯率

Public Class Methods

Exchange_CN_TO_TW(money, change_type = 0) click to toggle source
# File lib/exchange_rate.rb, line 365
def self.Exchange_CN_TO_TW(money, change_type = 0)
  if money.is_a? Numeric 
   case change_type
   when 0
     money *  get_CN[:buying_rate] 
   when 1
     money * get_CN[:cash_buy_rate]
   else
     money * get_CN[:buying_best_rate]
   end
  else
    raise "請輸入數字"
  end
end
Exchange_JP_TO_TW(money,change_type = 0) click to toggle source
# File lib/exchange_rate.rb, line 301
def self.Exchange_JP_TO_TW(money,change_type = 0)
  if money.is_a? Numeric
   case change_type
   when 0
     money * get_JP[:buying_rate]
   when 1
     money * get_JP[:cash_buy_rate]
   else
     money * get_JP[:buying_best_rate]
   end
   
 else
    raise "請輸入數字"
   end
end
Exchange_TW_TO_CN(money, change_type = 0) click to toggle source

人民幣換算

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

台幣換成日幣,change_type = 0:即期 1:現金 2:優惠匯率

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

美金換算

# File lib/exchange_rate.rb, line 318
def self.Exchange_TW_TO_US(money, change_type = 0)
  if money.is_a? Numeric 
   case change_type
   when 0
     money / get_US[:selling_rate]
   when 1
     money / get_US[:cash_sell_rate]
   else
     money / get_US[:selling_best_rate]
   end
    
  else
    raise "請輸入數字"
  end
end
Exchange_US_TO_TW(money, change_type = 0) click to toggle source
# File lib/exchange_rate.rb, line 334
def self.Exchange_US_TO_TW(money, change_type = 0)
  if money.is_a? Numeric
   case change_type
   when 0
     money * get_US[:buying_rate]
   when 1
     money * get_US[:cash_buy_rate]
   else
     money * get_US[:buying_best_rate]
   end
  else
    raise "請輸入數字"
  end
end
get_CN() click to toggle source
# File lib/exchange_rate.rb, line 270
def self.get_CN
  rates = refresh_bank_rate_json(1)
  bank_result = {
    cash_buy_rate:rates['result']['cash_buy_rate'].to_f,
    cash_sell_rate:rates['result']['cash_sell_rate'].to_f,
    buying_rate:rates['result']['buying_rate'].to_f,
    selling_rate:rates['result']['selling_rate'].to_f,
    buying_best_rate:rates['result']['buying_best_rate'].to_f,
    selling_best_rate:rates['result']['selling_best_rate'].to_f,
    bank_name:"ESun Bank CN"
  }
  bank_result
end
get_JP() click to toggle source
# File lib/exchange_rate.rb, line 242
def self.get_JP
  rates = refresh_bank_rate_json(3)
  bank_result = {
    cash_buy_rate:rates['result']['cash_buy_rate'].to_f,
    cash_sell_rate:rates['result']['cash_sell_rate'].to_f,
    buying_rate:rates['result']['buying_rate'].to_f,
    selling_rate:rates['result']['selling_rate'].to_f,
    buying_best_rate:rates['result']['buying_best_rate'].to_f,
    selling_best_rate:rates['result']['selling_best_rate'].to_f,
    bank_name:"ESun Bank JP"
  }
  bank_result
end
get_US() click to toggle source
# File lib/exchange_rate.rb, line 256
def self.get_US
  rates = refresh_bank_rate_json(0)
  bank_result = {
    cash_buy_rate:rates['result']['cash_buy_rate'].to_f,
    cash_sell_rate:rates['result']['cash_sell_rate'].to_f,
    buying_rate:rates['result']['buying_rate'].to_f,
    selling_rate:rates['result']['selling_rate'].to_f,
    buying_best_rate:rates['result']['buying_best_rate'].to_f,
    selling_best_rate:rates['result']['selling_best_rate'].to_f,
    bank_name:"ESun Bank US"
  }
  bank_result
end

Private Class Methods

parseNode(node,country_code) click to toggle source

code +7

# File lib/exchange_rate.rb, line 382
def self.parseNode(node,country_code)

rates = {
  cash_buy_rate:node.css("td[data-name=即期買入匯率]")[country_code].text,
  cash_sell_rate: node.css("td[data-name=即期賣出匯率]")[country_code].text,
  buying_rate: node.css("td[data-name=現金買入匯率]")[country_code].text,
  selling_rate: node.css("td[data-name=現金賣出匯率]")[country_code].text,
  buying_best_rate: node.css("td[data-name]")[3 + country_code*7].text,
  selling_best_rate: node.css("td[data-name]")[4 + country_code*7].text,
  name: name.strip
}
 rates
end
refresh_bank_rate_json(country_code) click to toggle source
# File lib/exchange_rate.rb, line 396
def self.refresh_bank_rate_json(country_code)
  url = "https://www.esunbank.com.tw/bank/personal/deposit/rate/forex/foreign-exchange-rates"
  html = Nokogiri::HTML(open(url)) 
  datetime = html.css("span[@id = LbQuoteTime]").text
  tableRows = html.css("table > tr > td") 
  rates = {
    update: datetime,  
    result:  parseNode(tableRows,country_code)
  }
  rates= rates.to_json
  return json = JSON.parse(rates)
end