Class: PurpleAirApi::V1::GetSensor

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

Overview

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

Constant Summary collapse

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: , sensor_index: , read_key: nil) ⇒ GetSensor

Creates a HTTP friendly options hash depending on your inputs

Parameters:

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

    Your HTTP client initialized in Client

  • sensor_index (Integer) (defaults to: )

    The sensor_index for the sensor you want to request data from.

  • read_key (String) (defaults to: nil)

    The read_key for the sensor you want to request data from if it is a private sensor.



26
27
28
29
30
# File 'lib/purple_air_api/V1/sensors/get_sensor.rb', line 26

def initialize(client:, sensor_index:, read_key: nil)
  @http_client = client
  @request_options = {}
  create_options_hash(sensor_index, read_key)
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_sensor.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_sensor.rb', line 7

def http_response
  @http_response
end

#indexObject

Returns the value of attribute index.



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

def index
  @index
end

#request_optionsObject

Returns the value of attribute request_options.



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

def request_options
  @request_options
end

Class Method Details

.callObject

Calls initializes the class and requests the data from PurpleAir.



16
17
18
# File 'lib/purple_air_api/V1/sensors/get_sensor.rb', line 16

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

Instance Method Details

#call(...) ⇒ Object

Calls initializes the class and requests the data from PurpleAir.



16
17
18
# File 'lib/purple_air_api/V1/sensors/get_sensor.rb', line 16

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": 1615053213,
  "sensor": {
      "sensor_index": 20,
      "name": "Oakdale",
      "model": "UNKNOWN",
      "location_type": 0,
      "latitude": 40.6031,
      "longitude": -111.8361,
      "altitude": 4636,
      "last_seen": 1615053181,
      "last_modified": 1575003022,
      "private": 0,
      "channel_state": 1,
      "channel_flags_manual": 2,
      "pm1.0_a": 0.0,
      "pm2.5_a": 0.0,
      "pm10.0_a": 0.0,
      "0.3_um_count_a": 0.0,
      "0.5_um_count_a": 0.0,
      "1.0_um_count_a": 0.0,
      "2.5_um_count_a": 0.0,
      "5.0_um_count_a": 0.0,
      "10.0_um_count_a": 0.0,
      "stats_a": {
          "pm2.5": 0.0,
          "pm2.5_10minute": 0.0,
          "pm2.5_30minute": 0.0,
          "pm2.5_60minute": 0.0,
          "pm2.5_6hour": 0.0,
          "pm2.5_24hour": 0.0,
          "pm2.5_1week": 0.0,
          "time_stamp": 1615053181
      },
      "analog_input": 0.01,
      "primary_id_a": 66984,
      "primary_key_a": "TLKXL2SOJ9M0KGFK",
      "secondary_id_a": 71207,
      "secondary_key_a": "224YRSOGD8TNE5FQ",
      "primary_id_b": 209227,
      "primary_key_b": "3YF75LZ4DL7HDH9O",
      "secondary_id_b": 209228,
      "secondary_key_b": "Y02CNZZDMR15KCMX",
      "hardware": "1.0+PMSX003-O",
      "led_brightness": 0.0,
      "firmware_version": "6.01",
      "rssi": -78.0,
      "icon": 0,
      "channel_flags_auto": 0
  }
}

Returns:

  • (Hash)


105
106
107
# File 'lib/purple_air_api/V1/sensors/get_sensor.rb', line 105

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

#parsed_responseObject

Delegate to PurpleAirApi::V1::GetSensor.json_response



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

def parsed_response
  json_response
end

#requestPurpleAirApi::V1::GetSensors

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



36
37
38
39
# File 'lib/purple_air_api/V1/sensors/get_sensor.rb', line 36

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