class PLA::Movements
Attributes
url[RW]
headers[R]
records[R]
Public Class Methods
new()
click to toggle source
# File lib/pla/movements.rb, line 18 def initialize @url = self.class.url @root = 'div.newsMainArea table tr td table tr' # It gets worse @records = [] @ships = get_ships_data set_headers set_records @records end
set_url(u)
click to toggle source
# File lib/pla/movements.rb, line 11 def set_url u @url = u end
Public Instance Methods
get_ships_data()
click to toggle source
# File lib/pla/movements.rb, line 31 def get_ships_data Nokogiri::HTML(open(@url)).css(@root) end
set_headers()
click to toggle source
# File lib/pla/movements.rb, line 35 def set_headers @headers = normalize_and_map(@ships.first.css('th')) @headers[-1] = 'notes' # pla returns this as empty. We infer it means notes, though usually it says 'Pilot required' end
set_records()
click to toggle source
# File lib/pla/movements.rb, line 40 def set_records @ships[1..-1].each do |t| record = {} fields = normalize_and_map(t.css('td')) fields.each_with_index do |f,i| record[ @headers[i].to_sym ] = f end movement = PLA::Movement.new(record) @records << movement.to_h end end
Private Instance Methods
normalize_and_map(elem)
click to toggle source
# File lib/pla/movements.rb, line 54 def normalize_and_map elem # This is a mess- the pla website wraps a date with non-breaking spaces # and some other stuff with normal whitespace. We don't want this because # it is a mess. Thus we convert nbsp chars (ord 160) with spaces (ord 32) # and then allow strip to tidy the rest up. nbsp = Nokogiri::HTML(" ").text elem.map{|c| c.text.gsub(nbsp, ' ').strip.downcase} end