Module: Ttsttb
- Defined in:
- lib/ttsttb.rb,
lib/ttsttb/version.rb
Overview
Scrape TTS and TTB data from MURC.
Constant Summary collapse
- VERSION =
'0.0.3'.freeze
Class Method Summary collapse
-
.find(date) ⇒ Object
Execute scraping.
-
.get_ttm(tts, ttb) ⇒ Object
calculate TTM.
-
.normalize(value) ⇒ Object
normalize string to float.
-
.scrape(doc) ⇒ Object
parse document.
Class Method Details
.find(date) ⇒ Object
Execute scraping
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/ttsttb.rb', line 7 def self.find(date) # TODO throw 'Data of today is not supported yet.' if date == Date.today # TODO throw 'Old data is not supported yet.' if date == Date.new(2006, 1, 1) require 'date' url = format('http://www.murc-kawasesouba.jp/fx/past/index.php?id=%s', date.strftime('%y%m%d')) require 'open-uri' doc = Nokogiri::HTML(open(url, redirect: false)) scrape(doc) end |
.get_ttm(tts, ttb) ⇒ Object
calculate TTM
54 55 56 57 58 59 60 |
# File 'lib/ttsttb.rb', line 54 def self.get_ttm(tts, ttb) require 'bigdecimal' return nil unless tts && ttb (BigDecimal(tts.to_s) + BigDecimal(ttb.to_s) / 2).round(2).to_f end |
.normalize(value) ⇒ Object
normalize string to float
47 48 49 50 51 |
# File 'lib/ttsttb.rb', line 47 def self.normalize(value) value = value.strip return nil if value == 'unquoted' value.to_f end |
.scrape(doc) ⇒ Object
parse document
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ttsttb.rb', line 24 def self.scrape(doc) rows = {} doc.xpath('//table[1]/tr').each do |tr| tds = tr.xpath('.//td') next unless tds[0] rows[tds[2].content] = { 'currency' => { 'en' => tds[0].content, 'ja' => tds[1].content }, 'code' => tds[2].content, 'tts' => normalize(tds[3].content), 'ttb' => normalize(tds[4].content), 'ttm' => get_ttm(normalize(tds[3].content), normalize(tds[4].content)) } end rows end |