Module: Ttsttb

Defined in:
lib/ttsttb.rb,
lib/ttsttb/version.rb

Overview

Scrape TTS and TTB data from MURC.

Constant Summary collapse

VERSION =
'0.2.1'.freeze

Class Method Summary collapse

Class Method Details

.find(date) ⇒ Object

Execute scraping



9
10
11
12
13
14
15
16
17
# File 'lib/ttsttb.rb', line 9

def self.find(date)
  # TODO
  raise 'Today is not supported yet.' if date == Date.today

  raise 'Old data before 1990/1/1 is not provided.' if date < Date.new(1990, 1, 1)

  doc = Nokogiri::HTML(html(date))
  scrape(doc)
end

.get_ttm(tts, ttb) ⇒ Object

calculate TTM



70
71
72
73
74
75
76
# File 'lib/ttsttb.rb', line 70

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

.html(date) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ttsttb.rb', line 19

def self.html(date)
  url = format('http://www.murc-kawasesouba.jp/fx/past/index.php?id=%<ymd>s',
               { :ymd => date.strftime('%y%m%d') })

  URI.open(url, :redirect => false).read.encode('utf-8')
rescue OpenURI::HTTPRedirect
  url = format('http://www.murc-kawasesouba.jp/fx/past_3month_result.php?y=%<y>s&m=%<m>s&d=%<d>s&c=',
               {
                 :y => date.strftime('%Y'),
                 :m => date.strftime('%m'),
                 :d => date.strftime('%d')
               })

  URI.open(url, :redirect => false).read.encode('utf-8')
end

.normalize(value) ⇒ Object

normalize string to float



62
63
64
65
66
67
# File 'lib/ttsttb.rb', line 62

def self.normalize(value)
  value = value.strip
  return nil if value == 'unquoted'

  value.to_f
end

.scrape(doc) ⇒ Object

parse document



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ttsttb.rb', line 36

def self.scrape(doc)
  rows = {}
  doc.xpath('//table[1]/tr').each do |tr|
    tds = tr.xpath('.//td')

    next unless tds[0]

    tts = normalize(tds[3].content)
    ttb = normalize(tds[4].content)

    rows[tds[2].content] = {
      'currency' => {
        'en' => tds[0].content,
        'ja' => tds[1].content
      },
      'code' => tds[2].content,
      'tts' => tts,
      'ttb' => ttb,
      'ttm' => get_ttm(tts, ttb)
    }
  end

  rows
end