helium-ruby 0.17.0 → 0.18.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dbfd4c95a1135dc05274c9817a0f162d440b4484
4
- data.tar.gz: a78dd6c5ba6f78b036fdb1282dca3e28b6e147a8
3
+ metadata.gz: b91648a288c3daa41d4c26265856513591b67986
4
+ data.tar.gz: d739e7f23b26dee61722fd96eb709e56f64c2af9
5
5
  SHA512:
6
- metadata.gz: a8c8332d1fcd0bbf5f0b398276e33c9d3be987bbe9380720f5dce9f00c317b9c3c7e487b97c904b72f1e4d4b03f5f1fdaec52313a7e3421fffccecf537203d5a
7
- data.tar.gz: d911b8b253af8e1f9da7151200f0e913f6d39fb2ff8aa05ccc2e79d2c89ae2db5454b78b525df80ab448225ac626f57b70efd0d9e1180b5f379d8061374db557
6
+ metadata.gz: 4bc90567717d28ea616d5b6399c1865756ecced3c92b47a2c4034118e1158d72316977511cadaffa3cdf2a05c0d100eff52ac5622866630b818739617932c302
7
+ data.tar.gz: c8455d1442c8fe00503d57c36f5a62ac3cb57ed31deb8792e9ff7a6cb3b4cb332728677b36f1bf003bda8799e0f92ce7cdefa93c034644ed3c96f6149e6c0d3b
@@ -15,6 +15,8 @@ require "helium/label"
15
15
  require "helium/timeseries"
16
16
  require "helium/data_point"
17
17
  require "helium/element"
18
+ require "helium/configuration"
19
+ require "helium/device_configuration"
18
20
 
19
21
  module Helium
20
22
  end
@@ -4,6 +4,8 @@ require 'helium/client/organizations'
4
4
  require 'helium/client/sensors'
5
5
  require 'helium/client/labels'
6
6
  require 'helium/client/elements'
7
+ require 'helium/client/configurations'
8
+ require 'helium/client/device_configurations'
7
9
 
8
10
  module Helium
9
11
  class Client
@@ -14,6 +16,9 @@ module Helium
14
16
  include Helium::Client::Sensors
15
17
  include Helium::Client::Labels
16
18
  include Helium::Client::Elements
19
+ include Helium::Client::Configurations
20
+ include Helium::Client::DeviceConfigurations
21
+
17
22
 
18
23
  API_VERSION = 1
19
24
  HOST = 'api.helium.com'
@@ -0,0 +1,31 @@
1
+ module Helium
2
+ class Client
3
+ module Configurations
4
+
5
+ def configurations
6
+ Configuration.all(client: self)
7
+ end
8
+
9
+ def configuration(id)
10
+ Configuration.find(id, client: self)
11
+ end
12
+
13
+ def config_device_configurations(id)
14
+ path = "/configuration/#{id}/device-configuration"
15
+ response = get(path)
16
+ device_confs_data = JSON.parse(response.body)["data"]
17
+
18
+ device_confs = device_confs_data.map do |dc|
19
+ DeviceConfiguration.new(client: self, params: dc)
20
+ end
21
+
22
+ return device_confs
23
+ end
24
+
25
+ # Configurations are immutable, so no updates are available
26
+ def create_configuration(attributes)
27
+ Configuration.create(attributes, client: self)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,64 @@
1
+ module Helium
2
+ class Client
3
+ module DeviceConfigurations
4
+
5
+ def device_configurations
6
+ DeviceConfiguration.all(client: self)
7
+ end
8
+
9
+ def device_configuration(id)
10
+ DeviceConfiguration.find(id, client: self)
11
+ end
12
+
13
+ def device_configuration_configuration(device_config)
14
+ path = "/device-configuration/#{device_config.id}/configuration"
15
+ response = get(path)
16
+ configj = JSON.parse(response.body)["data"]
17
+ config = Configuration.new(client: self, params: configj)
18
+ return config
19
+ end
20
+
21
+ def device_configuration_device(device_config)
22
+ path = "/device-configuration/#{device_config.id}/device"
23
+ response = get(path)
24
+ configj = JSON.parse(response.body)["data"]
25
+
26
+ if configj.dig("type") == "sensor"
27
+ device = Sensor.new(client: self, params: configj)
28
+ elsif configj.dig("type") == "element"
29
+ device = Element.new(client: self, params: configj)
30
+ end
31
+
32
+ return device
33
+ end
34
+
35
+ def create_device_configuration(device, configuration)
36
+ path = "/device-configuration"
37
+
38
+ body = {
39
+ data: {
40
+ type: "device-configuration",
41
+ relationships: {
42
+ configuration: {
43
+ data: {
44
+ id: configuration.id,
45
+ type: configuration.type
46
+ }
47
+ },
48
+ device: {
49
+ data: {
50
+ id: device.id,
51
+ type: device.type
52
+ }
53
+ }
54
+ }
55
+ }
56
+ }
57
+
58
+ response = post(path, body: body)
59
+ dc = JSON.parse(response.body)["data"]
60
+ return DeviceConfiguration.new(client: self, params: dc)
61
+ end
62
+ end
63
+ end
64
+ end
@@ -21,6 +21,14 @@ module Helium
21
21
  return sensors
22
22
  end
23
23
 
24
+ def element_device_configuration(element)
25
+ path = "/element/#{element.id}/device-configuration"
26
+ response = get(path)
27
+ dc_data = JSON.parse(response.body)["data"]
28
+ # dc_data is an array, but there will only be one for one
29
+ return DeviceConfiguration.new(client: self, params: dc_data[0])
30
+ end
31
+
24
32
  def element_timeseries(element, opts = {})
