helium-ruby 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +31 -0
  3. data/.csslintrc +2 -0
  4. data/.eslintignore +1 -0
  5. data/.eslintrc +213 -0
  6. data/.gitignore +0 -1
  7. data/.rubocop.yml +1156 -0
  8. data/CONTRIBUTING.md +33 -0
  9. data/LICENSE.txt +23 -17
  10. data/README.md +58 -47
  11. data/Rakefile +14 -0
  12. data/bin/console +8 -0
  13. data/docs/Helium.html +131 -0
  14. data/docs/Helium/Client.html +493 -0
  15. data/docs/Helium/Client/Http.html +428 -0
  16. data/docs/Helium/Client/Organizations.html +251 -0
  17. data/docs/Helium/Client/Sensors.html +543 -0
  18. data/docs/Helium/Client/Users.html +185 -0
  19. data/docs/Helium/Cursor.html +305 -0
  20. data/docs/Helium/DataPoint.html +707 -0
  21. data/docs/Helium/Organization.html +632 -0
  22. data/docs/Helium/Sensor.html +820 -0
  23. data/docs/Helium/User.html +563 -0
  24. data/docs/Helium/Utils.html +183 -0
  25. data/docs/_index.html +226 -0
  26. data/docs/class_list.html +51 -0
  27. data/docs/css/common.css +1 -0
  28. data/docs/css/full_list.css +58 -0
  29. data/docs/css/style.css +474 -0
  30. data/docs/file.README.html +266 -0
  31. data/docs/file_list.html +56 -0
  32. data/docs/frames.html +17 -0
  33. data/docs/index.html +266 -0
  34. data/docs/js/app.js +243 -0
  35. data/docs/js/full_list.js +216 -0
  36. data/docs/js/jquery.js +4 -0
  37. data/docs/method_list.html +475 -0
  38. data/docs/top-level-namespace.html +112 -0
  39. data/helium-ruby.gemspec +2 -0
  40. data/lib/helium.rb +1 -1
  41. data/lib/helium/client.rb +0 -16
  42. data/lib/helium/client/http.rb +50 -9
  43. data/lib/helium/client/sensors.rb +51 -28
  44. data/lib/helium/cursor.rb +54 -0
  45. data/lib/helium/data_point.rb +3 -3
  46. data/lib/helium/sensor.rb +9 -0
  47. data/lib/helium/version.rb +1 -1
  48. metadata +63 -3
  49. data/lib/helium/timeseries.rb +0 -35
@@ -0,0 +1,112 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>
7
+ Top Level Namespace
8
+
9
+ &mdash; Documentation by YARD 0.9.3
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
14
+
15
+ <link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
16
+
17
+ <script type="text/javascript" charset="utf-8">
18
+ pathId = "";
19
+ relpath = '';
20
+ </script>
21
+
22
+
23
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
24
+
25
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
26
+
27
+
28
+ </head>
29
+ <body>
30
+ <div class="nav_wrap">
31
+ <iframe id="nav" src="class_list.html"></iframe>
32
+ <div id="resizer"></div>
33
+ </div>
34
+
35
+ <div id="main" tabindex="-1">
36
+ <div id="header">
37
+ <div id="menu">
38
+
39
+ <a href="_index.html">Index</a> &raquo;
40
+
41
+
42
+ <span class="title">Top Level Namespace</span>
43
+
44
+ </div>
45
+
46
+ <div id="search">
47
+
48
+ <a class="full_list_link" id="class_list_link"
49
+ href="class_list.html">
50
+
51
+ <svg width="24" height="24">
52
+ <rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
53
+ <rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
54
+ <rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
55
+ </svg>
56
+ </a>
57
+
58
+ </div>
59
+ <div class="clear"></div>
60
+ </div>
61
+
62
+ <iframe id="search_frame" src="class_list.html"></iframe>
63
+
64
+ <div id="content"><h1>Top Level Namespace
65
+
66
+
67
+
68
+ </h1>
69
+ <div class="box_info">
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+ </div>
82
+
83
+ <h2>Defined Under Namespace</h2>
84
+ <p class="children">
85
+
86
+
87
+ <strong class="modules">Modules:</strong> <span class='object_link'><a href="Helium.html" title="Helium (module)">Helium</a></span>
88
+
89
+
90
+
91
+
92
+ </p>
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+ </div>
103
+
104
+ <div id="footer">
105
+ Generated on Thu Aug 18 10:50:33 2016 by
106
+ <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
107
+ 0.9.3 (ruby-2.3.1).
108
+ </div>
109
+
110
+ </div>
111
+ </body>
112
+ </html>
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
28
28
  spec.require_paths = ["lib"]
