class Ruboty::TrainDelay::Actions::Delay

Public Class Methods

create_train_list() click to toggle source
# File lib/ruboty/train_delay/actions/delay.rb, line 24
def create_train_list
  Ruboty::TrainDelay::AREA_SET.values.each do |area|
    Nokogiri::HTML(open(list_url(area)))
    .css("#mdAreaMajorLine .elmTblLstLine > table tr a")
    .map do |a|
      data = { name: a.content, href: a.attributes["href"].value }
      train_list.push data
    end
  end
end
list_url(area_code) click to toggle source
# File lib/ruboty/train_delay/actions/delay.rb, line 35
def list_url(area_code)
  "http://transit.loco.yahoo.co.jp/traininfo/area/#{area_code}/"
end
train_list() click to toggle source
# File lib/ruboty/train_delay/actions/delay.rb, line 20
def train_list
  @train_list ||= []
end

Public Instance Methods

call() click to toggle source
# File lib/ruboty/train_delay/actions/delay.rb, line 40
def call
  list = delay_list
  return message.reply(found_route) if list.length == 0
  return message.reply(many_route(list)) if list.length > 1
  message.reply("#{list.first[:name]}: #{cause_delay(list.first)}")
rescue OpenURI::HTTPError => e
  message.reply("情報を取得することができませんでした。URLが正しいか確認してください。")
  message.reply("今回使用したURL:#{list_url}")
end

Private Instance Methods

cause_delay(route) click to toggle source
# File lib/ruboty/train_delay/actions/delay.rb, line 56
def cause_delay(route)
  Nokogiri::HTML(open(route[:href]))
  .css('#mdServiceStatus p').text
end
delay_list() click to toggle source
# File lib/ruboty/train_delay/actions/delay.rb, line 61
def delay_list
  self.class.train_list
  .select { |site| site[:name].include? route }
end
found_route() click to toggle source
# File lib/ruboty/train_delay/actions/delay.rb, line 66
        def found_route
          <<-"EOS"
  知らない路線です。
  http://transit.loco.yahoo.co.jp/traininfo
  から探してください。
          EOS
        end
many_route(routes) click to toggle source
# File lib/ruboty/train_delay/actions/delay.rb, line 74
        def many_route(routes)
          <<-"EOS"
絞り込めませんね。下のどれかでしょうか?
#{routes.map { |route| "  * #{route[:name]}" }.join("\n") }
          EOS
        end
route() click to toggle source
# File lib/ruboty/train_delay/actions/delay.rb, line 52
def route
  message[:route]
end