sensorpush 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: de40da08a9b434e84bab3e6efcacf2a8fee90b433b24bca87256fd95573b0071
4
+ data.tar.gz: 84658a25be2fc440e1d1250b0a3a022d8a58e43e6767c05ccf83afa916226c02
5
+ SHA512:
6
+ metadata.gz: 5aa3cdbc5340a70167b2255ef32e1ca14b0dd6cea00ee02a0c1dbc0ca007c769e7c46b47e97222a2339757c43550e20351a72946a6d5d7324da4579eb278528c
7
+ data.tar.gz: 253c8cb5666475a315f16682ca500f61d5ae3b2278cc2fd873ffa07da2f38fe236163d9f0573a6bd9b01fbcf156209b9b43ebedfa75dfdedd754f08923ef52e9
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # IDE related
14
+ .idea/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,27 @@
1
+ Style/StringLiterals:
2
+ Enabled: true
3
+ EnforcedStyle: double_quotes
4
+
5
+ Style/StringLiteralsInInterpolation:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/Documentation:
10
+ Enabled: false
11
+
12
+ Metrics/MethodLength:
13
+ Enabled: true
14
+ Max: 30
15
+
16
+ Metrics/BlockLength:
17
+ Exclude:
18
+ - spec/**/*_spec.rb
19
+
20
+ Layout/LineLength:
21
+ Max: 120
22
+
23
+ Layout/DotPosition:
24
+ Enabled: false
25
+
26
+ Layout/MultilineMethodCallBraceLayout:
27
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in sensorpush.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 0.80"
data/Gemfile.lock ADDED
@@ -0,0 +1,68 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ sensorpush (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.8.4)
10
+ public_suffix (>= 2.0.2, < 6.0)
11
+ ast (2.4.2)
12
+ crack (0.4.5)
13
+ rexml
14
+ diff-lcs (1.5.0)
15
+ hashdiff (1.0.1)
16
+ parallel (1.23.0)
17
+ parser (3.2.2.3)
18
+ ast (~> 2.4.1)
19
+ racc
20
+ public_suffix (5.0.3)
21
+ racc (1.7.1)
22
+ rainbow (3.1.1)
23
+ rake (13.0.6)
24
+ regexp_parser (2.8.1)
25
+ rexml (3.2.6)
26
+ rspec (3.12.0)
27
+ rspec-core (~> 3.12.0)
28
+ rspec-expectations (~> 3.12.0)
29
+ rspec-mocks (~> 3.12.0)
30
+ rspec-core (3.12.2)
31
+ rspec-support (~> 3.12.0)
32
+ rspec-expectations (3.12.3)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.12.0)
35
+ rspec-mocks (3.12.6)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.12.0)
38
+ rspec-support (3.12.1)
39
+ rubocop (0.93.1)
40
+ parallel (~> 1.10)
41
+ parser (>= 2.7.1.5)
42
+ rainbow (>= 2.2.2, < 4.0)
43
+ regexp_parser (>= 1.8)
44
+ rexml
45
+ rubocop-ast (>= 0.6.0)
46
+ ruby-progressbar (~> 1.7)
47
+ unicode-display_width (>= 1.4.0, < 2.0)
48
+ rubocop-ast (1.29.0)
49
+ parser (>= 3.2.1.0)
50
+ ruby-progressbar (1.13.0)
51
+ unicode-display_width (1.8.0)
52
+ webmock (3.18.1)
53
+ addressable (>= 2.8.0)
54
+ crack (>= 0.3.2)
55
+ hashdiff (>= 0.4.0, < 2.0.0)
56
+
57
+ PLATFORMS
58
+ x86_64-darwin-22
59
+
60
+ DEPENDENCIES
61
+ rake (~> 13.0)
62
+ rspec (~> 3.0)
63
+ rubocop (~> 0.80)
64
+ sensorpush!
65
+ webmock (~> 3.18)
66
+
67
+ BUNDLED WITH
68
+ 2.2.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Garth Dubin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # Sensorpush
2
+
3
+ This gem will allow you to interface with the [SensorPush Cloud Gateway API](https://www.sensorpush.com/gateway-cloud-api).
4
+
5
+ SensorPush is a small temperature/humidity sensor. When paired with the SensorPush Gateway, it enables the gathering of
6
+ environmental metrics which are stored and accessed via the SensorPush Cloud Gateway API.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'sensorpush'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle install
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install sensorpush
23
+
24
+ ## Usage
25
+
26
+ After purchasing a SensorPush Gateway and setting it up through the iOS/Android application, you will have a username
27
+ and password to access the API. These credentials are the same as what you would use to access the [SensorPush Dashboard](https://dashboard.sensorpush.com/).
28
+
29
+ To begin using this gem to retrieve data, you must first authenticate with the service:
30
+
31
+ ```ruby
32
+ require 'sensorpush'
33
+
34
+ client = Sensorpush.new({ username: 'yourusername@example.com', password: 'yourpassword'} )
35
+ success = client.authenticate
36
+ ```
37
+
38
+ The authenticate method will return true if you've successfully connected to the SensorPush API.
39
+
40
+ Once you've authenticated you can retrieve a list of sensors by running:
41
+
42
+ ```ruby
43
+ sensors = client.sensors
44
+ ```
45
+
46
+ This method will return an array of Sensorpush::Sensor objects. Once you've determined which sensor
47
+ you would like to download metrics/samples from, you can query those by using the id of the sensor
48
+ and calling:
49
+
50
+ ```ruby
51
+ sensor = sensors.first
52
+ samples = client.samples(sensor.id)
53
+ ```
54
+
55
+ This returns an array of Sensorpush::Sample objects. The samples method has several options that can be
56
+ passed to it to refine which set of samples you would like.
57
+
58
+ ## Contributing
59
+
60
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gdubin/sensorpush.
61
+
62
+ ## License
63
+
64
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "sensorpush"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+ require "net/http"
5
+ require "json"
6
+ require "pp"
7
+
8
+ module Sensorpush
9
+ class Client
10
+ BASE_URL = "https://api.sensorpush.com/api/v1"
11
+ BASE_HEADERS = { 'accept': "application/json", 'Content-Type': "application/json" }.freeze
12
+
13
+ attr_accessor :username
14
+ attr_accessor :password
15
+ attr_accessor :accesstoken
16
+
17
+ def initialize(options = {})
18
+ self.username = options[:username]
19
+ self.password = options[:password]
20
+ self.accesstoken = options[:accesstoken]
21
+ end
22
+
23
+ def authenticate
24
+ authorization = authorize
25
+ self.accesstoken = get_token(authorization) if authorization
26
+
27
+ !accesstoken.nil?
28
+ end
29
+
30
+ def sensors
31
+ uri = URI(BASE_URL + "/devices/sensors")
32
+ body = {}
33
+
34
+ response = Net::HTTP.post(uri, body.to_json, headers)
35
+ json_object = JSON.parse(response.body)
36
+
37
+ json_object.collect do |_key, hash|
38
+ Sensorpush::Sensor.new(hash)
39
+ end
40
+ end
41
+
42
+ def samples(id, options = {})
43
+ uri = URI(BASE_URL + "/samples")
44
+ body = { sensors: [id] }
45
+ body.merge!({ limit: options[:limit] }) unless options[:limit].nil?
46
+ body.merge!({ startTime: options[:start_time].to_s }) unless options[:start_time].nil?
47
+ body.merge!({ endTime: options[:end_time].to_s }) unless options[:end_time].nil?
48
+
49
+ response = Net::HTTP.post(uri, body.to_json, headers)
50
+ json_object = JSON.parse(response.body)
51
+
52
+ samples_json_array = json_object.dig("sensors", id)
53
+ samples = samples_json_array&.collect do |hash|
54
+ Sensorpush::Sample.new(hash)
55
+ end
56
+ samples || []
57
+ end
58
+
59
+ private
60
+
61
+ def headers
62
+ headers = BASE_HEADERS.dup
63
+ headers.merge!({ 'Authorization': accesstoken }) if accesstoken
64
+ headers
65
+ end
66
+
67
+ def authorize
68
+ uri = URI(BASE_URL + "/oauth/authorize")
69
+ body = { email: username, password: password }
70
+
71
+ response = Net::HTTP.post(uri, body.to_json, headers)
72
+ json_object = JSON.parse(response.body)
73
+ json_object["authorization"]
74
+ end
75
+
76
+ def get_token(authorization)
77
+ uri = URI(BASE_URL + "/oauth/accesstoken")
78
+ body = { authorization: authorization }
79
+
80
+ response = Net::HTTP.post(uri, body.to_json, headers)
81
+ json_object = JSON.parse(response.body)
82
+
83
+ json_object["accesstoken"]
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+
5
+ module Sensorpush
6
+ class Sample
7
+ attr_accessor :humidity
8
+ attr_accessor :temperature
9
+ attr_accessor :observed
10
+
11
+ def initialize(attributes = {})
12
+ self.humidity = attributes["humidity"]
13
+ self.temperature = attributes["temperature"]
14
+ self.observed = DateTime.parse(attributes["observed"])
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sensorpush
4
+ class Sensor
5
+ attr_accessor :active
6
+ attr_accessor :name
7
+ attr_accessor :address
8
+ attr_accessor :battery_voltage
9
+ attr_accessor :id
10
+ attr_accessor :device_id
11
+
12
+ def initialize(attributes = {})
13
+ self.active = attributes["active"]
14
+ self.name = attributes["name"]
15
+ self.address = attributes["address"]
16
+ self.battery_voltage = attributes["battery_voltage"]
17
+ self.id = attributes["id"]
18
+ self.device_id = attributes["deviceId"]
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sensorpush
4
+ VERSION = "0.1.0"
5
+ end
data/lib/sensorpush.rb ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "sensorpush/version"
4
+ require_relative "sensorpush/client"
5
+ require_relative "sensorpush/sensor"
6
+ require_relative "sensorpush/sample"
7
+
8
+ module Sensorpush
9
+ class Error < StandardError; end
10
+
11
+ class << self
12
+ def new(options = {})
13
+ Client.new(options)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/sensorpush/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "sensorpush"
7
+ spec.version = Sensorpush::VERSION
8
+ spec.authors = ["Garth Dubin"]
9
+ spec.email = ["garth@dubin.dev"]
10
+
11
+ spec.summary = "SensorPush API Client Library"
12
+ spec.description = "Implements the SensorPush API to retrieve sensor and sample data."
13
+ spec.homepage = "https://github.com/gdubin/sensorpush"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/gdubin/sensorpush"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ # Uncomment to register a new dependency of your gem
30
+ # spec.add_dependency "example-gem", "~> 1.0"
31
+ spec.add_development_dependency "webmock", "~> 3.18"
32
+
33
+ # For more information and examples about making a new gem, checkout our
34
+ # guide at: https://bundler.io/guides/creating_gem.html
35
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sensorpush
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Garth Dubin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-09-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: webmock
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.18'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.18'
27
+ description: Implements the SensorPush API to retrieve sensor and sample data.
28
+ email:
29
+ - garth@dubin.dev
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - ".rubocop.yml"
37
+ - Gemfile
38
+ - Gemfile.lock
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - bin/console
43
+ - bin/setup
44
+ - lib/sensorpush.rb
45
+ - lib/sensorpush/client.rb
46
+ - lib/sensorpush/sample.rb
47
+ - lib/sensorpush/sensor.rb
48
+ - lib/sensorpush/version.rb
49
+ - sensorpush.gemspec
50
+ homepage: https://github.com/gdubin/sensorpush
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ homepage_uri: https://github.com/gdubin/sensorpush
55
+ source_code_uri: https://github.com/gdubin/sensorpush
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 2.4.0
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.2.3
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: SensorPush API Client Library
75
+ test_files: []