<commit_before>module NdfdRestApi
  class NdfdResponse
    attr_reader :error, :data

    def initialize(xml_doc)
      parsed = Nori.parse(xml_doc)
      if parsed["error"]
        @error = {:message => parsed["error"]["pre"]}
      else
        @data = parsed["dwml"]["data"]
        # pp @data
      end
    end

    def locations
      locations = @data["location"]
    end

    def num_days
      @data["time_layout"].first["layout_key"].match(/-n\d*/).to_s[2..-1].to_i
    end

    def num_locations
      location = @data["location"]
      if location.is_a? Hash
        1
      elsif location.is_a? Array
        location.length
      else
        raise TypeError.new("locations is neither Hash nor Array")
      end
    end

    def parse_locations
      locations = []
      if (num_locations == 1)
        location = @data["location"]
        point_key = location["point_key"]
        parameters = parameters_for_point(point_key)
        location["days"] = days(parameters)
        location["any_data"] = any_data?(parameters)
        locations << location
      else
        @data["location"].each{|location|
          point_key = location["location_key"]
          parameters = parameters_for_point(point_key)
          location["days"] = days(parameters)
          location["any_data"] = any_data?(parameters)
          locations << location
        }
      end
      locations
    end

    private

    def parameters_for_point(point_key)
      if @data["parameters"].is_a? Hash
        @data["parameters"]
      elsif @data["parameters"].is_a? Array
        @data["parameters"].detect{|parameter| parameter["@applicable_location"] == point_key}
      else
        raise TypeError.new("parameters is neither Hash nor Array")
      end
    end

    def days(parameters)
      days = []
      i = 0
      while (i < num_days)
        day = {}
        day["max"] = temp(parameters["temperature"], "maximum", i)
        day["min"] = temp(parameters["temperature"], "minimum", i)
        day["pop"] = probability_of_precipitation(parameters["probability_of_precipitation"], i)
        day["weather"] = weather(parameters["weather"], i)
        i += 1
        days << day
      end
      days
    end

    def temp(temperature_data, type, index)
      temp = temperature_data.detect{|temp| temp["@type"] == type}
      if (temp["value"].is_a? Array)
        temp["value"][index]
      else
        temp["value"]
      end
    end

    def probability_of_precipitation(pop_data, index)
      if (pop_data["@type"] == "12 hour")
        morning = pop_data["value"][index].to_i
        afternoon = pop_data["value"][index + 1].to_i
        day = (morning + afternoon) / 2
        {
          "morning" => morning,
          "afternoon" => afternoon,
          "day" => day
        }
      else
        {
          "day" => pop_data["value"][index]
        }
      end
    end

    def weather(weather_data, index)
      if (weather_data["@time_layout"].include? "24h")
        weather_summary(weather_data, index)
      else
        {
          "morning" => weather_summary(weather_data, index),
          "afternoon" => weather_summary(weather_data, index + 1)
        }
      end
    end

    def weather_summary(weather_data, index)
      if (weather_data["weather_conditions"].is_a? Hash)
        weather_data["weather_conditions"]["@weather_summary"]
      elsif weather_data["weather_conditions"][index]
        weather_data["weather_conditions"][index]["@weather_summary"]
      else
        weather_data["weather_conditions"][index]
      end
    end

    def any_data?(location_parameters)
      temp(location_parameters ["temperature"], "maximum", 0) != nil
    end

  end
end<commit_msg>Adding date data to each day in response. Will be of type DateTime, as parsed by Nori<commit_after>module NdfdRestApi
  class NdfdResponse
    attr_reader :error, :data

    def initialize(xml_doc)
      parsed = Nori.parse(xml_doc)
      if parsed["error"]
        @error = {:message => parsed["error"]["pre"]}
      else
        @data = parsed["dwml"]["data"]
        # pp @data
      end
    end

    def locations
      locations = @data["location"]
    end

    def num_days
      @data["time_layout"].first["layout_key"].match(/-n\d*/).to_s[2..-1].to_i
    end

    def num_locations
      location = @data["location"]
      if location.is_a? Hash
        1
      elsif location.is_a? Array
        location.length
      else
        raise TypeError.new("locations is neither Hash nor Array")
      end
    end

    def parse_locations
      locations = []
      if (num_locations == 1)
        location = @data["location"]
        point_key = location["point_key"]
        parameters = parameters_for_point(point_key)
        location["days"] = days(parameters)
        location["any_data"] = any_data?(parameters)
        locations << location
      else
        @data["location"].each{|location|
          point_key = location["location_key"]
          parameters = parameters_for_point(point_key)
          location["days"] = days(parameters)
          location["any_data"] = any_data?(parameters)
          locations << location
        }
      end
      locations
    end

    private

    def parameters_for_point(point_key)
      if @data["parameters"].is_a? Hash
        @data["parameters"]
      elsif @data["parameters"].is_a? Array
        @data["parameters"].detect{|parameter| parameter["@applicable_location"] == point_key}
      else
        raise TypeError.new("parameters is neither Hash nor Array")
      end
    end

    def days(parameters)
      days = []
      i = 0
      while (i < num_days)
        day = {}
        day["date"] = date(i)
        day["max"] = temp(parameters["temperature"], "maximum", i)
        day["min"] = temp(parameters["temperature"], "minimum", i)
        day["pop"] = probability_of_precipitation(parameters["probability_of_precipitation"], i)
        day["weather"] = weather(parameters["weather"], i)
        i += 1
        days << day
      end
      days
    end

    def date(index)
      if @data["time_layout"].first["start_valid_time"].is_a? Array
        @data["time_layout"].first["start_valid_time"][index]
      else
        @data["time_layout"].first["start_valid_time"]
      end
    end

    def temp(temperature_data, type, index)
      temp = temperature_data.detect{|temp| temp["@type"] == type}
      if (temp["value"].is_a? Array)
        temp["value"][index]
      else
        temp["value"]
      end
    end

    def probability_of_precipitation(pop_data, index)
      if (pop_data["@type"] == "12 hour")
        morning = pop_data["value"][index].to_i
        afternoon = pop_data["value"][index + 1].to_i
        day = (morning + afternoon) / 2
        {
          "morning" => morning,
          "afternoon" => afternoon,
          "day" => day
        }
      else
        {
          "day" => pop_data["value"][index]
        }
      end
    end

    def weather(weather_data, index)
      # TODO make checking for period consistent
      if (weather_data["@time_layout"].include? "24h")
        weather_summary(weather_data, index)
      else
        {
          "morning" => weather_summary(weather_data, index),
          "afternoon" => weather_summary(weather_data, index + 1)
        }
      end
    end

    def weather_summary(weather_data, index)
      if (weather_data["weather_conditions"].is_a? Hash)
        weather_data["weather_conditions"]["@weather_summary"]
      elsif weather_data["weather_conditions"][index]
        weather_data["weather_conditions"][index]["@weather_summary"]
      else
        weather_data["weather_conditions"][index]
      end
    end

    def any_data?(location_parameters)
      temp(location_parameters ["temperature"], "maximum", 0) != nil
    end

  end
end
{'fraction_non_alphanumeric': 0.12599435463176803, 'fraction_numerical': 0.00359250705671029, 'mean_word_length': 2.4083952776563184, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '3723788', 'n_tokens_mistral': 2368, 'n_tokens_neox': 2269, 'n_words': 483}