Module: Particle::Client::Products

Included in:
Particle::Client
Defined in:
lib/particle/client/products.rb

Overview

Client methods for the Particle product API

Instance Method Summary collapse

Instance Method Details

#add_device(product:, device_id:) ⇒ Hash

Add device to Particle product on the account

Parameters:

  • product (Product)

    A product to interact with

  • device_id (String)

    A device id

Returns:

  • (Hash)

    JSON response as a hash



56
57
58
# File 'lib/particle/client/products.rb', line 56

def add_device(product:, device_id:)
  post(product.add_device_path, id: device_id)
end

#get_devices(target) ⇒ Array<Device>

List all Particle product devices on the account

Returns:

  • (Array<Device>)

    List of Particle product devices to interact with



46
47
48
49
# File 'lib/particle/client/products.rb', line 46

def get_devices(target)
  response_body = get(product(target).devices_path)
  (response_body[:devices]).map { |attributes| device(attributes) }
end

#product(target) ⇒ Product

Create a domain model for a Particle product

Parameters:

  • target (String, Hash, Product)

    A product id, slug, hash of attributes or Product object

Returns:

  • (Product)

    A product object to interact with



13
14
15
16
17
18
19
# File 'lib/particle/client/products.rb', line 13

def product(target)
  if target.is_a? Product
    target
  else
    Product.new(self, target)
  end
end

#product_attributes(target) ⇒ Hash

Get information about a Particle product

Parameters:

Returns:

  • (Hash)

    The product attributes



33
34
35
36
37
38
39
40
41
# File 'lib/particle/client/products.rb', line 33

def product_attributes(target)
  response_body = get(product(target).path)

  # originally returned as an array, now seems to just return the 1 product as a hash;
  # this will handle both cases
  product = response_body[:product]
  product = product.first if product.is_a?(Array)
  product
end

#productsArray<Product>

List all Particle products on the account

Returns:

  • (Array<Product>)

    List of Particle products to interact with



24
25
26
27
# File 'lib/particle/client/products.rb', line 24

def products
  response_body = get(Product.list_path)
  (response_body[:products]).map { |attributes| product(attributes) }
end

#remove_product_device(product:, device_id:) ⇒ Hash

Remove device from a Particle product on the account

Parameters:

  • product (Product)

    A product to interact with

  • device_id (String)

    A device id

Returns:

  • (Hash)

    JSON response as a hash



65
66
67
# File 'lib/particle/client/products.rb', line 65

def remove_product_device(product:, device_id:)
  delete(product.remove_device_path(device_id))
end