Class: M2X::Client::Command

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/m2x/command.rb

Overview

Wrapper for M2X Commands API

Constant Summary collapse

PATH =
"/commands"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, attributes) ⇒ Command

Returns a new instance of Command.



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

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

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



7
8
9
# File 'lib/m2x/command.rb', line 7

def attributes
  @attributes
end

Class Method Details

.list(client, params = {}) ⇒ Array

Method for List Sent Commands endpoint. Retrieve the list of recent commands sent by the current user (as given by the API key).

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/command.rb', line 47

def list(client, params={})
  res = client.get(PATH, params)

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

.send!(client, params) ⇒ Command

Method for Send Command endpoint. Send a command with the given name to the given target devices. The name should be a custom string defined by the user and understood by the device.

Parameters:

  • client (Client)

    Client API

  • params

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

Returns:

  • (Command)

    The Command that was just sent.



63
64
65
# File 'lib/m2x/command.rb', line 63

def send!(client, params)
  client.post(PATH, nil, params, "Content-Type" => "application/json")
end

Instance Method Details

#inspectObject



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

def inspect
  "<#{self.class.name}: #{attributes.inspect}>"
end

#pathObject



16
17
18
# File 'lib/m2x/command.rb', line 16

def path
  @path ||= "#{ PATH }/#{ URI.encode(@attributes.fetch("id")) }"
end

#viewCommand

Method for View Command Details endpoint. Get details of a sent command including the delivery information for all devices that were targetted by the command at the time it was sent.

Returns:

  • (Command)

    The retrieved Command



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

def view
  res = @client.get(path)

  @attributes = res.json if res.success?
end