class ShippingForecast

Constants

URL

Public Class Methods

[](location) click to toggle source

Public: Returns a forecast for a particular location

# File lib/shipping_forecast.rb, line 22
def self.[] location
  report[location]
end
all() click to toggle source

Public: Returns all forecasts

# File lib/shipping_forecast.rb, line 27
def self.all
  report
end
locations() click to toggle source

Public: Returns an array of strings of each available location

# File lib/shipping_forecast.rb, line 32
def self.locations
  report.keys.sort
end
new() click to toggle source
# File lib/shipping_forecast.rb, line 36
def initialize
  @built = false
  @data  = nil
end
report() click to toggle source

Public: Returns a hash of OpenStruct objects, each representing a location report.

Contents of each report:

warning    — If there is a warning in effect, returns an OpenStruct object with attributes:
title      – The title of the warning, e.g., "Gale Warning"
issued     – When the warning was issued
summary    – The text summary of the warning
wind       – The wind conditions
seas       – The current sea conditions
visibility – The current visibility report
# File lib/shipping_forecast.rb, line 17
def self.report
  @raw_report ||= new.raw_report
end

Public Instance Methods

raw_report() click to toggle source
# File lib/shipping_forecast.rb, line 41
def raw_report
  build_data unless @built
  @data
end

Private Instance Methods

build_data() click to toggle source
# File lib/shipping_forecast.rb, line 48
def build_data
  agent = Mechanize.new
  page = agent.get(URL)

  locations = {}

  1.upto(31) do |i|
    area = page.search("#area-#{i}")
    location = area.search("h2").text

    # Warnings, if any
    warning = OpenStruct.new
    warning_detail = area.search(".warning-detail")
    warning.title   = warning_detail.search("strong").text.gsub(':', '')
    warning.issued  = warning_detail.search(".issued").text
    warning.summary = warning_detail.search(".summary").text

    # Build the hash
    area_hash = OpenStruct.new
    breakdown  = area.search("ul").children.search("span")

    area_hash.warning    = warning
    area_hash.location   = location
    area_hash.wind       = breakdown[0].text
    area_hash.seas       = breakdown[1].text
    area_hash.weather    = breakdown[2].text
    area_hash.visibility = breakdown[3].text

    locations[location] = area_hash
  end

  @data = locations
  @built = true
end