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 +4 -4
- data/CONTRIBUTORS.md +1 -0
- data/lib/helium/client/http.rb +2 -2
- data/lib/helium/client/labels.rb +0 -30
- data/lib/helium/collection.rb +48 -0
- data/lib/helium/label.rb +8 -5
- data/lib/helium/sensor.rb +30 -0
- data/lib/helium/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e020a0f77221244c8f3610773410718bd4609048
|
4
|
+
data.tar.gz: a7dfe863e2ba493480596852726401696ea27814
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 17c7b6554c56efc3e1aba9c687a98a0a6a7dbcdd7df6f5575f44238a755031b5b4f5cefa94562d42e404ac1a49099813b9e063fdc28374289f119f83b3018ae3
|
7
|
+
data.tar.gz: c0e67983f97cb527b104f27ddb3944e86f0176b7af2db386222ea186002a1edb635ca6b621dd9e6c348e3c9bd23c0680420ddacce4829fdecbf962b9aad12d4b
|
data/CONTRIBUTORS.md
CHANGED
data/lib/helium/client/http.rb
CHANGED
data/lib/helium/client/labels.rb
CHANGED
@@ -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
|
data/lib/helium/collection.rb
CHANGED
@@ -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
|
-
|
16
|
+
sensors.add_relationships(sensors_to_add)
|
17
|
+
self
|
18
|
+
end
|
17
19
|
|
18
|
-
|
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
|
-
|
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
|
data/lib/helium/version.rb
CHANGED
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.
|
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-
|
12
|
+
date: 2017-03-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: typhoeus
|