Class: M2X::Client::Stream

Inherits:
Resource show all
Defined in:
lib/m2x/stream.rb

Overview

Wrapper for AT&T M2X Data Streams API m2x.att.com/developer/documentation/v2/device

Instance Attribute Summary

Attributes inherited from Resource

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#delete!, #inspect, #refresh, #view

Constructor Details

#initialize(client, device, attributes) ⇒ Stream

Returns a new instance of Stream.



27
28
29
30
31
# File 'lib/m2x/stream.rb', line 27

def initialize(client, device, attributes)
  @client     = client
  @device     = device
  @attributes = attributes
end

Class Method Details

.fetch(client, device, name) ⇒ Object

Get details of a specific data Stream associated with a device

m2x.att.com/developer/documentation/v2/device#View-Data-Stream



11
12
13
14
15
# File 'lib/m2x/stream.rb', line 11

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) ⇒ Object

Retrieve list of data streams associated with a device.

m2x.att.com/developer/documentation/v2/device#List-Data-Streams



20
21
22
23
24
# File 'lib/m2x/stream.rb', line 20

def list(client, device)
  res = client.get("#{device.path}/streams")

  res.json["streams"].map{ |atts| new(client, device, atts) } if res.success?
end

Instance Method Details

#delete_values!(start, stop) ⇒ Object

Delete values in a stream by a date range The ‘start` and `stop` parameters should be ISO8601 timestamps

m2x.com/developer/documentation/v2/device#Delete-Data-Stream-Values



108
109
110
111
112
# File 'lib/m2x/stream.rb', line 108

def delete_values!(start, stop)
  params = { from: start, end: stop }

  @client.delete("#{path}/values", nil, params, "Content-Type" => "application/json")
end

#pathObject



33
34
35
# File 'lib/m2x/stream.rb', line 33

def path
  @path ||= "#{@device.path}/streams/#{ URI.encode(@attributes.fetch("name")) }"
end

#post_values(values) ⇒ Object

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 },
  [ ... ]
]

m2x.att.com/developer/documentation/v2/device#Post-Data-Stream-Values



98
99
100
101
102
# File 'lib/m2x/stream.rb', line 98

def post_values(values)
  params = { values: values }

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

#sampling(params) ⇒ Object

Sample values from the stream, sorted in reverse chronological order (most recent values first).

This method only works for numeric streams

m2x.att.com/developer/documentation/v2/device#Data-Stream-Sampling



61
62
63
# File 'lib/m2x/stream.rb', line 61

def sampling(params)
  @client.get("#{path}/sampling", params)
end

#stats(params = {}) ⇒ Object

Return count, min, max, average and standard deviation stats for the values of the stream.

This method only works for numeric streams

m2x.att.com/developer/documentation/v2/device#Data-Stream-Stats



71
72
73
# File 'lib/m2x/stream.rb', line 71

def stats(params={})
  @client.get("#{path}/stats", params)
end

#update!(params) ⇒ Object

Update stream properties (if the stream does not exist it gets created).

m2x.att.com/developer/documentation/v2/device#Create-Update-Data-Stream



41
42
43
44
45
# File 'lib/m2x/stream.rb', line 41

def update!(params)
  res = @client.put(path, {}, params, "Content-Type" => "application/json")

  @attributes = res.json if res.status == 201
end

#update_value(value, timestamp = nil) ⇒ Object

Update the current value of the stream. The timestamp is optional. If omitted, the current server time will be used

m2x.att.com/developer/documentation/v2/device#Update-Data-Stream-Value



79
80
81
82
83
84
85
# File 'lib/m2x/stream.rb', line 79

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 = {}) ⇒ Object

List values from the stream, sorted in reverse chronological order (most recent values first).

m2x.att.com/developer/documentation/v2/device#List-Data-Stream-Values



51
52
53
# File 'lib/m2x/stream.rb', line 51

def values(params={})
  @client.get("#{path}/values", params)
end