|
1 | 1 | # frozen_string_literal: true |
| 2 | +require 'pry' |
2 | 3 |
|
3 | 4 | module Usgs |
4 | | - # Main client class for interacting with USGS Water Services API |
5 | | - # |
6 | | - # @example Basic usage |
7 | | - # Usgs.client.get_sites(state_cd: "CO") |
8 | | - # |
9 | | - # @example With custom timeout |
10 | | - # client = Usgs::Client.new(timeout: 60) |
11 | 5 | class Client |
12 | | - include HTTParty |
13 | | - base_uri Usgs.config.base_url |
14 | | - |
15 | 6 | include Site |
16 | 7 | include InstantaneousValues |
17 | | - include DailyValues |
| 8 | + include Statistics |
18 | 9 |
|
19 | 10 | attr_reader :timeout, :user_agent |
20 | 11 |
|
21 | | - # Initialize a new USGS client |
22 | | - # |
23 | | - # @param timeout [Integer] Request timeout in seconds (default: 30) |
24 | | - # @param user_agent [String] Custom User-Agent header |
25 | | - def initialize(timeout: 30, user_agent: "usgs-ruby/#{Usgs::VERSION}") |
26 | | - @timeout = timeout |
27 | | - @user_agent = user_agent |
| 12 | + def initialize(timeout: 30, user_agent: "usgs-ruby/#{Usgs::VERSION}", debug: false) |
| 13 | + @timeout = timeout |
| 14 | + @user_agent = user_agent |
| 15 | + @debug = debug |
| 16 | + end |
| 17 | + |
| 18 | + # Base URL for USGS Water Services |
| 19 | + def base_url |
| 20 | + "https://waterservices.usgs.gov/nwis" |
28 | 21 | end |
29 | 22 |
|
30 | | - # Perform a GET request and parse JSON response |
31 | | - # |
32 | | - # @param path [String] API endpoint path |
33 | | - # @param query [Hash] Query parameters |
34 | | - # @return [Hash] Parsed JSON response |
35 | | - def get_json(path, query = {}) |
| 23 | + # Public: Perform GET and return parsed JSON |
| 24 | + def api_get(path, query = {}) |
36 | 25 | query = query.compact |
37 | 26 | url = "#{base_url}#{path}" |
38 | 27 |
|
39 | | - response = fetch_url(url, query: query, timeout: timeout, user_agent: user_agent) |
40 | | - JSON.parse(response.body) |
| 28 | + # binding.pry |
| 29 | + fetch_url(url, query: query, timeout: timeout, user_agent: user_agent) |
41 | 30 | end |
42 | 31 |
|
43 | 32 | private |
44 | 33 |
|
45 | | - # Low-level fetch with debug support |
46 | | - # |
47 | | - # @private |
48 | 34 | def fetch_url(url, query: {}, timeout: 30, user_agent: nil) |
49 | | - uri = build_uri(url, query) |
| 35 | + uri = URI(url) |
| 36 | + uri.query = URI.encode_www_form(query) unless query.empty? |
50 | 37 |
|
51 | | - if Usgs.instance_variable_defined?(:@debug) && Usgs.instance_variable_get(:@debug) |
52 | | - puts "\n=== USGS API Request ===" |
53 | | - puts "URL: #{uri}" |
54 | | - puts "=======================\n" |
55 | | - end |
| 38 | + puts "\n=== USGS Request ===\n#{uri}\n====================\n" if $DEBUG || ENV["USGS_DEBUG"] |
56 | 39 |
|
57 | 40 | http_get(uri, timeout: timeout, user_agent: user_agent) |
58 | 41 | end |
| 42 | + |
| 43 | + def http_get(uri, timeout: 30, user_agent: nil) |
| 44 | + Net::HTTP.start( |
| 45 | + uri.host, |
| 46 | + uri.port, |
| 47 | + use_ssl: true, |
| 48 | + open_timeout: timeout, |
| 49 | + read_timeout: timeout |
| 50 | + ) do |http| |
| 51 | + request = Net::HTTP::Get.new(uri) |
| 52 | + request["User-Agent"] = user_agent if user_agent |
| 53 | + request["Accept"] = "application/json" |
| 54 | + |
| 55 | + response = http.request(request) |
| 56 | + |
| 57 | + if response.is_a?(Net::HTTPSuccess) |
| 58 | + response |
| 59 | + else |
| 60 | + raise "USGS API Error #{response.code}: #{response.message}\n#{response.body}" |
| 61 | + end |
| 62 | + end |
| 63 | + end |
59 | 64 | end |
60 | 65 | end |
0 commit comments