helium-ruby 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -0
  3. data/README.md +3 -3
  4. data/docs/Helium.html +4 -4
  5. data/docs/Helium/Client.html +9 -9
  6. data/docs/Helium/Client/Elements.html +8 -110
  7. data/docs/Helium/Client/Http.html +31 -33
  8. data/docs/Helium/Client/Labels.html +38 -218
  9. data/docs/Helium/Client/Organizations.html +1 -1
  10. data/docs/Helium/Client/Sensors.html +30 -214
  11. data/docs/Helium/Client/Users.html +1 -1
  12. data/docs/Helium/ClientError.html +153 -0
  13. data/docs/Helium/Cursor.html +8 -8
  14. data/docs/Helium/DataPoint.html +9 -9
  15. data/docs/Helium/Element.html +16 -79
  16. data/docs/Helium/Error.html +267 -0
  17. data/docs/Helium/InvalidApiKey.html +157 -0
  18. data/docs/Helium/Label.html +29 -133
  19. data/docs/Helium/Organization.html +8 -8
  20. data/docs/Helium/Resource.html +717 -43
  21. data/docs/Helium/Sensor.html +31 -132
  22. data/docs/Helium/User.html +8 -8
  23. data/docs/Helium/Utils.html +1 -1
  24. data/docs/_index.html +33 -4
  25. data/docs/class_list.html +1 -1
  26. data/docs/file.README.html +4 -4
  27. data/docs/index.html +4 -4
  28. data/docs/method_list.html +112 -144
  29. data/docs/top-level-namespace.html +1 -1
  30. data/lib/helium.rb +1 -0
  31. data/lib/helium/client.rb +3 -3
  32. data/lib/helium/client/elements.rb +10 -27
  33. data/lib/helium/client/http.rb +38 -19
  34. data/lib/helium/client/labels.rb +7 -53
  35. data/lib/helium/client/sensors.rb +5 -54
  36. data/lib/helium/cursor.rb +6 -6
  37. data/lib/helium/data_point.rb +5 -5
  38. data/lib/helium/element.rb +7 -8
  39. data/lib/helium/error.rb +28 -0
  40. data/lib/helium/label.rb +3 -11
  41. data/lib/helium/organization.rb +4 -4
  42. data/lib/helium/resource.rb +103 -5
  43. data/lib/helium/sensor.rb +13 -15
  44. data/lib/helium/user.rb +4 -4
  45. data/lib/helium/version.rb +1 -1
  46. metadata +6 -2
@@ -2,11 +2,11 @@ module Helium
2
2
  class Organization < Resource
3
3
  attr_reader :name, :timezone
4
4
 
5
- def initialize(client:, params:)
6
- super(client: client, params: params)
5
+ def initialize(opts = {})
6
+ super(opts)
7
7
 
8
- @name = params.dig('attributes', 'name')
9
- @timezone = params.dig('attributes', 'timezone')
8
+ @name = @params.dig('attributes', 'name')
9
+ @timezone = @params.dig('attributes', 'timezone')
10
10
  end
11
11
 
12
12
  # TODO refactor into relationships
@@ -3,11 +3,103 @@ module Helium
3
3
  class Resource
4
4
  attr_reader :id
5
5
 
6
- def initialize(client:, params:)
7
- @client = client
8
- @id = params["id"]
9
- @created_at = params.dig('meta', 'created')
10
- @updated_at = params.dig('meta', 'updated')
6
+ def initialize(opts = {})
7
+ @client = opts.fetch(:client)
8
+ @params = opts.fetch(:params)
9
+
10
+ @id = @params["id"]
11
+ @created_at = @params.dig('meta', 'created')
12
+ @updated_at = @params.dig('meta', 'updated')
13
+ end
14
+
15
+ class << self
16
+ # NOTE seems a bit out of place to be doing client work here, but it
17
+ # makes sense for the Eigenclass to be responsable for constructing
18
+ # instances of its inheriting class.
19
+
20
+ # Returns all resources
21
+ # @option opts [Client] :client A Helium::Client
22
+ # @return [Array<Resource>] an Array of all of the inheriting Resource
23
+ def all(opts = {})
24
+ client = opts.fetch(:client)
25
+
26
+ response = client.get("/#{resource_name}")
27
+ resources_data = JSON.parse(response.body)["data"]
28
+
29
+ resources = resources_data.map do |resource_data|
30
+ self.new(client: client, params: resource_data)
31
+ end
32
+
33
+ return resources
34
+ end
35
+
36
+ # Finds a single Resource by id
37
+ # @param id [String] An id to find the Resource
38
+ # @option opts [Client] :client A Helium::Client
39
+ # @return [Resource]
40
+ def find(id, opts = {})
41
+ client = opts.fetch(:client)
42
+
43
+ response = client.get("/#{resource_name}/#{id}")
44
+ resource_data = JSON.parse(response.body)["data"]
45
+
46
+ return self.new(client: client, params: resource_data)
47
+ end
48
+
49
+ # Creates a new resource with given attributes
50
+ # @param attributes [Hash] The attributes for the new Resource
51
+ # @option opts [Client] :client A Helium::Client
52
+ # @return [Resource]
53
+ def create(attributes, opts = {})
54
+ client = opts.fetch(:client)
55
+
56
+ path = "/#{resource_name}"
57
+
58
+ body = {
59
+ data: {
60
+ attributes: attributes,
61
+ type: resource_name
62
+ }
63
+ }
64
+
65
+ response = client.post(path, body: body)
66
+ resource_data = JSON.parse(response.body)["data"]
67
+
68
+ return self.new(client: client, params: resource_data)
69
+ end
70
+
71
+ private
72
+
73
+ def resource_name
74
+ self.name.split('::').last.downcase
75
+ end
76
+ end # << self
77
+
78
+ # Updates a Resource
79
+ # @param attributes [Hash] The attributes to update
80
+ # @return [Resource] The updated resource
81
+ def update(attributes)
82
+ path = "/#{resource_name}/#{self.id}"
83
+
84
+ body = {
85
+ data: {
86
+ attributes: attributes,
87
+ id: self.id,
88
+ type: resource_name
89
+ }
90
+ }
91
+
92
+ response = @client.patch(path, body: body)
93
+ resource_data = JSON.parse(response.body)["data"]
94
+
95
+ return self.class.new(client: self, params: resource_data)
96
+ end
97
+
98
+ # Deletes the Resource
99
+ # @return [Boolean] Whether the operation was successful
100
+ def destroy
101
+ path = "/#{resource_name}/#{self.id}"
102
+ @client.delete(path)
11
103
  end
