module TraininfoKanto

Constants

PAGES
VERSION

Public Class Methods

description(detail_url) click to toggle source
# 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) click to toggle source
# 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_doc() click to toggle source
# 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) click to toggle source
# 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