class M2X::Client::Stream

Wrapper for {m2x.att.com/developer/documentation/v2/device M2X Data Streams} API.

Public Class Methods

fetch(client, device, name) click to toggle source

Method for {m2x.att.com/developer/documentation/v2/device#View-Data-Stream View Data Streams} endpoint.

@param {Client} client Client API @param (String) device device to fetch @param (String) name of the stream to be fetched @return {Stream} fetched stream

# File lib/m2x/stream.rb, line 15
def fetch(client, device, name)
  res = client.get("#{device.path}/streams/#{URI.encode(name)}")

  new(client, device, res.json) if res.success?
end
list(client, device) click to toggle source

Method for {m2x.att.com/developer/documentation/v2/device#List-Data-Streams List Data Streams} endpoint.

@param {Client} client Client API @param (String) device device to get its data streams @return (Array) List of {Stream} objects

# File lib/m2x/stream.rb, line 28
def list(client, device)
  res = client.get("#{device.path}/streams")

  res.json["streams"].map{ |atts| new(client, device, atts) } if res.success?
end
new(client, device, attributes) click to toggle source
# File lib/m2x/stream.rb, line 35
def initialize(client, device, attributes)
  @client     = client
  @device     = device
  @attributes = attributes
end

Public Instance Methods

delete_values!(start, stop) click to toggle source

Method for {m2x.com/developer/documentation/v2/device#Delete-Data-Stream-Values Delete Data Stream Values} endpoint. The `start` and `stop` parameters should be ISO8601 timestamps

@param (String) start from time to delete the data @param (String) stop end time to delete the data @return {Response} The API response, see M2X API docs for details

# File lib/m2x/stream.rb, line 134
def delete_values!(start, stop)
  params = { from: start, end: stop }

  @client.delete("#{path}/values", nil, params, "Content-Type" => "application/json")
end
path() click to toggle source
# File lib/m2x/stream.rb, line 41
def path
  @path ||= "#{@device.path}/streams/#{ URI.encode(@attributes.fetch("name")) }"
end
post_values(values) click to toggle source

Method for {m2x.att.com/developer/documentation/v2/device#Post-Data-Stream-Values Post Data Stream Values} endpoint. Post multiple values to the stream The `values` parameter is an array with the following format:

[
  { "timestamp": <Time in ISO8601>, "value": x },
  { "timestamp": <Time in ISO8601>, "value": y },
  [ ... ]
]

@param values The values being posted, formatted according to the API docs @return {Response} The API response, see M2X API docs for details

# File lib/m2x/stream.rb, line 120
def post_values(values)
  params = { values: values }

  @client.post("#{path}/values", nil, params, "Content-Type" => "application/json")
end
sampling(params) click to toggle source

Method for {m2x.att.com/developer/documentation/v2/device#Data-Stream-Sampling Data Stream Sampling} endpoint. This method only works for numeric streams

@param params Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters. @return (Array) List of sample values from the stream

# File lib/m2x/stream.rb, line 74
def sampling(params)
  @client.get("#{path}/sampling", params)
end
stats(params={}) click to toggle source

Method for {m2x.att.com/developer/documentation/v2/device#Data-Stream-Stats Data Stream Stats} endpoint. Returns the count, min, max, average and standard deviation stats for the values of the stream. This method only works for numeric streams

@param params Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters. @return (Array) Stats of the stream

# File lib/m2x/stream.rb, line 87
def stats(params={})
  @client.get("#{path}/stats", params)
end
update!(params) click to toggle source

Method for {m2x.att.com/developer/documentation/v2/device#Create-Update-Data-Stream Create Update Data Stream} endpoint.

@param params Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters. @return {Stream} The newly created stream

# File lib/m2x/stream.rb, line 51
def update!(params)
  @client.put(path, {}, params, "Content-Type" => "application/json").tap do |res|
    @attributes = res.json if res.status == 201
  end
end
update_value(value, timestamp=nil) click to toggle source

Method for {m2x.att.com/developer/documentation/v2/device#Update-Data-Stream-Value Update Data Stream Value} endpoint. The timestamp is optional. If omitted, the current server time will be used

@param (String) value Value to be updated @param (String) timestamp Current Timestamp @return void

# File lib/m2x/stream.rb, line 99
def update_value(value, timestamp=nil)
  params = { value: value }

  params[:timestamp] = timestamp if timestamp

  @client.put("#{path}/value", nil, params, "Content-Type" => "application/json")
end
values(params={}) click to toggle source

Method for {m2x.att.com/developer/documentation/v2/device#List-Data-Stream-Values List Data Stream Values} endpoint.

@param params Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters. @return (Array) List of values from the stream

# File lib/m2x/stream.rb, line 63
def values(params={})
  @client.get("#{path}/values", params)
end