class TideScraper::Scraper
Public Class Methods
new()
click to toggle source
# File lib/tide_scraper/scraper.rb, line 6 def initialize @site = 'http://www.ukho.gov.uk/easytide/EasyTide' @clnt = HTTPClient.new return true end
Public Instance Methods
get_port_info(port_id)
click to toggle source
# File lib/tide_scraper/ports.rb, line 3 def get_port_info(port_id) endpoint = '/SelectPrediction.aspx?PortID=' url = @site+endpoint+port_id.to_s response = @clnt.get(url) return nil unless response.code == 200 doc = Nokogiri::HTML response.content error = doc.xpath('//h1[contains(.,"An error has occurred")]').size return nil if error > 0 port_info = Hash.new port_info[:id] = port_id.to_s port_info[:name] = doc.xpath('//span[@class="PortName"]').text port_info[:country] = doc.xpath('//span[@class="CountryPredSummary"]').text return port_info end
get_prediction(port,length=7)
click to toggle source
Returns tidal prediction
# File lib/tide_scraper/predictions.rb, line 5 def get_prediction(port,length=7) endpoint = '/ShowPrediction.aspx' url = @site+endpoint+"?PortID=#{port}&PredictionLength=#{length}" response = @clnt.get(url, follow_redirect: true) #return nil if response.code != '200' doc = Nokogiri::HTML response.content tables = doc.xpath('//table[starts-with(@class,"HWLWTable")]') tides = [] tables.each do |table| tides.concat(parse_tide_table(table)) end tides.sort_by {|hsh| hsh[:time]} end
parse_tide_table(table)
click to toggle source
Will parse a table from a predictions page on UKHO's EasyTide TODO: Timezones
# File lib/tide_scraper/predictions.rb, line 21 def parse_tide_table(table) year = Time.now.year date = table.xpath('tr/th[@class="HWLWTableHeaderCell"]').text # Populate highs and lows hilow = [] table.xpath('tr[2]/th').each do |hilowth| hilow.push hilowth.text end # Populate times times = [] table.xpath('tr[3]/td').each do |timetd| times.push Time.parse(timetd.text + " " + year.to_s + " " + date) end heights = [] table.xpath('tr[4]/td').each do |heighttd| heights.push heighttd.text.gsub("\u00A0"," ") end tides = [] hilow.each_index do |i| tide = Hash.new tide[:stage] = hilow[i] tide[:time] = times[i] tide[:height] = heights[i] tides.push tide end return tides end