helium-ruby 0.23.0 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 43e173ae51cf81b50cd37e722e907a77a0361868
4
- data.tar.gz: a7869b3f5887e95dbdab8b5cb85500b43def65ff
3
+ metadata.gz: e020a0f77221244c8f3610773410718bd4609048
4
+ data.tar.gz: a7dfe863e2ba493480596852726401696ea27814
5
5
  SHA512:
6
- metadata.gz: c6e471b7bfb6dc6064c69f3eee51b21b7c7d91cfa64de7238e0fe9ac6ccd58d38fba76458e2d943881d786c0129a5f85e08ed3bb6dae6c96b93338cc55ebffb2
7
- data.tar.gz: da14f3d09e1b1d04904317bf79177d84934528ba7d1aefdd470453ea70910e91a862e9b121122d9b514355e64817a4663faeb9245ff50272a85b60c1a95570a5
6
+ metadata.gz: 17c7b6554c56efc3e1aba9c687a98a0a6a7dbcdd7df6f5575f44238a755031b5b4f5cefa94562d42e404ac1a49099813b9e063fdc28374289f119f83b3018ae3
7
+ data.tar.gz: c0e67983f97cb527b104f27ddb3944e86f0176b7af2db386222ea186002a1edb635ca6b621dd9e6c348e3c9bd23c0680420ddacce4829fdecbf962b9aad12d4b
data/CONTRIBUTORS.md CHANGED
@@ -2,3 +2,4 @@
2
2
 
3
3
  - Andrew "Bones" Allen
4
4
  - Jared Morrow (of the Canadian Morrows)
5
+ - Lane Seppala
@@ -29,8 +29,8 @@ module Helium
29
29
  run(path, :put, opts)
30
30
  end
31
31
 
32
- def delete(path)
33
- response = run(path, :delete)
32
+ def delete(path, opts = {})
33
+ response = run(path, :delete, opts)
34
34
  response.code == 204
35
35
  end
36
36
 
@@ -13,36 +13,6 @@ module Helium
13
13
  Label.create(attributes, client: self)
14
14
  end
15
15
 
16
- # TODO incorporate this logic into Helium::Collection
17
- def update_label_sensors(label, opts = {})
18
- sensors = opts.fetch(:sensors, [])
19
-
20
- path = "/label/#{label.id}/relationships/sensor"
21
-
22
- sensors = Array(sensors)
23
-
24
- new_sensor_data = sensors.map do |sensor|
25
- {
26
- id: sensor.id,
27
- type: 'sensor'
28
- }
29
- end
30
-
31
- body = {
32
- data: new_sensor_data
33
- }
34
-
35
- response = patch(path, body: body)
36
- sensors_data = JSON.parse(response.body)["data"]
37
-
38
- # TODO: these come back deflated. need to either inflate at this point or
39
- # when needed
40
- sensors = sensors_data.map do |sensor_data|
41
- Sensor.new(client: self, params: sensor_data)
42
- end
43
-
44
- return sensors
45
- end
46
16
  end
47
17
  end
48
18
  end
@@ -80,6 +80,27 @@ module Helium
80
80
  collection.last
81
81
  end
82
82
 
83
+ # Adds resources of the same type to the collection related to the
84
+ # '@belongs_to' resource
85
+ def add_relationships(items)
86
+ body = relationship_request_body(items)
87
+ @client.post(relationship_path, body: body)
88
+ end
89
+
90
+ # Replaces resources of the same type to the collection related to the
91
+ # '@belongs_to' resource. An empty array removes all resources.
92
+ def replace_relationships(items)
93
+ body = relationship_request_body(items)
94
+ @client.patch(relationship_path, body: body)
95
+ end
96
+
97
+ # Removes resources of the same type to the collection related to the
98
+ # '@belongs_to' resource. An empty array removes all resources.
99
+ def remove_relationships(items)
100
+ body = relationship_request_body(items)
101
+ @client.delete(relationship_path, body: body)
102
+ end
103
+
83
104
  protected
