Module: TraininfoKanto

Defined in:
lib/traininfo_kanto.rb,
lib/traininfo_kanto/cli.rb,
lib/traininfo_kanto/client.rb,
lib/traininfo_kanto/version.rb

Defined Under Namespace

Classes: CLI

Constant Summary collapse

PAGES =
{
  # Route: [Kanto page XPath table number, Kanto page XPath column number, detail page URL number]
  # JR東日本
  
VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.description(detail_url) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/traininfo_kanto/client.rb', line 63

def description(detail_url)
  charset = nil

  detail_html = URI.parse(detail_url).open do |f|
    charset = f.charset
    f.read
  end

  detail_doc = Nokogiri::HTML.parse(detail_html, nil, charset)
  detail_doc.xpath('//*[@id="mdServiceStatus"]/dl/dd/p').first.text
end

.get(route_array, url: false) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/traininfo_kanto/client.rb', line 36

def get(route_array, url: false)
  messages = []

  route_array.each do |route|
    status_xpath = "//*[@id='mdAreaMajorLine']/div[#{PAGES[route.to_sym][0]}]/table/tr[#{PAGES[route.to_sym][1]}]/td[2]"
    detail_url = "https://transit.yahoo.co.jp/traininfo/detail/#{PAGES[route.to_sym][2]}/0/"

    state = kanto_doc.xpath(status_xpath).first.text

    messages << message(route, state, url, detail_url)
  end

  messages
end

.kanto_docObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/traininfo_kanto/client.rb', line 51

def kanto_doc
  charset = nil
  kanto_url = 'https://transit.yahoo.co.jp/traininfo/area/4/'

  kanto_html = URI.parse(kanto_url).open do |f|
    charset = f.charset
    f.read
  end

  Nokogiri::HTML.parse(kanto_html, nil, charset)
end

.message(route, state, url_option, detail_url) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/traininfo_kanto/client.rb', line 75

def message(route, state, url_option, detail_url)
  state.slice!('[◯]')
  state.slice!('[!]')

  return "#{route}は#{state}です。" if state == '平常運転'

  message = case state
            when '運転状況'
              "#{route}は#{state}に変更があります。"
            when '列車遅延'
              "#{route}は#{state}があります。"
            when '運転見合わせ'
              "#{route}は#{state}しています。"
            end

  if url_option
    message + "\n" + description(detail_url) + "\n" + detail_url
  else
    message + "\n" + description(detail_url)
  end
end