Class: Helium::Element

Inherits:
Resource show all
Defined in:
lib/helium/element.rb

Instance Attribute Summary collapse

Attributes inherited from Resource

#id, #params, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#==, all, create, #created_at, #destroy, #eql?, find, #hash, initialize_from_path, #metadata, resource_name, #resource_name, #resource_path, singleton, #to_json, #update, #updated_at

Methods included from Utils

#datetime_to_iso, #kebab_case

Constructor Details

#initialize(opts = {}) ⇒ Element

Returns a new instance of Element



5
6
7
8
9
10
11
12
# File 'lib/helium/element.rb', line 5

def initialize(opts = {})
  super(opts)

  @name        = @params.dig("attributes", "name")
  @mac         = @params.dig("meta", "mac")
  @last_seen   = @params.dig('meta', 'last-seen')
  @device_type = @params.dig('meta', 'device-type')
end

Instance Attribute Details

#device_typeObject (readonly)

Returns the value of attribute device_type



3
4
5
# File 'lib/helium/element.rb', line 3

def device_type
  @device_type
end

#last_seenDateTime? (readonly)

Returns when the resource was last seen

Returns:

  • (DateTime, nil)

    when the resource was last seen



31
32
33
# File 'lib/helium/element.rb', line 31

def last_seen
  @last_seen
end

#macObject (readonly)

Returns the value of attribute mac



3
4
5
# File 'lib/helium/element.rb', line 3

def mac
  @mac
end

#nameObject (readonly)

Returns the value of attribute name



3
4
5
# File 'lib/helium/element.rb', line 3

def name
  @name
end

Class Method Details

.all_pathObject



18
19
20
# File 'lib/helium/element.rb', line 18

def self.all_path
  "/element?include=label"
end

Instance Method Details

#add_labels(labels_to_add = []) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/helium/element.rb', line 64

def add_labels(labels_to_add = [])
  # There's no first-class support for modifying the labels of a element in
  # the API yet, so we modify each label's relationship to the element. Once
  # this is supported in the API, this can use #add_relationships instead.
  # Same comment applies for the following 3 functions
  labels_to_add = Array(labels_to_add)
  labels_to_add.each do |label|
    label.add_elements(self)
  end
  self
end

#as_jsonObject

TODO can probably generalize this a bit more



55
56
57
58
59
60
61
62
# File 'lib/helium/element.rb', line 55

def as_json
  super.merge({
    name: name,
    mac: mac,
    last_seen: last_seen,
    device_type: device_type
  })
end

#device_configurationObject



26
27
28
# File 'lib/helium/element.rb', line 26

def device_configuration
  @client.element_device_configuration(self)
end

#labelsObject



22
23
24
# File 'lib/helium/element.rb', line 22

def labels
  Collection.new(klass: Label, client: @client, belongs_to: self)
end

#remove_labels(labels_to_remove = []) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/helium/element.rb', line 89

def remove_labels(labels_to_remove = [])
  labels_to_remove = Array(labels_to_remove)
  labels_to_remove.each do |label|
    label.remove_elements(self)
  end
  self
end

#replace_labels(labels_to_replace = []) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/helium/element.rb', line 76

def replace_labels(labels_to_replace = [])
  # To support replacement, we remove this element from each label, and then
  # add it to the specified set
  labels_to_replace = Array(labels_to_replace)
  labels.each do |label|
    label.remove_elements(self)
  end
  labels_to_replace.each do |label|
    label.add_elements(self)
  end
  self
end

#sensorsObject



14
15
16
# File 'lib/helium/element.rb', line 14

def sensors
  Collection.new(klass: Sensor, client: @client, belongs_to: self)
end

#timeseries(opts = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/helium/element.rb', line 36

def timeseries(opts = {})
  size        = opts.fetch(:size, 1000)
  port        = opts.fetch(:port, nil)
  start_time  = opts.fetch(:start_time, nil)
  end_time    = opts.fetch(:end_time, nil)
  aggtype     = opts.fetch(:aggtype, nil)
  aggsize     = opts.fetch(:aggsize, nil)

  @client.element_timeseries(self,
    size:       size,
    port:       port,
    start_time: start_time,
    end_time:   end_time,
    aggtype:    aggtype,
    aggsize:    aggsize
  )
end