class WxAlert::AlertsIndex

Class to contain an index of CAP alerts, usually for a specific State or FIPS code, but may also (and by default) contain ALL NWS alerts in the US.

Constants

BAD_CODE
NONE

String constants.

Attributes

filtered[R]
fixed[R]
index[RW]

Class variables, the index itself, and two booleans: fixed and filtered, each contain internal information about the state of the index data.

Public Class Methods

new(code = 'us', filtered = false) click to toggle source

Initaialize class variables. Takes two parameters:

  • The code to retrieve, either a two character US State abbreviation or UGC code. Defaults to 'us', that is the entire US.

  • If the new AlertsIndex is a filtered version of an already-processed index.

NOTE: This does NOT call update.

# File lib/WxAlert/alertsindex.rb, line 36
def initialize(code = 'us', filtered = false)
  @code = code
  # Sets up an empty array marked as such
  @filtered = filtered
  @fixed = filtered
  @index = []
end

Public Instance Methods

exclude(key, value) click to toggle source

Filtering method. Excludes a specific alert key and value and returns the filtered index. For info on alert keys, see the docs for WxTweet::Alert

# File lib/WxAlert/alertsindex.rb, line 89
def exclude(key, value)
  # Create a fresh index marked as filtered
  match_events = AlertsIndex.new("#{@code}", true)
  # If a string is passed, change to symbol for hash
  # matching
  hash_key = Alert.str_to_sym(key)
  # If there are no alerts return.
  return match_events if no_alerts?
  # Copy the original index array into the new index array
  match_events.index.replace @index
  # Remove any matching entries
  match_events.index.delete_if { |alert| alert.match?(hash_key, value) }
  # Return the new AlertsIndex
  match_events
end
no_alerts?() click to toggle source

Check if there are no alerts, returns true if the index has no alerts. (Empty or has the non-alert entry).

# File lib/WxAlert/alertsindex.rb, line 77
def no_alerts?
  !!(@index.empty? || !@index.first.is_a?(WxAlert::Alert) ||
      @index.first.not_alert?)
end
print() click to toggle source

Prints each entry in the index.

update() click to toggle source

Updates the index via the NWS

# File lib/WxAlert/alertsindex.rb, line 54
def update
  # If this is a filtered index, it cannot
  # be updated.
  # TODO: Make an update and filter method
  return false if @filtered
  # Retrieve the fresh index from the NWS
  retrieve
  # It's new, so the data is dirty.
  @fixed = false
  # Give the data a makeover.
  make_pretty
    # If something goes wrong, rescue the method
    # and print an error.
rescue => err
  @index = ['Error']
  # maybe @index = ["#{err.message}"]
  # pp "Retrieval error: #{err.message}, #{@code}"
  # pp err.backtrace
end

Protected Instance Methods

make_pretty() click to toggle source

Index pretty-ifier, converts index to more useful format: Keys are changed to symbols, VTEC, geocodes, etc. are fixed

# File lib/WxAlert/alertsindex.rb, line 127
def make_pretty
  # Don't fix a fixed index
  return @index if @fixed

  # If the index is a single entry, put it in an
  # array
  @index = [@index] unless @index.is_a?(Array)
  # Map *in place* each raw entry and convert it into
  # an Alert.
  @index.map! { |alert| Alert.new(alert) }
  # Mark as fixed, returns true.
  @fixed = true
end
retrieve() click to toggle source

Retrieves raw alerts from a State or UGC code. Performs pre-validation.

# File lib/WxAlert/alertsindex.rb, line 166
def retrieve
  if @code =~ /^[a-z]{2}$/
    retrieve_state
  elsif @code =~ /^[A-Z]{2}[CZ]\d{3}$/
    retrieve_ugc
  else
    fail BAD_CODE
  end
end
retrieve_state() click to toggle source

Retrieve all alerts from a STATE

# File lib/WxAlert/alertsindex.rb, line 142
def retrieve_state
  # Set up a new Downloader
  dl = Downloader.get("/cap/#{@code}.atom")
  # Fail if the code isn't valid (depends on NWS)
  fail BAD_CODE if dl =~ /invalid arg x/
  # Extract the entries from the retried array
  # (generated by HTTParty)
  @index = dl['feed']['entry']
end
retrieve_ugc() click to toggle source

Retrieve all alerts from a UGC code

# File lib/WxAlert/alertsindex.rb, line 153
def retrieve_ugc
  # Set up a new Downloader
  dl = Downloader.get(
      "/cap/wwaatmget.php?x=#{@code}&y=0")
  # Fail if the code isn't valid (depends on NWS)
  fail BAD_CODE if dl =~ /404 Not Found/
  # Extract the entries from the retried array
  # (generated by HTTParty)
  @index = dl['feed']['entry']
end