Class: Particle::Product

Inherits:
Model
  • Object
show all
Defined in:
lib/particle/product.rb

Overview

Domain model for one Particle product

Constant Summary collapse

ID_REGEX =
/^\d+$/

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

attribute_reader, #attributes, #inspect

Constructor Details

#initialize(client, attributes) ⇒ Product

Returns a new instance of Product.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/particle/product.rb', line 9

def initialize(client, attributes)
  super(client, attributes)

  attributes = attributes.to_s if attributes.is_a?(Integer)

  if attributes.is_a? String
    if attributes =~ ID_REGEX
      @attributes = { id: attributes }
    else
      @attributes = { slug: attributes }
    end
  else
    # Listing all devices returns partial attributes so check if the
    # device was fully loaded or not
    @fully_loaded = true if attributes.key?(:name)
  end
end

Class Method Details

.list_pathObject



85
86
87
# File 'lib/particle/product.rb', line 85

def self.list_path
  "v1/products"
end

Instance Method Details

#add_device(device_id) ⇒ Object

Add a Particle device to product on the account

Examples:

Add a device to Product

product.add_device('12345')


45
46
47
# File 'lib/particle/product.rb', line 45

def add_device(device_id)
  @client.add_device(product: self, device_id: device_id)
end

#add_device_pathObject



89
90
91
# File 'lib/particle/product.rb', line 89

def add_device_path
  "/v1/products/#{id_or_slug}/devices"
end

#devicesObject



37
38
39
# File 'lib/particle/product.rb', line 37

def devices
  @devices = @client.get_devices(id_or_slug)
end

#devices_pathObject



101
102
103
# File 'lib/particle/product.rb', line 101

def devices_path
  "/v1/products/#{id_or_slug}/devices"
end

#firmware(target) ⇒ Object



57
58
59
# File 'lib/particle/product.rb', line 57

def firmware(target)
  @client.product_firmware(self, target)
end

#firmware_path(version) ⇒ Object



105
106
107
# File 'lib/particle/product.rb', line 105

def firmware_path(version)
  "/v1/products/#{id_or_slug}/firmware/#{version}"
end

#firmware_upload_pathObject



109
110
111
# File 'lib/particle/product.rb', line 109

def firmware_upload_path
  "/v1/products/#{id_or_slug}/firmware"
end

#get_attributesObject



32
33
34
35
# File 'lib/particle/product.rb', line 32

def get_attributes
  @loaded = @fully_loaded = true
  @attributes = @client.product_attributes(self)
end

#idObject



66
67
68
69
# File 'lib/particle/product.rb', line 66

def id
  get_attributes unless @attributes[:id]
  @attributes[:id]
end

#id_or_slugObject



81
82
83
# File 'lib/particle/product.rb', line 81

def id_or_slug
  @attributes[:id] || @attributes[:slug]
end

#organizationObject



76
77
78
79
# File 'lib/particle/product.rb', line 76

def organization
  get_attributes unless @attributes[:organization] || @attributes[:org]
  @attributes[:organization] || @attributes[:org]
end

#pathObject



97
98
99
# File 'lib/particle/product.rb', line 97

def path
  "/v1/products/#{id_or_slug}"
end

#remove_device(device_id) ⇒ Object

Remove a Particle device from a product on the account

Examples:

Remove a device from Product

product.remove_device('12345')


53
54
55
# File 'lib/particle/product.rb', line 53

def remove_device(device_id)
  @client.remove_product_device(product: self, device_id: device_id)
end

#remove_device_path(device_id) ⇒ Object



93
94
95
# File 'lib/particle/product.rb', line 93

def remove_device_path(device_id)
  "/v1/products/#{id_or_slug}/devices/#{device_id}"
end

#slugObject



71
72
73
74
# File 'lib/particle/product.rb', line 71

def slug
  get_attributes unless @attributes[:slug]
  @attributes[:slug]
end

#upload_firmware(version, title, binary, desc = nil) ⇒ Object



61
62
63
64
# File 'lib/particle/product.rb', line 61

def upload_firmware(version, title, binary, desc = nil)
  params = { version: version, title: title, binary: binary, description: desc }
  @client.upload_product_firmware(self, params)
end