Class: NoaaNceiWeather::Location

Inherits:
Weather
  • Object
show all
Defined in:
lib/noaa_ncei_weather/location.rb

Overview

Class for querying against the /datacategory endpoint of the NOAA API

Constant Summary collapse

@@endpoint =

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

'locations'

Instance Attribute Summary collapse

Attributes included from Connection

#token

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Weather

all, first

Methods included from Connection

#parse_params, #request, token=, #where

Constructor Details

#initialize(id, name, datacoverage, mindate, maxdate) ⇒ Location

Creates a new instance of NoaaNceiWeather::Location



22
23
24
25
26
27
# File 'lib/noaa_ncei_weather/location.rb', line 22

def initialize(id, name, datacoverage, mindate, maxdate)
  super(id, name)
  @datacoverage = datacoverage
  @mindate = mindate
  @maxdate = maxdate
end

Instance Attribute Details

#datacoverageObject (readonly)

Returns the value of attribute datacoverage.



19
# File 'lib/noaa_ncei_weather/location.rb', line 19

attr_reader :mindate, :maxdate, :datacoverage

#idString (readonly)

Returns The unique Identifier.

Returns:

  • (String)

    The unique Identifier



19
# File 'lib/noaa_ncei_weather/location.rb', line 19

attr_reader :mindate, :maxdate, :datacoverage

#maxdateString (readonly)

Returns Latest availability of data in this location.

Returns:

  • (String)

    Latest availability of data in this location



19
# File 'lib/noaa_ncei_weather/location.rb', line 19

attr_reader :mindate, :maxdate, :datacoverage

#mindateDate (readonly)

Returns Earliest availability of data in this location.

Returns:

  • (Date)

    Earliest availability of data in this location



19
20
21
# File 'lib/noaa_ncei_weather/location.rb', line 19

def mindate
  @mindate
end

#nameString (readonly)

Returns The descriptive name.

Returns:

  • (String)

    The descriptive name



19
# File 'lib/noaa_ncei_weather/location.rb', line 19

attr_reader :mindate, :maxdate, :datacoverage

Class Method Details

.find(id) ⇒ Location?

Finds a specific instance of NoaaNceiWeather::Location by its ID

Parameters:

  • id (String)

    String ID of the resource.

Returns:



86
87
88
89
90
91
92
93
# File 'lib/noaa_ncei_weather/location.rb', line 86

def self.find(id)
  data = super(@@endpoint + "/#{id}")
  if data && data.any?
    self.new data['id'], data['name'], data['datacoverage'], Date.parse(data['mindate']), Date.parse(data['maxdate'])
  else
    nil
  end
end

.find_by_zip(zip) ⇒ Location?

Find a Location based on Zip code. Generates a locationid with the zip and

uses #find to return the object

Parameters:

  • zip (String)

    Five digit zip code

Returns:



100
101
102
# File 'lib/noaa_ncei_weather/location.rb', line 100

def self.find_by_zip(zip)
  self.find("ZIP:#{zip}")
end

.where(params = {}) ⇒ Array<Location>

Finds a set of Locations based on the parameters given

Parameters:

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

    Hash to set filters on the request sent to the NOAA API

Options Hash (params):

  • :datasetid (String)

    String ID of a Dataset

  • :dataset (Dataset)

    Dataset object

  • :locationid (String)

    String ID of a NoaaNceiWeather::Location

  • :location (Location)
  • :datacategoryid (String)

    String ID of a DataCategory

  • :datacategory (DataCategory)

    DataCategory object

  • :startdate (Date, String)

    Date or ISO formatted string to restrict data sets to those with data after this date

  • :enddate (Date, String)

    Date or ISO formatted string to restrict data sets to those with data before this date

  • :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:



123
124
125
126
127
128
129
130
# File 'lib/noaa_ncei_weather/location.rb', line 123

def self.where(params = {})
  data = super(@@endpoint, params)
  if data && data.any?
    data.collect {|item| self.new item['id'], item['name'], item['datacoverage'], Date.parse(item['mindate']), Date.parse(item['maxdate'])}
  else
    []
  end
end

Instance Method Details

#data_categories(params = {}) ⇒ Array<DataCategory>

Retrieves a collection of DataCategory objects associated with this instance

of {Location}

Parameters:

Returns:



46
47
48
49
# File 'lib/noaa_ncei_weather/location.rb', line 46

def data_categories(params = {})
  params.merge!({locationid: @id})
  DataCategory.where(params)
end

#data_sets(params = {}) ⇒ Array<Dataset>

Retrieves a collection of Dataset objects associated with this instance

of {Location}

Parameters:

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

    See Dataset.where for valid key/values.

Returns:



35
36
37
38
# File 'lib/noaa_ncei_weather/location.rb', line 35

def data_sets(params = {})
  params.merge!({locationid: @id})
  Dataset.where(params)
end

#data_types(params = {}) ⇒ Array<DataType>

Retrieves a collection of DataType objects associated with this instance

of {Location}

Parameters:

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

    See DataType.where for valid key/values.

Returns:



57
58
59
60
# File 'lib/noaa_ncei_weather/location.rb', line 57

def data_types(params = {})
  params.merge!({locationid: @id})
  DataType.where(params)
end

#location_categoryLocationCategory

Find the NoaaNceiWeather::LocationCategory that this Location belongs to

Returns:



77
78
79
80
# File 'lib/noaa_ncei_weather/location.rb', line 77

def location_category
  locationcategoryid = self.id.split(":")[0]
  LocationCategory.find(locationcategoryid)
end

#stations(params = {}) ⇒ Array<Station>

Retrieves a collection of Station objects associated with this instance

of {Location}

Parameters:

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

    See Station.where for valid key/values.

Returns:



68
69
70
71
# File 'lib/noaa_ncei_weather/location.rb', line 68

def stations(params = {})
  params.merge!({locationid: @id})
  Station.where(params)
end