Class: NoaaNceiWeather::Data

Inherits:
Object
  • Object
show all
Extended by:
Connection
Defined in:
lib/noaa_ncei_weather/data.rb

Overview

Class for querying against the /data endpoint of the NOAA API. This endpoint gives access to the actual measurements taken.

Constant Summary collapse

@@endpoint =

Endpoint portion of the API URL, appended to the Connection URL for requests

'data'

Instance Attribute Summary collapse

Attributes included from Connection

#token

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Connection

parse_params, request, token=, where

Constructor Details

#initialize(date, datatype, station, attributes, value) ⇒ Data

Creates a new Data object



25
26
27
28
29
30
31
# File 'lib/noaa_ncei_weather/data.rb', line 25

def initialize(date, datatype, station, attributes, value)
  @date = date
  @datatype = datatype
  @station = station
  @attributes = attributes
  @value = value
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



22
23
24
# File 'lib/noaa_ncei_weather/data.rb', line 22

def attributes
  @attributes
end

#datatypeString (readonly)

Returns The type of data measured, ID of NoaaNceiWeather::DataType.

Returns:



22
# File 'lib/noaa_ncei_weather/data.rb', line 22

attr_reader :date, :datatype, :station, :attributes, :value

#dateDateTime (readonly)

Returns The date/time this data object was measured.

Returns:

  • (DateTime)

    The date/time this data object was measured



22
23
24
# File 'lib/noaa_ncei_weather/data.rb', line 22

def date
  @date
end

#stationString (readonly)

Returns The station at which the measurement was taken, ID of Station.

Returns:

  • (String)

    The station at which the measurement was taken, ID of Station



22
# File 'lib/noaa_ncei_weather/data.rb', line 22

attr_reader :date, :datatype, :station, :attributes, :value

#valueObject (readonly)

Returns the value of attribute value.



22
# File 'lib/noaa_ncei_weather/data.rb', line 22

attr_reader :date, :datatype, :station, :attributes, :value

Class Method Details

.where(datasetid, startdate, enddate, params = {}) ⇒ Array<Data>

Retrieves a collection of NoaaNceiWeather::Data objects based on the params given.

Parameters:

  • datasetid (String, Dataset)

    A String ID for a Dataset or a DataSet object from the NOAA DB.

  • startdate (String, Date)

    A Date or ISO8601 formatted date string for the earliest data that should be retrieved

  • enddate (String, Date)

    A Date or ISO8601 formatted date string for the latest data that should be retrieved

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

    A hash including other params to set filters on the NOAA request

Options Hash (params):

  • :datatypeid (String)

    String ID of a NoaaNceiWeather::DataType within the selected dataset that should be retrieved

  • :datatype (DataType)
  • :locationid (String)

    String ID of a Location

  • :location (Location)

    Location object

  • :stationid (String)

    String ID of a Station

  • :station (Station)

    Station object

  • :units (String) — default: 'metric'

    Accepts string ‘standard’ or ‘metric’ to set the unit of measurement returned in the value

  • :sortfield (String) — default: 'id'

    Accepts string values ‘id’, ‘name, ’mindate’, ‘maxdate’, and ‘datacoverage’ to sort data before being returned

  • :sortorder (String) — default: 'asc'

    Accepts ‘asc’ or ‘desc’ for sort order

  • :limit (Integer)

    Set a limit to the amount of records returned

  • :offset (Integer) — default: 0

    Used to offset the result list

Returns:

  • (Array<Data>)

    An array of Data objects



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/noaa_ncei_weather/data.rb', line 57

def self.where(datasetid, startdate, enddate, params = {})
  datasetid = datasetid.id if datasetid.respond_to?(:id)
  params[:datasetid] = datasetid
  params[:includemetadata] = true

  startdate = Date.parse startdate if startdate.kind_of? String
  enddate = Date.parse enddate if enddate.kind_of? String
  to_date = enddate
  enddate = startdate + 365 if enddate - startdate > 365
  limit = params[:limit] if params[:limit]

  output = []
  begin
    params.merge!({startdate: startdate, enddate: enddate})

    data = super(@@endpoint, params)
    output.concat data
    if limit && output.count >= limit
      output = output[0...limit]
      break
    end
    startdate = enddate + 1
    enddate = startdate + 365
    enddate = to_date if enddate > to_date
    params[:limit] = limit
    params[:offset] = nil
  end while to_date > startdate
  output.collect {|item| self.new DateTime.parse(item['date']), item['datatype'], item['station'], item['attributes'], item['value']}
end