Class: NWS::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/nws/client.rb

Overview

HTTP client for making requests to the NWS API

Constant Summary collapse

BASE_URL =

Base URL for the NWS API

"https://api.weather.gov"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_agent: nil) ⇒ Client

Initialize a new NWS API client

Parameters:

  • user_agent (String, nil) (defaults to: nil)

    Custom User-Agent string. If nil, uses default.



19
20
21
# File 'lib/nws/client.rb', line 19

def initialize(user_agent: nil)
  @user_agent = user_agent || default_user_agent
end

Instance Attribute Details

#user_agentString (readonly)

Returns User-Agent string sent with requests.

Returns:

  • (String)

    User-Agent string sent with requests



14
15
16
# File 'lib/nws/client.rb', line 14

def user_agent
  @user_agent
end

Instance Method Details

#get(path, params: {}) ⇒ Hash

Make a GET request to the NWS API

Parameters:

  • path (String)

    API path (e.g., “/points/38.8977,-77.0365”)

  • params (Hash) (defaults to: {})

    Query parameters

Returns:

  • (Hash)

    Parsed JSON response

Raises:



32
33
34
35
36
37
38
39
40
# File 'lib/nws/client.rb', line 32

def get(path, params: {})
  uri = build_uri(path, params)
  request = Net::HTTP::Get.new(uri)
  request["User-Agent"] = user_agent
  request["Accept"] = "application/geo+json"

  response = execute_request(uri, request)
  handle_response(response)
end

#get_url(url) ⇒ Hash

Make a GET request to a full URL

Parameters:

  • url (String)

    Full URL to request

Returns:

  • (Hash)

    Parsed JSON response

Raises:



50
51
52
53
54
55
56
57
58
# File 'lib/nws/client.rb', line 50

def get_url(url)
  uri = URI.parse(url)
  request = Net::HTTP::Get.new(uri)
  request["User-Agent"] = user_agent
  request["Accept"] = "application/geo+json"

  response = execute_request(uri, request)
  handle_response(response)
end