Class: PurpleAirApi::V1::GetSensors

Inherits:
Object
  • Object
show all
Defined in:
lib/purple_air_api/V1/sensors/get_sensors.rb

Overview

Class for requesting sensor data. This will return different response formats from the API.

Constant Summary collapse

DEFAULT_FIELDS =

The default value for fields that will be returned by PurpleAir

%w[icon name latitude longitude altitude pm2.5].freeze
DEFAULT_LOCATION_TYPE =

The default location type for the sensor

%w[outside inside].freeze
URL =

The endpoint URL

'https://api.purpleair.com/v1/sensors'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client: , **options) ⇒ GetSensors

Creates a HTTP friendly options hash depending on your inputs

Parameters:

  • client (Faraday::Connection) (defaults to: )

    Your HTTP client initialized in Client

  • options (Hash)

    Your HTTP options for the request.



32
33
34
35
36
# File 'lib/purple_air_api/V1/sensors/get_sensors.rb', line 32

def initialize(client:, **options)
  @http_client = client
  @request_options = {}
  create_options_hash(options)
end

Instance Attribute Details

#http_clientObject (readonly)

Returns the value of attribute http_client.



8
9
10
# File 'lib/purple_air_api/V1/sensors/get_sensors.rb', line 8

def http_client
  @http_client
end

#http_responseObject

Returns the value of attribute http_response.



7
8
9
# File 'lib/purple_air_api/V1/sensors/get_sensors.rb', line 7

def http_response
  @http_response
end

#parsed_responseHash

Takes the raw response from PurpleAir and generates a hash indexed by sensor index. You can use this response like a normal hash object. This GetSensorsClass.parsed_response would return a hash of data for sensor 47 with each hash key labelling the associated data.

Examples:

parsed_response example

response.parsed_response
{
  :fields=>["sensor_index", "name", "icon", "latitude", "longitude", "altitude", "pm2.5"],
  :api_version=>"V1.0.6-0.0.9",
  :time_stamp=>1614787814,
  :data_time_stamp=>nil,
  :max_age=>3600,
  :data=>
  {
    20=>{"sensor_index"=>20, "name"=>"Oakdale", "icon"=>0, "latitude"=>40.6031,
         "longitude"=>-111.8361, "altitude"=>4636, "pm2.5"=>0.0},
    47=>{"sensor_index"=>47, "name"=>"OZONE TEST", "icon"=>0, "latitude"=>40.4762,
         "longitude"=>-111.8826, "altitude"=>nil, "pm2.5"=>nil}
  }
}

Returns:

  • (Hash)


69
70
71
# File 'lib/purple_air_api/V1/sensors/get_sensors.rb', line 69

def parsed_response
  @parsed_response ||= parse_response
end

#request_optionsObject

Returns the value of attribute request_options.



7
8
9
# File 'lib/purple_air_api/V1/sensors/get_sensors.rb', line 7

def request_options
  @request_options
end

Class Method Details

.callObject

Calls initializes the class and requests the data from PurpleAir.



23
24
25
# File 'lib/purple_air_api/V1/sensors/get_sensors.rb', line 23

def self.call(...)
  new(...).request
end

Instance Method Details

#call(...) ⇒ Object

Calls initializes the class and requests the data from PurpleAir.



23
24
25
# File 'lib/purple_air_api/V1/sensors/get_sensors.rb', line 23

def self.call(...)
  new(...).request
end

#json_responseHash

Takes the raw response from PurpleAir and parses the JSON into a Hash.

Examples:

json_response example

response.json_response
{
  :api_version=>"V1.0.6-0.0.9",
  :time_stamp=>1614787814,
  :data_time_stamp=>1614787807,
  :location_type=>0,
  :max_age=>3600,
  :fields=>["sensor_index", "name", "icon", "latitude", "longitude", "altitude", "pm2.5"],
  :data=>[
            [20, "Oakdale", 0, 40.6031, -111.8361, 4636, 0.0],
            [47, "OZONE TEST", 0, 40.4762, -111.8826, nil, nil]
         ]
}

Returns:

  • (Hash)


91
92
93
# File 'lib/purple_air_api/V1/sensors/get_sensors.rb', line 91

def json_response
  @json_response ||= FastJsonparser.parse(http_response.body)
end

#requestPurpleAirApi::V1::GetSensors

Makes a get request to the PurpleAir Get Sensors Data endpoint api.purpleair.com/v1/sensors.



42
43
44
45
# File 'lib/purple_air_api/V1/sensors/get_sensors.rb', line 42

def request
  self.http_response = http_client.get(URL, request_options)
  self
end