Module: Usgs::Utils

Included in:
DailyValues, InstantaneousValues, Site
Defined in:
lib/usgs/utils.rb

Overview

Provides utility methods for handling dates, query parameters, and API pagination.

This module contains helper methods used across the CDSS API client for data formatting, safe type conversion, and managing paginated responses.

Instance Method Summary collapse

Instance Method Details

#format_date(date) ⇒ Object



38
39
40
41
42
# File 'lib/usgs/utils.rb', line 38

def format_date(date)
  Date.parse(date.to_s).strftime("%Y-%m-%d")
rescue StandardError
  Date.today.strftime("%Y-%m-%d")
end

#format_datetime(datetime) ⇒ Object



44
45
46
47
48
# File 'lib/usgs/utils.rb', line 44

def format_datetime(datetime)
  return nil unless datetime

  Time.parse(datetime.to_s).utc.strftime("%Y-%m-%dT%H:%M")
end

#resolve_parameter_codes(codes) ⇒ Object

Convert symbols to official USGS parameter codes



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/usgs/utils.rb', line 10

def resolve_parameter_codes(codes)
  return nil if codes.nil?

  mapping = {
    discharge: "00060",
    gage_height: "00065",
    temperature: "00010",
    precipitation: "00045",
    do: "00300",
    conductivity: "00095",
    ph: "00400"
  }

  Array(codes).map do |c|
    if c.is_a?(Symbol)
      mapped = mapping[c]
      if mapped.nil?
        raise ArgumentError,
              "Invalid parameter code: #{c}. Valid codes are: #{mapping.keys.join(', ')}"
      end

      mapped
    else
      c.to_s
    end
  end.join(",")
end