Class: NWS::Client
- Inherits:
-
Object
- Object
- NWS::Client
- 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
-
#user_agent ⇒ String
readonly
User-Agent string sent with requests.
Instance Method Summary collapse
-
#get(path, params: {}) ⇒ Hash
Make a GET request to the NWS API.
-
#get_url(url) ⇒ Hash
Make a GET request to a full URL.
-
#initialize(user_agent: nil) ⇒ Client
constructor
Initialize a new NWS API client.
Constructor Details
#initialize(user_agent: nil) ⇒ Client
Initialize a new NWS API client
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_agent ⇒ String (readonly)
Returns 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
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
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 |