<commit_before>require 'httparty'
require 'pp'

module Stormpocalypse
  class Radar
    attr_reader :location, :threats
    
    # Initialize with a location (US state abbrev)
    # TODO: support multiple location types
    def initialize(location = 'ny')
      @location = location
      scan()
    end

    # Check for new threats
    def scan
      @threats = []
      entry = HTTParty.get("http://alerts.weather.gov/cap/#{@location}.atom", 
                           :format => :xml).fetch('feed').fetch('entry')

      if entry.kind_of?(Array)
        entry.each do |item|
          begin
            process_threat(item)
          rescue Exception => e
            next
          end
        end
      else
        begin
          process_threat(entry)
        rescue Exception => e
          return
        end
      end
    end

    def process_threat(item)
      return nil if item.fetch('summary').eql?("There are no active watches, warnings or advisories")

      alert = HTTParty.get(item.fetch('id'), :format => :xml).fetch('alert').fetch('info')
      threat = Threat.new(alert)
      @threats << threat
    end
  end

  class Threat
    attr_accessor :event, :summary, :category, :severity, :certainty, :locations, 
      :description, :expires_at, :instructions
    
    def initialize(alert)
      @event = alert['event']
      @summary = alert['headline']
      @description = alert['description']
      @category = alert['category']
      @instructions = alert['instructions'] || ''
      @severity = alert['severity']
      @certainty = alert['certainty']
      @expires_at = DateTime.parse(alert['expires'])
      @locations = alert['area']['areaDesc'].split('; ')
    end
  end
end<commit_msg>added FIPS code to threat location object<commit_after>require 'httparty'
require 'pp'

module Stormpocalypse
  class Radar
    attr_reader :location, :threats
    
    # Initialize with a location (US state abbrev)
    # TODO: support multiple location types
    def initialize(location = 'ny')
      @location = location
      scan()
    end

    # Check for new threats
    def scan
      @threats = []
      entry = HTTParty.get("http://alerts.weather.gov/cap/#{@location}.atom", 
                           :format => :xml).fetch('feed').fetch('entry')

      if entry.kind_of?(Array)
        entry.each do |item|
          begin
            process_threat(item)
          rescue Exception => e
            next
          end
        end
      else
        begin
          process_threat(entry)
        rescue Exception => e
          return
        end
      end
    end

    def process_threat(item)
      return nil if item.fetch('summary').eql?("There are no active watches, warnings or advisories")

      alert = HTTParty.get(item.fetch('id'), :format => :xml).fetch('alert').fetch('info')
      threat = Threat.new(alert, item.fetch('id'))
      @threats << threat
    end
  end

  class Threat
    attr_accessor :nws_id, :event, :summary, :category, :severity, :certainty, :urgency, :locations, 
      :description, :expires_at, :instructions
    
    def initialize(alert, id)
      @nws_id = id
      @event = alert['event']
      @summary = alert['headline']
      @description = alert['description']
      @category = alert['category']
      @instructions = alert['instructions'] || ''
      @severity = alert['severity']
      @certainty = alert['certainty']
      @urgency = alert['urgency']
      @expires_at = DateTime.parse(alert['expires'])
      @locations = []

      geo = alert.fetch('area').fetch('geocode')
      geo.each do |param|
        if param.fetch('valueName') == 'FIPS6'
          puts param
          @locations << param.fetch('value')
        end
      end

    end
  end
end
{'fraction_non_alphanumeric': 0.12382739212007504, 'fraction_numerical': 0.0002680246582685607, 'mean_word_length': 2.291005291005291, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6408996', 'n_tokens_mistral': 1119, 'n_tokens_neox': 1064, 'n_words': 270}