12
104
 
13
105
  # Override equality to use id for comparisons
@@ -54,5 +146,11 @@ module Helium
54
146
  def to_json(*options)
55
147
  as_json.to_json(*options)
56
148
  end
149
+
150
+ private
151
+
152
+ def resource_name
153
+ self.class.name.split('::').last.downcase
154
+ end
57
155
  end
58
156
  end
@@ -2,15 +2,22 @@ module Helium
2
2
  class Sensor < Resource
3
3
  attr_reader :name, :mac, :ports
4
4
 
5
- def initialize(client:, params:)
6
- super(client: client, params: params)
5
+ def initialize(opts = {})
6
+ super(opts)
7
7
 
8
- @name = params.dig('attributes', 'name')
9
- @mac = params.dig('meta', 'mac')
10
- @ports = params.dig('meta', 'ports')
8
+ @name = @params.dig('attributes', 'name')
9
+ @mac = @params.dig('meta', 'mac')
10
+ @ports = @params.dig('meta', 'ports')
11
11
  end
12
12
 
13
- def timeseries(size: 1000, port: nil, start_time: nil, end_time: nil, aggtype: nil, aggsize: nil)
13
+ def timeseries(opts = {})
14
+ size = opts.fetch(:size, 1000)
15
+ port = opts.fetch(:port, nil)
16
+ start_time = opts.fetch(:start_time, nil)
17
+ end_time = opts.fetch(:end_time, nil)
18
+ aggtype = opts.fetch(:aggtype, nil)
19
+ aggsize = opts.fetch(:aggsize, nil)
20
+
14
21
  @client.sensor_timeseries(self,
15
22
  size: size,
16
23
  port: port,
@@ -21,15 +28,6 @@ module Helium
21
28
  )
22
29
  end
23
30
 
24
- # TODO CRUD methods will be generalized into the Resource object
25
- def update(name:)
26
- @client.update_sensor(self, name: name)
27
- end
28
-
29
- def destroy
30
- @client.delete_sensor(self)
31
- end
32
-
33
31
  # TODO can probably generalize this a bit more
34
32
  def as_json
35
33
  super.merge({
@@ -2,11 +2,11 @@ module Helium
2
2
  class User < Resource
3
3
  attr_reader :name, :email
4
4
 
5
- def initialize(client:, params:)
6
- super(client: client, params: params)
5
+ def initialize(opts = {})
6
+ super(opts)
7
7
 
8
- @name = params.dig('attributes', 'name')
9
- @email = params.dig('meta', 'email')
8
+ @name = @params.dig('attributes', 'name')
9
+ @email = @params.dig('meta', 'email')
10
10
  end
11
11
 
12
12
  # TODO can probably generalize this a bit more
@@ -1,3 +1,3 @@
1
1
  module Helium
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: helium-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Allen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-23 00:00:00.000000000 Z
11
+ date: 2016-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus
@@ -252,9 +252,12 @@ files:
252
252
  - docs/Helium/Client/Organizations.html
253
253
  - docs/Helium/Client/Sensors.html
254
254
  - docs/Helium/Client/Users.html
255
+ - docs/Helium/ClientError.html
255
256
  - docs/Helium/Cursor.html
256
257
  - docs/Helium/DataPoint.html
257
258
  - docs/Helium/Element.html
259
+ - docs/Helium/Error.html
260
+ - docs/Helium/InvalidApiKey.html
258
261
  - docs/Helium/Label.html
259
262
  - docs/Helium/Organization.html
260
263
  - docs/Helium/Resource.html
@@ -287,6 +290,7 @@ files:
287
290
  - lib/helium/cursor.rb
288
291
  - lib/helium/data_point.rb
289
292
  - lib/helium/element.rb
293
+ - lib/helium/error.rb
290
294
  - lib/helium/label.rb
291
295
  - lib/helium/organization.rb
292
296
  - lib/helium/resource.rb