29
29
 
30
30
  spec.add_dependency "typhoeus", "~> 1.1.0"
31
+ spec.add_dependency "ruby_dig", "~> 0.0.2"
31
32
 
32
33
  spec.add_development_dependency "bundler", "~> 1.12"
33
34
  spec.add_development_dependency "rake", "~> 10.0"
@@ -41,4 +42,5 @@ Gem::Specification.new do |spec|
41
42
  spec.add_development_dependency "terminal-notifier-guard", "~> 1.7.0"
42
43
  spec.add_development_dependency "simplecov", "~> 0.12.0"
43
44
  spec.add_development_dependency "human_time", "~> 0.2.0"
45
+ spec.add_development_dependency "yard", "~> 0.9.3"
44
46
  end
@@ -5,10 +5,10 @@ require 'date'
5
5
  require "helium/version"
6
6
  require "helium/utils"
7
7
  require "helium/client"
8
+ require "helium/cursor"
8
9
  require "helium/user"
9
10
  require "helium/organization"
10
11
  require "helium/sensor"
11
- require "helium/timeseries"
12
12
  require "helium/data_point"
13
13
 
14
14
  module Helium
@@ -11,16 +11,6 @@ module Helium
11
11
  include Helium::Client::Organizations
12
12
  include Helium::Client::Sensors
13
13
 
14
- API_VERSION = 1
15
- HOST = 'api.helium.com'
16
- PROTOCOL = 'https'
17
-
18
- BASE_HTTP_HEADERS = {
19
- 'Accept' => 'application/json',
20
- 'Content-Type' => 'application/json',
21
- 'User-Agent' => 'helium-ruby'
22
- }
23
-
24
14
  attr_accessor :api_key
25
15
 
26
16
  def initialize(api_key:, debug: false)
@@ -35,11 +25,5 @@ module Helium
35
25
  def debug?
36
26
  @debug == true
37
27
  end
38
-
39
- def http_headers
40
- BASE_HTTP_HEADERS.merge({
41
- 'Authorization' => api_key
42
- })
43
- end
44
28
  end
45
29
  end
@@ -1,26 +1,67 @@
1
1
  module Helium
2
2
  class Client
3
3
  module Http