84
105
 
85
106
  def add_filter_criteria(criteria)
@@ -115,6 +136,16 @@ module Helium
115
136
  uri.to_s
116
137
  end
117
138
 
139
+ def relationship_path
140
+ if @belongs_to
141
+ URI.parse("#{@belongs_to.resource_path}/relationships/#{@klass.resource_name}").to_s
142
+ else
143
+ raise Helium::Error.new(
144
+ "The collection must be associated with a resource to modify the
145
+ relationship")
146
+ end
147
+ end
148
+
118
149
  def collection_from_response(response)
119
150
  resources_data = JSON.parse(response.body)["data"]
120
151
 
@@ -125,5 +156,22 @@ module Helium
125
156
  return resources
126
157
  end
127
158
 
159
+ def relationship_request_body(items)
160
+ items = Array(items)
161
+ if items.all? {|item| item.is_a? @klass }
162
+ new_items_data = items.map do |item|
163
+ {
164
+ id: item.id,
165
+ type: "#{@klass.resource_name}"
166
+ }
167
+ end
168
+ { data: new_items_data }
169
+ else
170
+ raise Helium::Error.new(
171
+ "All items added to the collection must be of type " + @klass.to_s)
172
+ end
173
+
174
+ end
175
+
128
176
  end
129
177
  end
data/lib/helium/label.rb CHANGED
@@ -13,15 +13,18 @@ module Helium
13
13
  end
14
14
 
15
15
  def add_sensors(sensors_to_add = [])
16
- sensors_to_add = Array(sensors_to_add)
16
+ sensors.add_relationships(sensors_to_add)
17
+ self
18
+ end
17
19
 
18
- @client.update_label_sensors(self, sensors: sensors + sensors_to_add)
20
+ def replace_sensors(sensors_to_replace = [])
21
+ sensors.replace_relationships(sensors_to_replace)
22
+ self
19
23
  end
20
24
 
21
25
  def remove_sensors(sensors_to_remove = [])
22
- sensors_to_remove = Array(sensors_to_remove)
23
-
24
- @client.update_label_sensors(self, sensors: sensors - sensors_to_remove)
26
+ sensors.remove_relationships(sensors_to_remove)
27
+ self
25
28
  end
26
29
 
27
30
  # TODO can probably generalize this a bit more
data/lib/helium/sensor.rb CHANGED
@@ -83,5 +83,35 @@ module Helium
83
83
  device_type: device_type
84
84
  })
85
85
  end
86
+
87
+ def add_labels(labels_to_add = [])
88
+ # There's no first-class support for modifying the labels of a sensor in
89
+ # the API yet, so we modify each label's relationship to the sensor. Once
90
+ # this is supported in the API, this can use #add_relationships instead.
91
+ # Same comment applies for the following 3 functions
92
+ labels_to_add = Array(labels_to_add)
93
+ labels_to_add.each do |label|
94
+ label.add_sensors(self)
95
+ end
96
+ end
97
+
98
+ def replace_labels(labels_to_replace = [])
99
+ # To support replacement, we remove this sensor from each label, and then
100
+ # add it to the specified set
101
+ labels_to_replace = Array(labels_to_replace)
102
+ labels.each do |label|
103
+ label.remove_sensors(self)
104
+ end
105
+ labels_to_replace.each do |label|
106
+ label.add_sensors(self)
107
+ end
108
+ end
109
+
110
+ def remove_labels(labels_to_remove = [])
111
+ labels_to_remove = Array(labels_to_remove)
112
+ labels_to_remove.each do |label|
113
+ label.remove_sensors(self)
114
+ end
115
+ end
86
116
  end
87
117
  end
@@ -1,3 +1,3 @@
1
1
  module Helium
2
- VERSION = "0.23.0"
2
+ VERSION = "0.24.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: helium-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.23.0
4
+ version: 0.24.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Allen
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2017-02-25 00:00:00.000000000 Z
12
+ date: 2017-03-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: typhoeus