Class: TideScraper::Scraper
- Inherits:
-
Object
- Object
- TideScraper::Scraper
- Defined in:
- lib/tide_scraper/ports.rb,
lib/tide_scraper/scraper.rb,
lib/tide_scraper/predictions.rb
Instance Method Summary collapse
- #get_port_info(port_id) ⇒ Object
-
#get_prediction(port, length = 7) ⇒ Object
Returns tidal prediction.
-
#initialize ⇒ Scraper
constructor
A new instance of Scraper.
-
#parse_tide_table(table) ⇒ Object
Will parse a table from a predictions page on UKHO’s EasyTide TODO: Timezones.
Constructor Details
#initialize ⇒ Scraper
Returns a new instance of Scraper.
6 7 8 9 10 |
# File 'lib/tide_scraper/scraper.rb', line 6 def initialize @site = 'http://www.ukho.gov.uk/easytide/EasyTide' @clnt = HTTPClient.new return true end |
Instance Method Details
#get_port_info(port_id) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# 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) ⇒ Object
Returns tidal prediction
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# 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) ⇒ Object
Will parse a table from a predictions page on UKHO’s EasyTide TODO: Timezones
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# 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 |