Class: UkBuses::Query
- Inherits:
-
Object
- Object
- UkBuses::Query
- Defined in:
- lib/uk_buses/query.rb
Instance Attribute Summary collapse
-
#stop_code ⇒ Object
Returns the value of attribute stop_code.
Instance Method Summary collapse
- #fetch_buses ⇒ Object
- #get_document ⇒ Object
-
#initialize(stop_code) ⇒ Query
constructor
A new instance of Query.
- #url ⇒ Object
- #xpath ⇒ Object
Constructor Details
#initialize(stop_code) ⇒ Query
Returns a new instance of Query.
9 10 11 |
# File 'lib/uk_buses/query.rb', line 9 def initialize(stop_code) @stop_code = stop_code end |
Instance Attribute Details
#stop_code ⇒ Object
Returns the value of attribute stop_code.
7 8 9 |
# File 'lib/uk_buses/query.rb', line 7 def stop_code @stop_code end |
Instance Method Details
#fetch_buses ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# 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 ⇒ Object
21 22 23 |
# File 'lib/uk_buses/query.rb', line 21 def get_document Nokogiri::HTML(open(url)) end |
#url ⇒ Object
13 14 15 |
# File 'lib/uk_buses/query.rb', line 13 def url "http://nextbuses.mobi/WebView/BusStopSearch/BusStopSearchResults/#{ @stop_code }" end |
#xpath ⇒ Object
17 18 19 |
# File 'lib/uk_buses/query.rb', line 17 def xpath '//*[@id="wrapper"]/div[4]/table[1]/tr' end |