Class: M2X::Client::Collection

Inherits:
Resource
  • Object
show all
Includes:
Metadata
Defined in:
lib/m2x/collection.rb

Overview

Wrapper for M2X Collections API

Constant Summary collapse

PATH =
"/collections"

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

.create!(client, params) ⇒ Collection

Method for Create Collection endpoint.

Parameters:

  • client (Client)

    Client API

  • params

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

Returns:



31
32
33
34
35
# File 'lib/m2x/collection.rb', line 31

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 Collections 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:



18
19
20
21
22
# File 'lib/m2x/collection.rb', line 18

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

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

Instance Method Details

#add_device(device_id) ⇒ Response

Method for Add a device to the collection endpoint.

Parameters:

  • device_id (String)

    ID of the Device being added to Collection

Returns:

  • (Response)

    The API response, see M2X API docs for details



60
61
62
# File 'lib/m2x/collection.rb', line 60

def add_device(device_id)
  @client.put("#{ path }/devices/#{ device_id }")
end

#devicesArray

Returns:

  • (Array)

    List of Device objects



48
49
50
51
52
# File 'lib/m2x/collection.rb', line 48

def devices
  res = @client.get("#{path}/devices")

  res.json["devices"].map{ |atts| M2X::Client::Device.new(@client, atts) } if res.success?
end

#pathObject



39
40
41
# File 'lib/m2x/collection.rb', line 39

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

#remove_device(device_id) ⇒ Response

Parameters:

  • device_id (String)

    ID of the Device being removed from Collection

Returns:

  • (Response)

    The API response, see M2X API docs for details



70
71
72
# File 'lib/m2x/collection.rb', line 70

def remove_device(device_id)
  @client.delete("#{ path }/devices/#{ device_id }")
end