Class: Rzd::Commands::GetTrains

Inherits:
Object
  • Object
show all
Defined in:
lib/rzd/commands/get_trains.rb

Instance Method Summary collapse

Constructor Details

#initializeGetTrains

Returns a new instance of GetTrains.



5
6
7
# File 'lib/rzd/commands/get_trains.rb', line 5

def initialize
  @agent = Mechanize.new
end

Instance Method Details

#execute(from, to, date) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rzd/commands/get_trains.rb', line 9

def execute( from, to, date )
  page = @agent.get "http://pass.rzd.ru/isvp/public/pass/isvp/public/rzd/express?schd_id=1&action=submit&layer_id=4922&STRUCTURE_ID=735&src=#{CGI::escape from}&dst=#{CGI::escape to}&date=#{CGI::escape date}"
  table = page.parser.css('form table')[2]
  if table
    rows = table.css('tr')
    rows.shift
    rows.map do |row|
      cells = row.css('td')

      Rzd::Train.new cells[2].text.strip, cells[3].text.strip, get_seats( cells[7..11].text )
    end
  else
    []
  end
end