Module: Lpp
- Defined in:
- lib/lpp.rb,
lib/lpp/version.rb
Constant Summary collapse
- REGEX =
/NAPOVED PRIHODOV ZA (.+?) \((.+?)\)(.+?)(?:NAPOVED PRIHODOV ZA (.+?) \((.+?)\)(.+?))?Napovedi/m- VERSION =
"0.0.2"
Class Method Summary collapse
Class Method Details
.arrivals(station) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/lpp.rb', line 11 def self.arrivals(station) http = Net::HTTP.new('wbus.talktrack.com') data = "__EVENTTARGET=&__EVENTARGUMENT=&tb_postaja=#{station}&b_send=Prika%C5%BEi" headers = { 'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko)', 'Content-Type' => 'application/x-www-form-urlencoded' } res = http.post('/wap.aspx', data, headers) text = Nokogiri::HTML(res.body).text raise 'Station not found' if /Postaje s tem imenom nismo našli/.match(text) matches = REGEX.match(text) result = Hash.new result[matches[2]] = {name: matches[1], buses: parse(matches[3])} # Twice if there are two statons if matches[4] result[matches[5]] = {name: matches[4], buses: parse(matches[6])} end result end |
.arrivals_for_bus(station, bus) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/lpp.rb', line 36 def self.arrivals_for_bus(station, bus) arrivals = arrivals(station) result = Hash.new arrivals.each do |station_key, station_value| buses = station_value[:buses].select do |bus_entry| bus_entry =~ /^#{bus}/ end if !buses.empty? result[station_key] = {name: station_value[:name], buses: buses} end end result end |
.parse(buses) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/lpp.rb', line 50 def self.parse(buses) result = Hash.new buses.strip.split("\r\n").each do |bus| matches = /(\d+)(?:\s+(.))? (.*?)\:(?:\s(n))? (\d+\:\d+)(?:,(?:\s(n))? (\d+\:\d+))?(?:,(?:\s(n))? (\d+\:\d+))?/.match(bus) if matches times = ([matches[5]] + [matches[7]] + [matches[9]]).compact easy = ([matches[4]] + [matches[6]] + [matches[8]]) bus_number = matches[1]+matches[2].to_s result[bus_number + " " + matches[3]] = { route: matches[3], bus_number: bus_number, arrivals: times.each_with_index.map do |time, index| {time: time, easy: easy[index] == 'n'} end } end end result end |