Class: Neurio

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/neurio.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Neurio

Construct a new Neurio API instance

Parameters:

  • options (Hash)

    configuration options

Options Hash (options):

  • client_id (String)

    the Neurio client id

  • client_secret (String)

    the Neurio client secret

Raises:

  • (ArgumentError)


13
14
15
16
17
# File 'lib/neurio.rb', line 13

def initialize(options)
  @client_secret = options[:client_secret]
  @client_id = options[:client_id]
  raise ArgumentError.new("client_id and client_secret must be specified") unless @client_secret && @client_id
end

Instance Method Details

#default_sensor_idString

Look up the ID of the first (and usually only) sensor associated with this account

Returns:

  • (String)

    The Neurio sensorId



34
35
36
# File 'lib/neurio.rb', line 34

def default_sensor_id
  user["locations"][0]["sensors"][0]["sensorId"]
end

#last(sensor_id = default_sensor_id) ⇒ Neurio::Reading

Get the most recent sensor reading

Parameters:

  • sensor_id (String) (defaults to: default_sensor_id)

    The sensor id to retrieve data for

Returns:

  • (Neurio::Reading)


23
24
25
26
27
28
# File 'lib/neurio.rb', line 23

def last(sensor_id = default_sensor_id)
  headers =  {"content_type" => "application/json", "authorization" => "Bearer #{token}"}
  query = {"sensorId" => sensor_id}
  response = self.class.get("/v1/samples/live/last", :headers => headers, :query => query)
  Reading.new(response, sensor_id)
end

#live(last_timestamp = nil, sensor_id = default_sensor_id) ⇒ Array<Neurio::Reading>

Get recent sensor readings since last_timestamp.

Parameters:

  • last_timestamp (DateTime) (defaults to: nil)

    Timestamp of last sample already received. API defaults to 2 minutes ago if omitted.

  • sensor_id (String) (defaults to: default_sensor_id)

    The sensor id to retrieve data for

Returns:

  • (Array<Neurio::Reading>)

    Array of sensor readings



43
44
45
46
47
48
49
50
51
52
# File 'lib/neurio.rb', line 43

def live(last_timestamp = nil, sensor_id = default_sensor_id)
  headers =  {"content_type" => "application/json", "authorization" => "Bearer #{token}"}
  query = {"sensorId" => sensor_id}
  if last_timestamp
    query["last"] = last_timestamp.iso8601(3)
  end
  
  response = self.class.get("/v1/samples/live", :headers => headers, :query => query)
  response.map{|x| Reading.new(x, sensor_id)}
end