4
- def get(path = nil, url: nil, options: {})
5
- path = path.gsub(/^\//, '') if path
6
- url ||= "#{PROTOCOL}://#{HOST}/v#{API_VERSION}/#{path}"
4
+ API_VERSION = 1
5
+ HOST = 'api.helium.com'
6
+ PROTOCOL = 'https'
7
7
 
8
- request = Typhoeus::Request.new(url, {
9
- params: options,
10
- headers: http_headers
11
- })
8
+ BASE_HTTP_HEADERS = {
9
+ 'Accept' => 'application/json',
10
+ 'Content-Type' => 'application/json',
11
+ 'User-Agent' => 'helium-ruby'
12
+ }
13
+
14
+ def get(path = nil, url: nil, params: {})
15
+ request = generate_request(path, url: url, method: :get, params: params)
16
+ run(request)
17
+ end
18
+
19
+ def paginated_get(path, klass:, params: {})
20
+ Cursor.new(client: self, path: path, klass: klass, params: params)
21
+ end
22
+
23
+ def post(path, body: {})
24
+ request = generate_request(path, method: :post, body: body)
25
+ run(request)
26
+ end
12
27
 
28
+ def patch(path, body: {})
29
+ request = generate_request(path, method: :patch, body: body)
13
30
  run(request)
14
31
  end
15
32
 
33
+ def delete(path)
34
+ request = generate_request(path, method: :delete)
35
+ response = run(request)
36
+ response.code == 204
37
+ end
38
+
16
39
  private
17
40
 
41
+ def http_headers
42
+ BASE_HTTP_HEADERS.merge({
43
+ 'Authorization' => api_key
44
+ })
45
+ end
46
+
47
+ def generate_request(path = nil, url: nil, method:, params: {}, body: {})
48
+ path = path.gsub(/^\//, '') if path
49
+ url ||= "#{PROTOCOL}://#{HOST}/v#{API_VERSION}/#{path}"
50
+
51
+ Typhoeus::Request.new(url, {
52
+ method: method,
53
+ params: params,
54
+ headers: http_headers,
55
+ body: JSON.generate(body)
56
+ })
57
+ end
58
+
18
59
  def run(request)
19
60
  request.run()
20
61
 
21
62
  if debug?
22
- # TODO print request method dynamically, won't always be GET
23
- puts "GET #{request.url} #{request.response.code} #{request.response.total_time}"
63
+ method = request.options[:method]
64
+ puts "#{method.upcase} #{request.url} #{request.response.code} #{request.response.total_time}"
24
65
  # puts request.response.body
25
66
  end
26
67
 
@@ -19,40 +19,63 @@ module Helium
19
19
  return Sensor.new(client: self, params: sensor_data)
20
20
  end
21
21
 
22
- def sensor_timeseries(sensor, size: 1000, port: nil, start_time: nil, end_time: nil, aggtype: nil, aggsize: nil)
23
- options = {
24
- "page[size]" => size,
25
- "filter[port]" => port,
26
- "filter[start]" => datetime_to_iso(start_time),
27
- "filter[end]" => datetime_to_iso(end_time),
28
- "agg[type]" => aggtype,
29
- "agg[size]" => aggsize
22
+ def sensor_timeseries(sensor, opts = {})
23
+ path = "/sensor/#{sensor.id}/timeseries"
24
+
25
+ params = {
26
+ "page[size]" => opts.fetch(:size, nil),
27
+ "filter[port]" => opts.fetch(:port, nil),
28
+ "filter[start]" => datetime_to_iso(opts.fetch(:start_time, nil)),
29
+ "filter[end]" => datetime_to_iso(opts.fetch(:end_time, nil)),
30
+ "agg[type]" => opts.fetch(:aggtype),
31
+ "agg[size]" => opts.fetch(:aggsize)
30
32
  }.delete_if { |key, value| value.to_s.empty? }
31
33
 
32
- response = get("/sensor/#{sensor.id}/timeseries", options: options)
33
- json_results = JSON.parse(response.body)
34
- timeseries_data = json_results["data"]
35
- timeseries_links = json_results["links"]
34
+ paginated_get(path, klass: Helium::DataPoint, params: params)
35
+ end
36
+
37
+ def new_sensor(name:)
38
+ path = "/sensor"
39
+
40
+ body = {
41
+ data: {
42
+ attributes: {
43
+ name: name
44
+ },
45
+ type: "sensor"
46
+ }
47
+ }
36
48
 
37
- return Timeseries.new(
38
- client: self,
39
- params: timeseries_data,
40
- links: timeseries_links
41
- )
49
+ response = post(path, body: body)
50
+ sensor_data = JSON.parse(response.body)["data"]
51
+
52
+ return Sensor.new(client: self, params: sensor_data)
42
53
  end
43
54
 
44
- def sensor_timeseries_by_link(url)
45
- response = get(url: url)
46
- json_results = JSON.parse(response.body)
47
- timeseries_data = json_results["data"]
48
- timeseries_links = json_results["links"]
49
-
50
- return Timeseries.new(
51
- client: self,
52
- params: timeseries_data,
53
- links: timeseries_links
54
- )
55
+ def update_sensor(sensor, name:)
56
+ path = "/sensor/#{sensor.id}"
57
+
58
+ body = {
59
+ data: {
60
+ attributes: {
61
+ name: name
62
+ },
63
+ id: sensor.id,
64
+ type: "sensor"
65
+ }
66
+ }
67
+
68
+ response = patch(path, body: body)
69
+ sensor_data = JSON.parse(response.body)["data"]
70
+
71
+ return Sensor.new(client: self, params: sensor_data)
55
72
  end
73
+
74
+ def delete_sensor(sensor)
75
+ path = "/sensor/#{sensor.id}"
76
+ delete(path)
77
+ end
78
+
56
79
  end
57
80
  end
58
81
  end
@@ -0,0 +1,54 @@
1
+ module Helium
2
+ class Cursor
3
+ include Enumerable
4
+
5
+ def initialize(client:, path:, klass:, params: {})
6
+ @client = client
7
+ @path = path
8
+ @klass = klass
9
+ @params = params
10
+
11
+ @collection = []
12
+ @next_link = nil
13
+ @is_last = false
14
+ end
15
+
16
+ def each(start = 0)
17
+ return to_enum(:each, start) unless block_given?
18
+
19
+ Array(@collection[start..-1]).each do |element|
20
+ yield(element)
21
+ end
22
+
23
+ unless last?
24
+ start = [@collection.size, start].max
25
+
26
+ fetch_next_page
27
+
28
+ each(start, &Proc.new)
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def fetch_next_page
35
+ if @next_link
36
+ response = @client.get(url: @next_link)
37
+ else
38
+ response = @client.get(@path, params: @params)
39
+ end
40
+
41
+ json_results = JSON.parse(response.body)
42
+ data = json_results["data"]
43
+ links = json_results["links"]
44
+
45
+ @next_link = links["prev"]
46
+ @is_last = @next_link.nil?
47
+ @collection += data.map{ |el| @klass.new(client: @client, params: el) }
48
+ end
49
+
50
+ def last?
51
+ @is_last
52
+ end
53
+ end
54
+ end
@@ -5,9 +5,9 @@ module Helium
5
5
  def initialize(client:, params:)
6
6
  @client = client
7
7
  @id = params["id"]
8
- @timestamp = params["attributes"]["timestamp"]
9
- @value = params["attributes"]["value"]
10
- @port = params["attributes"]["port"]
8
+ @timestamp = params.dig("attributes", "timestamp")
9
+ @value = params.dig("attributes", "value")
10
+ @port = params.dig("attributes", "port")
11
11
  end
12
12
 
13
13
  def timestamp
@@ -30,5 +30,14 @@ module Helium
30
30
  aggsize: aggsize
31
31
  )
32
32
  end
33
+
34
+ # TODO these kinds of methods should be generalized into a Resource object
35
+ def update(name:)
36
+ @client.update_sensor(self, name: name)
37
+ end
38
+
39
+ def destroy
40
+ @client.delete_sensor(self)
41
+ end
33
42
  end
34
43
  end
@@ -1,3 +1,3 @@
1
1
  module Helium
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.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.2.0
4
+ version: 0.3.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-16 00:00:00.000000000 Z
11
+ date: 2016-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: ruby_dig
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.0.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.0.2
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -192,6 +206,20 @@ dependencies:
192
206
  - - "~>"
193
207
  - !ruby/object:Gem::Version
194
208
  version: 0.2.0
209
+ - !ruby/object:Gem::Dependency
210
+ name: yard
211
+ requirement: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - "~>"
214
+ - !ruby/object:Gem::Version
215
+ version: 0.9.3
216
+ type: :development
217
+ prerelease: false
218
+ version_requirements: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - "~>"
221
+ - !ruby/object:Gem::Version
222
+ version: 0.9.3
195
223
  description: A Ruby gem for building applications with the Helium API
196
224
  email:
197
225
  - allenan@helium.com
@@ -199,10 +227,16 @@ executables: []
199
227
  extensions: []
200
228
  extra_rdoc_files: []
201
229
  files:
230
+ - ".codeclimate.yml"
231
+ - ".csslintrc"
232
+ - ".eslintignore"
233
+ - ".eslintrc"
202
234
  - ".gitignore"
203
235
  - ".rspec"
236
+ - ".rubocop.yml"
204
237
  - ".travis.yml"
205
238
  - CODE_OF_CONDUCT.md
239
+ - CONTRIBUTING.md
206
240
  - Gemfile
207
241
  - Guardfile
208
242
  - LICENSE.txt
@@ -210,6 +244,32 @@ files:
210
244
  - Rakefile
211
245
  - bin/console
212
246
  - bin/setup
247
+ - docs/Helium.html
248
+ - docs/Helium/Client.html
249
+ - docs/Helium/Client/Http.html
250
+ - docs/Helium/Client/Organizations.html
251
+ - docs/Helium/Client/Sensors.html
252
+ - docs/Helium/Client/Users.html
253
+ - docs/Helium/Cursor.html
254
+ - docs/Helium/DataPoint.html
255
+ - docs/Helium/Organization.html
256
+ - docs/Helium/Sensor.html
257
+ - docs/Helium/User.html
258
+ - docs/Helium/Utils.html
259
+ - docs/_index.html
260
+ - docs/class_list.html
261
+ - docs/css/common.css
262
+ - docs/css/full_list.css
263
+ - docs/css/style.css
264
+ - docs/file.README.html
265
+ - docs/file_list.html
266
+ - docs/frames.html
267
+ - docs/index.html
268
+ - docs/js/app.js
269
+ - docs/js/full_list.js
270
+ - docs/js/jquery.js
271
+ - docs/method_list.html
272
+ - docs/top-level-namespace.html
213
273
  - helium-ruby.gemspec
214
274
  - lib/helium.rb
215
275
  - lib/helium/client.rb
@@ -217,10 +277,10 @@ files:
217
277
  - lib/helium/client/organizations.rb
218
278
  - lib/helium/client/sensors.rb
219
279
  - lib/helium/client/users.rb
280
+ - lib/helium/cursor.rb
220
281
  - lib/helium/data_point.rb
221
282
  - lib/helium/organization.rb
222
283
  - lib/helium/sensor.rb
223
- - lib/helium/timeseries.rb
224
284
  - lib/helium/user.rb
225
285
  - lib/helium/utils.rb
226
286
  - lib/helium/version.rb