25
33
  path = "/element/#{element.id}/timeseries"
26
34
 
@@ -30,6 +30,14 @@ module Helium
30
30
  return labels
31
31
  end
32
32
 
33
+ def sensor_device_configuration(sensor)
34
+ path = "/sensor/#{sensor.id}/device-configuration"
35
+ response = get(path)
36
+ dc_data = JSON.parse(response.body)["data"]
37
+ # dc_data is an array, but there will only be one for one
38
+ return DeviceConfiguration.new(client: self, params: dc_data[0])
39
+ end
40
+
33
41
  def sensor_timeseries(sensor, opts = {})
34
42
  path = "/sensor/#{sensor.id}/timeseries"
35
43
 
@@ -0,0 +1,15 @@
1
+ module Helium
2
+ class Configuration < Resource
3
+ attr_reader :settings
4
+
5
+ def initialize(opts = {})
6
+ super(opts)
7
+ @settings = @params.dig('attributes')
8
+ end
9
+
10
+ def device_configurations
11
+ @client.config_device_configurations(self.id)
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ module Helium
2
+ class DeviceConfiguration < Resource
3
+ attr_reader :loaded
4
+
5
+ def initialize(opts = {})
6
+ super(opts)
7
+ @loaded = @params.dig('meta', 'loaded')
8
+ end
9
+
10
+ def device
11
+ @client.device_configuration_device(self)
12
+ end
13
+
14
+ def configuration
15
+ @client.device_configuration_configuration(self)
16
+ end
17
+
18
+ def as_json
19
+ super.merge({ loaded: loaded })
20
+ end
21
+
22
+ end
23
+ end
@@ -14,6 +14,10 @@ module Helium
14
14
  @client.element_sensors(self)
15
15
  end
16
16
 
17
+ def device_configuration
18
+ @client.element_device_configuration(self)
19
+ end
20
+
17
21
  # @return [DateTime, nil] when the resource was last seen
18
22
  def last_seen
19
23
  return nil if @last_seen.nil?
@@ -9,7 +9,12 @@ module Helium
9
9
  # @return [Helium::Error]
10
10
  def self.from_response(response)
11
11
  status = response.code
12
- message = JSON.parse(response.body)["errors"].first["detail"]
12
+ # Default the error message in the case of no error body
13
+ message = if response.body && response.body.length >= 2
14
+ JSON.parse(response.body)["errors"].first["detail"]
15
+ else
16
+ "Unknown error with code: #{response.code}"
17
+ end
13
18
 
14
19
  klass = case status
15
20
  when 401 then Helium::InvalidApiKey
@@ -2,6 +2,7 @@ module Helium
2
2
  # Abstract base class for Helium Resources returned by the API
3
3
  class Resource
4
4
  attr_reader :id, :type, :params
5
+ include Helium::Utils
5
6
 
6
7
  def initialize(opts = {})
7
8
  @client = opts.fetch(:client)
@@ -14,6 +15,8 @@ module Helium
14
15
  end
15
16
 
16
17
  class << self
18
+ include Helium::Utils
19
+
17
20
  # NOTE seems a bit out of place to be doing client work here, but it
18
21
  # makes sense for the Eigenclass to be responsible for constructing
19
22
  # instances of its inheriting class.
@@ -76,7 +79,7 @@ module Helium
76
79
  private
77
80
 
78
81
  def resource_name
79
- self.name.split('::').last.downcase
82
+ kebab_case(self.name.split('::').last)
80
83
  end
81
84
  end # << self
82
85
 
@@ -161,7 +164,7 @@ module Helium
161
164
  private
162
165
 
163
166
  def resource_name
164
- self.class.name.split('::').last.downcase
167
+ kebab_case(self.class.name.split('::').last)
165
168
  end
166
169
  end
167
170
  end
@@ -23,6 +23,10 @@ module Helium
23
23
  @client.sensor_labels(self)
24
24
  end
25
25
 
26
+ def device_configuration
27
+ @client.sensor_device_configuration(self)
28
+ end
29
+
26
30
  def timeseries(opts = {})
27
31
  size = opts.fetch(:size, 1000)
28
32
  port = opts.fetch(:port, nil)
@@ -4,5 +4,9 @@ module Helium
4
4
  return nil if datetime.nil?
5
5
  Time.parse(datetime.to_s).utc.iso8601
6
6
  end
7
+
8
+ def kebab_case(string)
9
+ string.gsub(/(.)([A-Z])/,'\1-\2').downcase
10
+ end
7
11
  end
8
12
  end
@@ -1,3 +1,3 @@
1
1
  module Helium
2
- VERSION = "0.17.0"
2
+ VERSION = "0.18.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.17.0
4
+ version: 0.18.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: 2016-11-15 00:00:00.000000000 Z
12
+ date: 2016-11-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: typhoeus
@@ -283,14 +283,18 @@ files:
283
283
  - helium-ruby.gemspec
284
284
  - lib/helium.rb
285
285
  - lib/helium/client.rb
286
+ - lib/helium/client/configurations.rb
287
+ - lib/helium/client/device_configurations.rb
286
288
  - lib/helium/client/elements.rb
287
289
  - lib/helium/client/http.rb
288
290
  - lib/helium/client/labels.rb
289
291
  - lib/helium/client/organizations.rb
290
292
  - lib/helium/client/sensors.rb
291
293
  - lib/helium/client/users.rb
294
+ - lib/helium/configuration.rb
292
295
  - lib/helium/cursor.rb
293
296
  - lib/helium/data_point.rb
297
+ - lib/helium/device_configuration.rb
294
298
  - lib/helium/element.rb
295
299
  - lib/helium/error.rb
296
300
  - lib/helium/label.rb