Class: M2X::Client::Collection
- 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
Class Method Summary collapse
-
.create!(client, params) ⇒ Collection
Method for Create Collection endpoint.
-
.list(client, params = {}) ⇒ Array
Method for List Collections endpoint.
Instance Method Summary collapse
-
#add_device(device_id) ⇒ Response
Method for Add a device to the collection endpoint.
-
#devices ⇒ Array
Method for List Devices from an existing collection endpoint.
- #path ⇒ Object
-
#remove_device(device_id) ⇒ Response
Method for Remove a device from the collection 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
.create!(client, params) ⇒ Collection
Method for Create Collection endpoint.
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.
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.
60 61 62 |
# File 'lib/m2x/collection.rb', line 60 def add_device(device_id) @client.put("#{ path }/devices/#{ device_id }") end |
#devices ⇒ Array
Method for List Devices from an existing collection endpoint.
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 |
#path ⇒ Object
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
Method for Remove a device from the collection endpoint.
70 71 72 |
# File 'lib/m2x/collection.rb', line 70 def remove_device(device_id) @client.delete("#{ path }/devices/#{ device_id }") end |