class UkBuses::Query

Attributes

stop_code[RW]

Public Class Methods

new(stop_code) click to toggle source
# File lib/uk_buses/query.rb, line 9
def initialize(stop_code)
  @stop_code = stop_code
end

Public Instance Methods

fetch_buses() click to toggle source
# File lib/uk_buses/query.rb, line 25
def fetch_buses
  buses = []

  get_document.xpath(xpath).each do |row|
    current_bus = {}
      row.children.each do |child|

        child_inner = child.inner_text.strip
        current_bus[:route_number] = child_inner if child_inner.length >= 1 && child_inner.length < 5
        current_bus[:destination] = child_inner.split(/\b(at|in|DUE)\b/).first[0..-2] if child_inner.length > 5
        if child_inner.split(/\b(at|in|DUE)\b/)[1] == 'DUE'
          current_bus[:arrives] = 'DUE'
        else
          current_bus[:arrives] = child_inner.split(/\b(at|in|DUE)\b/)[2].strip if child_inner.length > 5
        end
      end
    buses << UkBuses::Bus.new(current_bus[:route_number],
                              current_bus[:destination],
                              current_bus[:arrives])
  end
  buses
end
get_document() click to toggle source
# File lib/uk_buses/query.rb, line 21
def get_document
  Nokogiri::HTML(open(url))
end
url() click to toggle source
# File lib/uk_buses/query.rb, line 13
def url
  "http://nextbuses.mobi/WebView/BusStopSearch/BusStopSearchResults/#{ @stop_code }"
end
xpath() click to toggle source
# File lib/uk_buses/query.rb, line 17
def xpath
  '//*[@id="wrapper"]/div[4]/table[1]/tr'
end