neurio 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/neurio.rb +32 -20
  3. metadata +30 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 488f5c1086893d8fea4198e70eb742941b49d565
4
- data.tar.gz: a3af973e89fa390c9ac8d39d5dcfd39885041f75
3
+ metadata.gz: 21fef552b4b70cb5489a9ce83408f748a1605b48
4
+ data.tar.gz: 3d7ab4772cf23e30c614b4ca8d0783f7c37860f8
5
5
  SHA512:
6
- metadata.gz: d5f6db2b12cd6883537ff826214b54503022bba68e8831b4fedbd309ef4c7573d09f290ab01256cf9088ca255f01965e3a1d8caae6ecedd18a2c25c7626b7d13
7
- data.tar.gz: 282eb981f7becbda6ee49010ef9db2e4c147b82a8f41981c489c160a7158368bda44e1b85ed10d10ce1c03dca5f2b610c747612625cc8107d405ec14e133e063
6
+ metadata.gz: 6f00738845047878ee0717997c29eee6f0dbc9c08bd208e2ac51cc89a4990b04cd62083ee9bf664cd46225b5924c5693540fd4eddb87bc7dead7844af3f773d6
7
+ data.tar.gz: 7e7482419de38a39cc9839a1e1d810d607567338c77644feced46aebe8b928dfd2a5f3333e6f962d7c60cccb5bdc427c90682f1083f0897a1e102250ced8b383
@@ -1,44 +1,45 @@
1
1
  require 'httparty'
2
+ require_relative 'neurio/reading'
2
3
 
3
4
  class Neurio
4
5
  include HTTParty
5
6
  base_uri "https://api.neur.io"
6
7
 
7
- class Reading
8
- attr_reader :consumption_power, :consumption_energy, :timestamp
9
- def initialize(values)
10
- @timestamp = DateTime.parse(values["timestamp"]).to_time
11
- @consumption_power = values["consumptionPower"]
12
- @consumption_energy = values["consumptionEnergy"]
13
- end
14
- end
15
-
16
-
8
+ # Construct a new Neurio API instance
9
+ #
10
+ # @param options [Hash] configuration options
11
+ # @option options client_id [String] the Neurio client id
12
+ # @option options client_secret [String] the Neurio client secret
17
13
  def initialize(options)
18
14
  @client_secret = options[:client_secret]
19
15
  @client_id = options[:client_id]
20
16
  raise ArgumentError.new("client_id and client_secret must be specified") unless @client_secret && @client_id
21
17
  end
22
18
 
19
+ # Get the most recent sensor reading
20
+ #
21
+ # @param sensor_id [String] The sensor id to retrieve data for
22
+ # @return [Neurio::Reading]
23
23
  def last(sensor_id = default_sensor_id)
24
24
  headers = {"content_type" => "application/json", "authorization" => "Bearer #{token}"}
25
25
  query = {"sensorId" => sensor_id}
26
26
  response = self.class.get("/v1/samples/live/last", :headers => headers, :query => query)
27
- Reading.new(response)
28
- end
29
-
30
- def user
31
- unless @user
32
- headers = {"content_type" => "application/json", "authorization" => "Bearer #{token}"}
33
- @user = self.class.get("/v1/users/current", :headers => headers)
34
- end
35
- @user
27
+ Reading.new(response, sensor_id)
36
28
  end
37
29
 
30
+
31
+ # Look up the ID of the first (and usually only) sensor associated with this account
32
+ #
33
+ # @return [String] The Neurio sensorId
38
34
  def default_sensor_id
39
35
  user["locations"][0]["sensors"][0]["sensorId"]
40
36
  end
41
37
 
38
+ # Get recent sensor readings since last_timestamp.
39
+ #
40
+ # @param last_timestamp [DateTime] Timestamp of last sample already received. API defaults to 2 minutes ago if omitted.
41
+ # @param sensor_id [String] The sensor id to retrieve data for
42
+ # @return [Array<Neurio::Reading>] Array of sensor readings
42
43
  def live(last_timestamp = nil, sensor_id = default_sensor_id)
43
44
  headers = {"content_type" => "application/json", "authorization" => "Bearer #{token}"}
44
45
  query = {"sensorId" => sensor_id}
@@ -47,10 +48,11 @@ class Neurio
47
48
  end
48
49
 
49
50
  response = self.class.get("/v1/samples/live", :headers => headers, :query => query)
50
- response.map{|x| Reading.new(x)}
51
+ response.map{|x| Reading.new(x, sensor_id)}
51
52
  end
52
53
 
53
54
  private
55
+ # Obtain an API token for this client
54
56
  def token
55
57
  unless @token
56
58
  options = {"grant_type" => "client_credentials", "client_id" => @client_id, "client_secret" => @client_secret}
@@ -60,5 +62,15 @@ class Neurio
60
62
  end
61
63
  @token
62
64
  end
65
+
66
+ # Obtain a user object associated with the token
67
+ def user
68
+ unless @user
69
+ headers = {"content_type" => "application/json", "authorization" => "Bearer #{token}"}
70
+ @user = self.class.get("/v1/users/current", :headers => headers)
71
+ end
72
+ @user
73
+ end
74
+
63
75
 
64
76
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neurio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Francis
@@ -17,9 +17,6 @@ dependencies:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0.13'
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 0.13.5
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
@@ -27,9 +24,34 @@ dependencies:
27
24
  - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0.13'
30
- - - ">="
27
+ - !ruby/object:Gem::Dependency
28
+ name: yard
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: redcarpet
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
31
53
  - !ruby/object:Gem::Version
32
- version: 0.13.5
54
+ version: '3.3'
33
55
  description: API functionality for Neurio energy monitoring devices. See http://neur.io/
34
56
  email: joe@lostapathy.com
35
57
  executables: []
@@ -57,8 +79,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
79
  version: '0'
58
80
  requirements: []
59
81
  rubyforge_project:
60
- rubygems_version: 2.4.5
82
+ rubygems_version: 2.4.8
61
83
  signing_key:
62
84
  specification_version: 4
63
85
  summary: Neurio API
64
86
  test_files: []
87
+ has_rdoc: