class Gull::Client

Client exposes methods and options for fetching alerts from the NWS/NOAA web service

Attributes

errors[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/gull/client.rb, line 10
def initialize(options = {})
  @options = {
    url: 'http://alerts.weather.gov/cap/us.php?x=1',
    strict: false
  }.merge options
end

Public Instance Methods

fetch() click to toggle source
# File lib/gull/client.rb, line 17
def fetch
  self.errors = []
  content = response
  document = Nokogiri::XML content do |config|
    config.strict if @options[:strict]
  end
  process document.xpath('//xmlns:feed/xmlns:entry', namespaces)
end

Private Instance Methods

create_instance(entry) click to toggle source
# File lib/gull/client.rb, line 51
def create_instance(entry)
  return if entry.xpath('cap:event').empty?

  alert = Alert.new
  alert.parse entry
  alert
end
namespaces() click to toggle source
# File lib/gull/client.rb, line 59
def namespaces
  { 'xmlns' => 'http://www.w3.org/2005/Atom',
    'cap' => 'urn:oasis:names:tc:emergency:cap:1.1',
    'ha' => 'http://www.alerting.net/namespace/index_1.0' }
end
process(entries) click to toggle source
# File lib/gull/client.rb, line 40
def process(entries)
  alerts = []
  entries.each do |entry|
    alert = create_instance entry
    alerts.push alert unless alert.nil?
    errors.push entry if alert.nil?
  end

  alerts
end
response() click to toggle source
# File lib/gull/client.rb, line 28
def response
  client = HTTPClient.new
  begin
    return client.get_content @options[:url]
  rescue HTTPClient::TimeoutError
    raise TimeoutError, 'Timeout while connecting to NWS web service'
  rescue HTTPClient::KeepAliveDisconnected, HTTPClient::BadResponseError,
         SocketError, Errno::ECONNREFUSED, Errno::ECONNRESET
    raise HttpError, 'Could not connect to NWS web service'
  end
end