Class: M2X::Client::Device

Inherits:
Resource show all
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

#attributes

Class Method Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • client (Client)

    Client API

  • params (defaults to: {})

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:



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.

Parameters:

  • client (Client)

    Client API

  • params

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:

  • (Device)

    newly created device.



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.

Parameters:

  • client (Client)

    Client API

  • params (defaults to: {})

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:



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.

Parameters:

  • client (Client)

    Client API

  • params (defaults to: {})

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:



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.

Parameters:

  • client (Client)

    Client API

Returns:

  • (Array)

    Device Tags associated with the account



59
60
61
# File 'lib/m2x/device.rb', line 59

def tags(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.

Parameters:

  • id (String)

    Command ID to get

Returns:



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

Parameters:

  • params (defaults to: {})

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:

  • (Array)

    List of Command objects.



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.

Parameters:

  • params

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:

  • (Key)

    The newly created 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

Parameters:

  • start (String)

    from time to delete the location history

  • stop (String)

    end time to delete the location history

Returns:

  • (Response)

    The API response, see M2X API docs for details.



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

#keysArray

Method for List Keys endpoint.

Returns:

  • (Array)

    List of Key objects.



229
230
231
# File 'lib/m2x/device.rb', line 229

def keys
  ::M2X::Client::Key.list(@client, params.merge(device: self["id"]))
end

#locationResponse

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.

Returns:

  • (Response)

    Most recently logged location of the Device, see M2X API docs for details



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.

Parameters:

  • params (defaults to: {})

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:

  • (Array)

    Location history of the Device



110
111
112
# File 'lib/m2x/device.rb', line 110

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

#logArray

Method for View Request Log endpoint. Retrieve list of HTTP requests received lately by the specified device (up to 100 entries).

Returns:

  • (Array)

    Most recent API requests made against this Device



88
89
90
# File 'lib/m2x/device.rb', line 88

def log
  @client.get("#{path}/log")
end

#pathObject



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.

Parameters:

  • params

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:

  • (Response)

    the API response, see M2X API docs for details



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.

Parameters:

  • params

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:

  • (Response)

    the API response, see M2X API docs for details



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.

Parameters:

  • id (String)

    Command ID to process

  • params (defaults to: {})

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:

  • (Response)

    The API response, see M2X API docs for details



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.

Parameters:

  • id (String)

    Command ID to process

  • params (defaults to: {})

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:

  • (Response)

    The API response, see M2X API docs for details



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.

Parameters:

  • name (String)

    name of the stream to be fetched

Returns:

  • (Stream)

    The matching stream



205
206
207
# File 'lib/m2x/device.rb', line 205

def stream(name)
  ::M2X::Client::Stream.fetch(@client, self, name)
end

#streamsArray

Method for List Data Streams endpoint.

Returns:

  • (Array)

    List of Stream objects.



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.

Parameters:

  • params

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:

  • (Response)

    The API response, see M2X API docs for details.



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).

Parameters:

  • name (String)

    Name of the stream to be updated

  • params (defaults to: {})

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:

  • (Stream)

    The Stream being updated



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.

Parameters:

  • params (defaults to: {})

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:

  • (Array)

    List of values from all the streams.



166
167
168
# File 'lib/m2x/device.rb', line 166

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

#values_export(params = {}) ⇒ Response

Parameters:

  • params (defaults to: {})

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:

  • (Response)

    the API response, see M2X API docs for details



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

Parameters:

  • params

    Query parameters passed as keyword arguments. View M2X API Docs for listing of available parameters.

Returns:

  • (Array)

    List of values matching the search criteria.



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