<commit_before>module Usgs
  class Request

    def self.measurements_by(input, date_range = nil)
      response = make_api_request(input, date_range)
      return response if response[:status] == "Invalid Request"
      time_series = response["value"]["timeSeries"]
      # create a hash using gauge_id as the key
      # this accounts for the USGS api response where multiple timeSeries
      # objects exist for the same gauge_id
      streams = Hash.new
      time_series.each do |ts|
        site_id = ts["sourceInfo"]["siteCode"].first["value"]
        state_property = ts["sourceInfo"]["siteProperty"].find { |p| p["name"] == "stateCd" }
        state = state_lookup[state_property["value"]]
        values =  ts["values"].first["value"]
        unit = ts["variable"]["variableDescription"]
        measurements = []

        unless streams.key?(site_id)
          streams[site_id] = {}
          streams[site_id][:sitename] = ts["sourceInfo"]["siteName"]
          streams[site_id][:state] = state
          streams[site_id][:geolocation] =  ts["sourceInfo"]["geoLocation"]["geogLocation"]
          streams[site_id][:measurements] = measurements
        end

        values.each do |value|
          streams[site_id][:measurements] << {
            "dateTime" => value["dateTime"],
            "unit" => unit,
            "value" => value["value"]
          }
        end
      end

      # convert the hash to individual gauge objects
      streams.map do |gauge_id, data|
        Usgs::Gauge.new(
          gauge_id:  gauge_id,
          site_name: data[:sitename],
          state: data[:state],
          geo_location:  data[:geolocation],
          measurements: data[:measurements]
        )
      end

    end

    private

    # https://waterservices.usgs.gov/nwis/iv/?format=json&indent=on&sites=06614800&siteType=ST&siteStatus=active&parameterCd=00060
    def self.make_api_request(input, date_range=nil)
      base_url = "https://waterservices.usgs.gov/nwis/iv/?format=json"
      if input.length == 2 && date_range
        request_params = "&stateCd=#{input}&startDT=#{date_range.first.to_s}&endDT=#{date_range.last.to_s}"
      elsif input.length == 2
        request_params = "&stateCd=#{input}"
      elsif input.length >= 8 && date_range
        request_params = "&sites=#{input}&startDT=#{date_range.first.to_s}&endDT=#{date_range.last.to_s}"
      elsif input.length >= 8
        request_params = "&sites=#{input}"
      else
        return {:status => "Invalid Request"}
      end<commit_msg>Removes a piece of commented out code<commit_after>module Usgs
  class Request

    def self.measurements_by(input, date_range = nil)
      response = make_api_request(input, date_range)
      return response if response[:status] == "Invalid Request"
      time_series = response["value"]["timeSeries"]
      # create a hash using gauge_id as the key
      # this accounts for the USGS api response where multiple timeSeries
      # objects exist for the same gauge_id
      streams = Hash.new
      time_series.each do |ts|
        site_id = ts["sourceInfo"]["siteCode"].first["value"]
        state_property = ts["sourceInfo"]["siteProperty"].find { |p| p["name"] == "stateCd" }
        state = state_lookup[state_property["value"]]
        values =  ts["values"].first["value"]
        unit = ts["variable"]["variableDescription"]
        measurements = []

        unless streams.key?(site_id)
          streams[site_id] = {}
          streams[site_id][:sitename] = ts["sourceInfo"]["siteName"]
          streams[site_id][:state] = state
          streams[site_id][:geolocation] =  ts["sourceInfo"]["geoLocation"]["geogLocation"]
          streams[site_id][:measurements] = measurements
        end

        values.each do |value|
          streams[site_id][:measurements] << {
            "dateTime" => value["dateTime"],
            "unit" => unit,
            "value" => value["value"]
          }
        end
      end

      # convert the hash to individual gauge objects
      streams.map do |gauge_id, data|
        Usgs::Gauge.new(
          gauge_id:  gauge_id,
          site_name: data[:sitename],
          state: data[:state],
          geo_location:  data[:geolocation],
          measurements: data[:measurements]
        )
      end

    end

    private

    def self.make_api_request(input, date_range=nil)
      base_url = "https://waterservices.usgs.gov/nwis/iv/?format=json"
      if input.length == 2 && date_range
        request_params = "&stateCd=#{input}&startDT=#{date_range.first.to_s}&endDT=#{date_range.last.to_s}"
      elsif input.length == 2
        request_params = "&stateCd=#{input}"
      elsif input.length >= 8 && date_range
        request_params = "&sites=#{input}&startDT=#{date_range.first.to_s}&endDT=#{date_range.last.to_s}"
      elsif input.length >= 8
        request_params = "&sites=#{input}"
      else
        return {:status => "Invalid Request"}
      end
{'fraction_non_alphanumeric': 0.13399554023920535, 'fraction_numerical': 0.004257044394891546, 'mean_word_length': 2.726586102719033, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 3, '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': '5679785', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1499, 'n_words': 294}