<commit_before>module Barometer
  module Parser
    class NoaaForecast
      def initialize(response)
        @response = response
      end

      def parse(payload)
        _build_forecasts(payload)
        _parse_forecast_location(payload)

        @response
      end

      private

      def _parse_forecast_location(payload)
        latitude = payload.fetch('location', 'point', '@latitude')
        longitude = payload.fetch('location', 'point', '@longitude')

        @response.location.tap do |location|
          location.latitude = latitude
          location.longitude = longitude
        end

        @response.station.tap do |station|
          station.latitude = latitude
          station.longitude = longitude
        end
      end

      def _build_forecasts(payload)
        p12h_start_times = payload.fetch('time_layout').detect{|layout| layout["layout_key"] == "<KEY>"}["start_valid_time"]
        p12h_end_times = payload.fetch('time_layout').detect{|layout| layout["layout_key"] == "<KEY>"}["end_valid_time"]

        high_temps = payload.fetch('parameters', 'temperature').detect{|t| t['@type'] == 'maximum'}.fetch('value', []).map(&:to_i)
        low_temps = payload.fetch('parameters', 'temperature').detect{|t| t['@type'] == 'minimum'}.fetch('value', []).map(&:to_i)
        pops = payload.fetch('parameters', 'probability_of_precipitation', 'value').map(&:to_i)
        summaries = payload.fetch('parameters', 'weather', 'weather_conditions').map{|c| c['@weather_summary'] }
        icons = payload.fetch('parameters', 'conditions_icon', 'icon_link').map{|l| l.match(/(\w*)\.[a-zA-Z]{3}$/)[1] }

        high_temps.each_with_index do |high_temp, index|
          @response.build_forecast do |forecast_response|
            forecast_response.pop = pops[index*2]
            forecast_response.high = [:imperial, high_temp]
            forecast_response.low = [:imperial, low_temps[index]]
            forecast_response.condition = summaries[index]
            forecast_response.icon = icons[index]
            forecast_response.starts_at = p12h_start_times[index*2]
            forecast_response.ends_at = p12h_end_times[index*2]
          end

          @response.build_forecast do |forecast_response|
            forecast_response.pop = pops[index*2 + 1]
            forecast_response.high = [:imperial, high_temp]
            forecast_response.low = [:imperial, low_temps[index]]
            forecast_response.condition = summaries[index]
            forecast_response.icon = icons[index]
            forecast_response.starts_at = p12h_start_times[index*2 + 1]
            forecast_response.ends_at = p12h_end_times[index*2 + 1]
          end
        end
      end
    end
  end
end<commit_msg>cleanup Noaa forecast parser<commit_after>module Barometer
  module Parser
    class NoaaForecast
      def initialize(response)
        @response = response
      end

      def parse(payload)
        @payload = payload

        build_forecasts
        parse_location

        @response
      end

      private

      attr_reader :payload

      def build_forecasts
        each_forecast do |index, shared_index|
          @response.build_forecast do |forecast_response|
            forecast_response.starts_at = start_times[index]
            forecast_response.ends_at = end_times[index]
            forecast_response.icon = icons[shared_index]
            forecast_response.condition = summaries[shared_index]
            forecast_response.pop = precipitations[index]
            forecast_response.high = [:imperial, high_temperatures[shared_index]]
            forecast_response.low = [:imperial, low_temperatures[shared_index]]
          end
        end
      end

      def parse_location
        parse_forecast_location
        parse_forecast_station
      end

      def parse_forecast_location
        @response.location.tap do |location|
          location.latitude = latitude
          location.longitude = longitude
        end
      end

      def parse_forecast_station
        @response.station.tap do |station|
          station.latitude = latitude
          station.longitude = longitude
        end
      end

      def latitude
        @latitude ||= payload.fetch('location', 'point', '@latitude')
      end

      def longitude
        @longitude ||= payload.fetch('location', 'point', '@longitude')
      end

      def total_forecasts
        high_temperatures.count * 2
      end

      def each_forecast
        (0...total_forecasts).each do |index|
          yield index, shared_index(index)
        end
      end

      def shared_index(index)
        (index / 2).floor
      end

      def start_times
        @start_times ||= payload.fetch('time_layout').detect{|layout| layout['layout_key'] == '<KEY>'}['start_valid_time']
      end

      def end_times
        @end_times ||= payload.fetch('time_layout').detect{|layout| layout['layout_key'] == '<KEY>'}['end_valid_time']
      end

      def high_temperatures
        @high_temperatures ||= payload.fetch('parameters', 'temperature').detect{|t| t['@type'] == 'maximum'}.fetch('value', []).map(&:to_i)
      end

      def low_temperatures
        @low_temperatures ||= payload.fetch('parameters', 'temperature').detect{|t| t['@type'] == 'minimum'}.fetch('value', []).map(&:to_i)
      end

      def precipitations
        @precipitations ||= payload.fetch('parameters', 'probability_of_precipitation', 'value').map(&:to_i)
      end

      def summaries
        @summaries ||= payload.fetch('parameters', 'weather', 'weather_conditions').map{|c| c['@weather_summary'] }
      end

      def icons
        @icons ||= payload.fetch('parameters', 'conditions_icon', 'icon_link').map{|l| l.match(/(\w*)\.[a-zA-Z]{3}$/)[1] }
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.12043795620437957, 'fraction_numerical': 0.004866180048661801, 'mean_word_length': 2.656289707750953, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '6342054', 'n_tokens_mistral': 1745, 'n_tokens_neox': 1670, 'n_words': 304}