Class: M2X::Client::Device
- Includes:
- Metadata
- Defined in:
- lib/m2x/device.rb
Overview
Wrapper for M2X Device API
Constant Summary collapse
- PATH =
"/devices"
Instance Attribute Summary
Attributes inherited from Resource
Class Method Summary collapse
-
.catalog(client, params = {}) ⇒ Array
Method for List Search Public Devices Catalog endpoint.
-
.create!(client, params) ⇒ Device
Method for Create Device endpoint.
-
.list(client, params = {}) ⇒ Array
Method for List Devices endpoint.
-
.search(client, params = {}) ⇒ Array
Method for Search Devices endpoint.
-
.tags(client) ⇒ Array
Method for List Device Tags endpoint.
Instance Method Summary collapse
-
#command(id) ⇒ Command
Method for Device’s View of Command Details endpoint.
-
#commands(params = {}) ⇒ Array
Method for Device’s List of Recieved Commands endpoint.
-
#create_key(params) ⇒ Key
Method for Create Key endpoint.
-
#delete_locations!(start, stop) ⇒ Response
Method for Delete Location History endpoint.
-
#keys ⇒ Array
Method for List Keys endpoint.
-
#location ⇒ Response
Method for Read Device Location endpoint.
-
#location_history(params = {}) ⇒ Array
Method for Read Device Location History endpoint.
-
#log ⇒ Array
Method for View Request Log endpoint.
- #path ⇒ Object
-
#post_update(params) ⇒ Response
Method for - Post Device Update (Single Value to Multiple Streams) endpoint.
-
#post_updates(params) ⇒ Response
Method for - Post Device Updates (Multiple Values to Multiple Streams) endpoint.
-
#process_command(id, params = {}) ⇒ Response
Method for Process Command endpoint.
-
#reject_command(id, params = {}) ⇒ Response
Method for Reject Command endpoint.
-
#stream(name) ⇒ Stream
Method for View Data Stream endpoint.
-
#streams ⇒ Array
Method for List Data Streams endpoint.
-
#update_location(params) ⇒ Response
Method for Update Device Location endpoint.
-
#update_stream(name, params = {}) ⇒ Stream
(also: #create_stream)
Method for Create Update Data Stream endpoint.
-
#values(params = {}) ⇒ Array
Method for List Values from all Data Streams endpoint.
-
#values_export(params = {}) ⇒ Response
Method for Export Values from all Data Streams of a Device endpoint.
-
#values_search(params) ⇒ Array
Method for Search Values from all Data Streams of a Device endpoint.
Methods included from Metadata
#metadata_path, #read_metadata, #read_metadata_field, #update_metadata, #update_metadata_field
Methods inherited from Resource
#delete!, #initialize, #inspect, #refresh, #update!, #view
Constructor Details
This class inherits a constructor from M2X::Client::Resource
Class Method Details
.catalog(client, params = {}) ⇒ Array
Method for List Search Public Devices Catalog endpoint. This allows unauthenticated users to search Devices from other users that have been marked as public, allowing them to read public Device metadata, locations, streams list, and view each Devices’ stream metadata and its values.
47 48 49 50 51 |
# File 'lib/m2x/device.rb', line 47 def catalog(client, params={}) res = client.get("#{PATH}/catalog", params) res.json["devices"].map{ |atts| new(client, atts) } if res.success? end |
.create!(client, params) ⇒ Device
Method for Create Device endpoint.
70 71 72 73 74 |
# File 'lib/m2x/device.rb', line 70 def create!(client, params) res = client.post(PATH, nil, params, "Content-Type" => "application/json") new(client, res.json) if res.success? end |
.list(client, params = {}) ⇒ Array
Method for List Devices endpoint.
17 18 19 20 21 |
# File 'lib/m2x/device.rb', line 17 def list(client, params={}) res = client.get(PATH, params) res.json["devices"].map{ |atts| new(client, atts) } if res.success? end |
.search(client, params = {}) ⇒ Array
Method for Search Devices endpoint.
30 31 32 33 34 |
# File 'lib/m2x/device.rb', line 30 def search(client, params={}) res = client.get("#{PATH}/search", params) res.json["devices"].map{ |atts| new(client, atts) } if res.success? end |
.tags(client) ⇒ Array
Method for List Device Tags endpoint.
59 60 61 |
# File 'lib/m2x/device.rb', line 59 def (client) client.get("#{PATH}/tags") end |
Instance Method Details
#command(id) ⇒ Command
Method for Device’s View of Command Details endpoint. Get details of a received command including the delivery information for this device.
264 265 266 267 268 |
# File 'lib/m2x/device.rb', line 264 def command(id) res = @client.get("#{path}/commands/#{id}") M2X::Client::Command.new(@client, res.json) if res.success? end |
#commands(params = {}) ⇒ Array
Method for Device’s List of Recieved Commands endpoint.
251 252 253 254 255 |
# File 'lib/m2x/device.rb', line 251 def commands(params = {}) res = @client.get("#{path}/commands", params) res.json["commands"].map { |atts| M2X::Client::Command.new(@client, atts) } if res.success? end |
#create_key(params) ⇒ Key
Method for Create Key endpoint. Note that, according to the parameters sent, you can create a Master API Key or a Device/Stream API Key.
241 242 243 |
# File 'lib/m2x/device.rb', line 241 def create_key(params) ::M2X::Client::Key.create!(@client, params.merge(device: self["id"])) end |
#delete_locations!(start, stop) ⇒ Response
Method for Delete Location History endpoint. The ‘start` and `stop` parameters should be ISO8601 timestamps
132 133 134 135 136 |
# File 'lib/m2x/device.rb', line 132 def delete_locations!(start, stop) params = { from: start, end: stop } @client.delete("#{path}/location/waypoints", nil, params, "Content-Type" => "application/json") end |
#keys ⇒ Array
Method for List Keys endpoint.
229 230 231 |
# File 'lib/m2x/device.rb', line 229 def keys ::M2X::Client::Key.list(@client, params.merge(device: self["id"])) end |
#location ⇒ Response
Method for Read Device Location endpoint. Note that this method can return an empty value (response status of 204) if the device has no location defined.
99 100 101 |
# File 'lib/m2x/device.rb', line 99 def location @client.get("#{path}/location") end |
#location_history(params = {}) ⇒ Array
Method for Read Device Location History endpoint. Returns the 30 most recently logged locations by default.
110 111 112 |
# File 'lib/m2x/device.rb', line 110 def location_history(params = {}) @client.get("#{path}/location/waypoints", params) end |
#log ⇒ Array
Method for View Request Log endpoint. Retrieve list of HTTP requests received lately by the specified device (up to 100 entries).
88 89 90 |
# File 'lib/m2x/device.rb', line 88 def log @client.get("#{path}/log") end |
#path ⇒ Object
77 78 79 |
# File 'lib/m2x/device.rb', line 77 def path @path ||= "#{ PATH }/#{ URI.encode(@attributes.fetch("id")) }" end |
#post_update(params) ⇒ Response
Method for - Post Device Update (Single Value to Multiple Streams) endpoint. All the streams should be created before posting values using this method.
156 157 158 |
# File 'lib/m2x/device.rb', line 156 def post_update(params) @client.post("#{path}/update", nil, params, "Content-Type" => "application/json") end |
#post_updates(params) ⇒ Response
Method for - Post Device Updates (Multiple Values to Multiple Streams) endpoint. All the streams should be created before posting values using this method.
145 146 147 |
# File 'lib/m2x/device.rb', line 145 def post_updates(params) @client.post("#{path}/updates", nil, params, "Content-Type" => "application/json") end |
#process_command(id, params = {}) ⇒ Response
Method for Process Command endpoint.
277 278 279 |
# File 'lib/m2x/device.rb', line 277 def process_command(id, params = {}) @client.post("#{path}/commands/#{id}/process", nil, params) end |
#reject_command(id, params = {}) ⇒ Response
Method for Reject Command endpoint.
288 289 290 |
# File 'lib/m2x/device.rb', line 288 def reject_command(id, params = {}) @client.post("#{path}/commands/#{id}/reject", nil, params) end |
#stream(name) ⇒ Stream
Method for View Data Stream endpoint.
205 206 207 |
# File 'lib/m2x/device.rb', line 205 def stream(name) ::M2X::Client::Stream.fetch(@client, self, name) end |
#streams ⇒ Array
Method for List Data Streams endpoint.
195 196 197 |
# File 'lib/m2x/device.rb', line 195 def streams ::M2X::Client::Stream.list(@client, self) end |
#update_location(params) ⇒ Response
Method for Update Device Location endpoint.
120 121 122 |
# File 'lib/m2x/device.rb', line 120 def update_location(params) @client.put("#{path}/location", nil, params, "Content-Type" => "application/json") end |
#update_stream(name, params = {}) ⇒ Stream Also known as: create_stream
Method for Create Update Data Stream endpoint. (if a stream with this name does not exist it gets created).
217 218 219 220 221 |
# File 'lib/m2x/device.rb', line 217 def update_stream(name, params={}) stream = ::M2X::Client::Stream.new(@client, self, "name" => name) stream.update!(params) end |
#values(params = {}) ⇒ Array
Method for List Values from all Data Streams endpoint.
166 167 168 |
# File 'lib/m2x/device.rb', line 166 def values(params = {}) @client.get("#{path}/values", params) end |
#values_export(params = {}) ⇒ Response
Method for Export Values from all Data Streams of a Device endpoint.
176 177 178 |
# File 'lib/m2x/device.rb', line 176 def values_export(params = {}) @client.get("#{path}/values/export.csv", params) end |
#values_search(params) ⇒ Array
Method for Search Values from all Data Streams of a Device endpoint.
186 187 188 |
# File 'lib/m2x/device.rb', line 186 def values_search(params) @client.get("#{path}/values/search", nil, params, "Content-Type" => "application/json") end |