Module: NoaaNceiWeather::Connection Abstract
Overview
Constant Summary collapse
- @@url =
Base URL for the NOAA API. Endppoints are appended in concrete classes.
'https://www.ncdc.noaa.gov/cdo-web/api/v2/'
- @@token =
Note:
Each Token is restricted to five request per second and 1,000 requests per day per the NOAA documentation
Connection token required to make requests. Must be set before calling class methods from any NoaaNceiWeather module classes.
''
Instance Attribute Summary collapse
-
#token ⇒ String
writeonly
Token required to be sent with any requests.
Class Method Summary collapse
-
.token=(token) ⇒ Object
Set the request token to be used in requests to NOAA.
Instance Method Summary collapse
-
#parse_params(params) ⇒ Hash
Parses params to the format expected by the API.
-
#request(endpoint, params = {}) ⇒ Hash
Raw request sent via RestClient to the API.
-
#where(endpoint, params = {}) ⇒ Array<Hash>
Request with parameters used by child classes.
Instance Attribute Details
#token=(value) ⇒ String (writeonly)
Returns Token required to be sent with any requests. This can be obtained for free from NOAA. The token is good for 5 requests per second, 1,000 requests per day.
24 25 26 |
# File 'lib/noaa_ncei_weather/connection.rb', line 24 def self.token=(token) @@token = token end |
Class Method Details
.token=(token) ⇒ Object
Set the request token to be used in requests to NOAA. Token must be obtained from NOAA. Use this before trying to make any requests:
NoaaNceiWeather::Connection.token = 'token'
24 25 26 |
# File 'lib/noaa_ncei_weather/connection.rb', line 24 def self.token=(token) @@token = token end |
Instance Method Details
#parse_params(params) ⇒ Hash
Parses params to the format expected by the API. Allows more flexibility in what can be sent in as a parameter. Objects, Dates, and Limits are converted into strings as expected by the API.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/noaa_ncei_weather/connection.rb', line 35 def parse_params(params) # Handle passing of weather objects as parameters for other api queries objects = [:dataset, :datatype, :location, :station, :datacategory, :locationcategory] objects.each do |object| params[(object.to_s + "id").to_sym] = params.delete(object).id if params.has_key?(object) end # Handle Date, DateTime, or Time parameters # Convert to formatted string the api is expecting dates = [:startdate, :enddate] dates.each do |date| params[date] = params[date].iso8601 if params[date].respond_to?(:iso8601) end # Prep for handling requests for over the 1k NOAA limit params[:limit] = 1000 unless params[:limit] && params[:limit] < 1000 #return modified params params end |
#request(endpoint, params = {}) ⇒ Hash
Raw request sent via RestClient to the API. Used by all other requests
63 64 65 66 67 |
# File 'lib/noaa_ncei_weather/connection.rb', line 63 def request(endpoint, params = {}) url = @@url + endpoint response = RestClient::Request.execute(method: 'get', url: url, headers: {token: @@token, params: params}) JSON.parse(response.body) end |
#where(endpoint, params = {}) ⇒ Array<Hash>
Request with parameters used by child classes. Handles NOAA max limit of 1000 records by looping through the request.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/noaa_ncei_weather/connection.rb', line 76 def where(endpoint, params = {}) limit = params[:limit] || Float::INFINITY params = self.parse_params(params) output = [] begin response = self.request(endpoint, params) break unless response.any? = response['metadata']['resultset'] output.concat response['results'] count = ['offset'] + ['limit'] - 1 params[:offset] = count + 1 params[:limit] = limit - count if (limit - count) < 1000 end while count < ['count'] && count < limit && limit > 1000 output end |