nuki_api 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0e68542328e3d444b8c7c6fd573e9c99a404a62a441b858f130585d2a55caefd
4
+ data.tar.gz: b2905d447f5d2aa1a1e5d50d8d8d3c4987c6efabcd3bf7eccba4930c6746306c
5
+ SHA512:
6
+ metadata.gz: b1a90652659da6aae8220f1ccedd9b91ec4056706a5cdcaedf6c8e211b5460c5db5ea7da286bda125b9cb56eff311ecd4330d2d252fd43ff28dc61b6bf2b284c
7
+ data.tar.gz: 3c71c38cdc55d73220e92fad8e1507caec5834b7837e0eb38d4a711757c3fa8470402d3ae17952e50e55ab203b460cdd3296795a100ffc3ec11b04865eb7854f
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.0
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.3
17
+ bundle install
18
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,20 @@
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
+ # project files
14
+ /.idea/
15
+ nuki_api.iml
16
+
17
+ # custom ignores
18
+ .env
19
+ *.gem
20
+ /.irb_history
data/.irbrc ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'irb'
4
+ require 'irb/completion'
5
+ require 'rubygems'
6
+ require 'pp'
7
+ IRB.conf[:SAVE_HISTORY] = 300
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ Style/StringLiterals:
2
+ Enabled: true
3
+ EnforcedStyle: single_quotes
4
+
5
+ Style/StringLiteralsInInterpolation:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Layout/LineLength:
10
+ Max: 120
11
+
12
+ AllCops:
13
+ TargetRubyVersion: 2.4.0
14
+ NewCops: enable
data/CHANGELOG.md ADDED
@@ -0,0 +1 @@
1
+ * 0.0.1 First available methods from API: able to get locks and create keypad authentications at least
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at TODO: Write your email address. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Dockerfile ADDED
@@ -0,0 +1,12 @@
1
+ FROM ruby:3.0
2
+
3
+ WORKDIR /usr/src/app
4
+
5
+ COPY Gemfile .
6
+ COPY Gemfile.lock .
7
+ COPY nuki_api.gemspec .
8
+ COPY lib/nuki_api/version.rb lib/nuki_api/version.rb
9
+
10
+ RUN bundle install
11
+
12
+ COPY . .
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in nuki_api.gemspec
6
+ gemspec
7
+
8
+ group :development do
9
+ gem 'dotenv', '~> 2.5'
10
+ end
11
+
12
+ group :test do
13
+ gem 'rake', '~> 13.0'
14
+ gem 'rspec', '~> 3.0'
15
+ gem 'rubocop', '~> 0.80'
16
+ gem 'vcr', '~> 6.0'
17
+ gem 'webmock', '~> 3.12'
18
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,99 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ nuki_api (0.0.1)
5
+ api_cache (~> 0.3.0)
6
+ dry-configurable (~> 0.13)
7
+ faraday (~> 2.6.0)
8
+ oj (~> 3.11)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ addressable (2.8.7)
14
+ public_suffix (>= 2.0.2, < 7.0)
15
+ api_cache (0.3.0)
16
+ ast (2.4.2)
17
+ base64 (0.2.0)
18
+ bigdecimal (3.1.9)
19
+ concurrent-ruby (1.3.5)
20
+ crack (1.0.0)
21
+ bigdecimal
22
+ rexml
23
+ diff-lcs (1.5.1)
24
+ dotenv (2.8.1)
25
+ dry-configurable (0.16.1)
26
+ dry-core (~> 0.6)
27
+ zeitwerk (~> 2.6)
28
+ dry-core (0.9.1)
29
+ concurrent-ruby (~> 1.0)
30
+ zeitwerk (~> 2.6)
31
+ faraday (2.6.0)
32
+ faraday-net_http (>= 2.0, < 3.1)
33
+ ruby2_keywords (>= 0.0.4)
34
+ faraday-net_http (3.0.2)
35
+ hashdiff (1.1.2)
36
+ oj (3.16.9)
37
+ bigdecimal (>= 3.0)
38
+ ostruct (>= 0.2)
39
+ ostruct (0.6.1)
40
+ parallel (1.26.3)
41
+ parser (3.3.6.0)
42
+ ast (~> 2.4.1)
43
+ racc
44
+ public_suffix (6.0.1)
45
+ racc (1.8.1)
46
+ rainbow (3.1.1)
47
+ rake (13.2.1)
48
+ regexp_parser (2.10.0)
49
+ rexml (3.4.0)
50
+ rspec (3.13.0)
51
+ rspec-core (~> 3.13.0)
52
+ rspec-expectations (~> 3.13.0)
53
+ rspec-mocks (~> 3.13.0)
54
+ rspec-core (3.13.2)
55
+ rspec-support (~> 3.13.0)
56
+ rspec-expectations (3.13.3)
57
+ diff-lcs (>= 1.2.0, < 2.0)
58
+ rspec-support (~> 3.13.0)
59
+ rspec-mocks (3.13.2)
60
+ diff-lcs (>= 1.2.0, < 2.0)
61
+ rspec-support (~> 3.13.0)
62
+ rspec-support (3.13.2)
63
+ rubocop (0.93.1)
64
+ parallel (~> 1.10)
65
+ parser (>= 2.7.1.5)
66
+ rainbow (>= 2.2.2, < 4.0)
67
+ regexp_parser (>= 1.8)
68
+ rexml
69
+ rubocop-ast (>= 0.6.0)
70
+ ruby-progressbar (~> 1.7)
71
+ unicode-display_width (>= 1.4.0, < 2.0)
72
+ rubocop-ast (1.37.0)
73
+ parser (>= 3.3.1.0)
74
+ ruby-progressbar (1.13.0)
75
+ ruby2_keywords (0.0.5)
76
+ unicode-display_width (1.8.0)
77
+ vcr (6.3.1)
78
+ base64
79
+ webmock (3.24.0)
80
+ addressable (>= 2.8.0)
81
+ crack (>= 0.3.2)
82
+ hashdiff (>= 0.4.0, < 2.0.0)
83
+ zeitwerk (2.6.18)
84
+
85
+ PLATFORMS
86
+ aarch64-linux
87
+ x86_64-linux
88
+
89
+ DEPENDENCIES
90
+ dotenv (~> 2.5)
91
+ nuki_api!
92
+ rake (~> 13.0)
93
+ rspec (~> 3.0)
94
+ rubocop (~> 0.80)
95
+ vcr (~> 6.0)
96
+ webmock (~> 3.12)
97
+
98
+ BUNDLED WITH
99
+ 2.2.33
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 TODO: Write your name
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,48 @@
1
+ # NukiApi
2
+
3
+ ![main workflow](https://github.com/dlage/nuki_api_gem/actions/workflows/main.yml/badge.svg)
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/nuki_api`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ TODO: Delete this and the text above, and describe your gem
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'nuki_api'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install nuki_api
24
+
25
+ ## Usage
26
+
27
+ TODO: Write usage instructions here
28
+ ```ruby
29
+ client = NukiApi::Client.new
30
+ ```
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/nuki_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/nuki_api/blob/master/CODE_OF_CONDUCT.md).
41
+
42
+ ## License
43
+
44
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
45
+
46
+ ## Code of Conduct
47
+
48
+ Everyone interacting in the NukiApi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/nuki_api/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.pattern = FileList['spec/**/*_spec.rb']
8
+ end
9
+
10
+ require 'rubocop/rake_task'
11
+
12
+ RuboCop::RakeTask.new
13
+
14
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'nuki_api'
6
+ require 'dotenv/load'
7
+
8
+ # You can add fixtures and/or initialization code here to make experimenting
9
+ # with your gem easier. You can also use a different console, if you like.
10
+
11
+ # (If you use this, don't forget to add pry to your Gemfile!)
12
+ # require "pry"
13
+ # Pry.start
14
+
15
+ require 'irb'
16
+ 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,6 @@
1
+ services:
2
+ app:
3
+ build: .
4
+ command: bash -c "rake"
5
+ volumes:
6
+ - .:/usr/src/app
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'api_exceptions'
4
+ require_relative 'constants'
5
+ require_relative 'http_status_codes'
6
+ require 'dry-configurable'
7
+ require 'api_cache'
8
+ require 'digest/bubblebabble'
9
+
10
+ module NukiApi
11
+ # Core class responsible for api interface operations
12
+ class API
13
+ include ApiExceptions
14
+ include Constants
15
+ include HttpStatusCodes
16
+ include Dry::Configurable
17
+
18
+ HTTP_STATUS_MAPPING = {
19
+ HTTP_BAD_REQUEST_CODE => BadRequestError,
20
+ HTTP_UNAUTHORIZED_CODE => UnauthorizedError,
21
+ HTTP_FORBIDDEN_CODE => ForbiddenError,
22
+ HTTP_NOT_FOUND_CODE => NotFoundError,
23
+ HTTP_UNPROCESSABLE_ENTITY_CODE => UnprocessableEntityError,
24
+ 'default' => ApiError
25
+ }.freeze
26
+ API_ENDPOINT = 'https://api.nuki.io'
27
+ API_TIMEOUT = 5 # API response timeout
28
+ API_STALE_VALIDITY = :forever # 604800 = 7 days - Maximum time to use old data
29
+ API_TOKEN = 'TESTING'
30
+
31
+ setting :follow_redirects, default: true
32
+
33
+ # The api endpoint used to connect to NukiApi if none is set
34
+ setting :endpoint, default: ENV['NUKI_API_ENDPOINT'] || API_ENDPOINT, reader: true
35
+
36
+ # The timeout in requests made to fsapp
37
+ setting :timeout, default: ENV['NUKI_API_TIMEOUT'] || API_TIMEOUT, reader: true
38
+
39
+ # The stale validity in requests that fail to fsapp
40
+ setting :stale_validity, default: ENV['NUKI_API_STALE_VALIDITY'] || API_STALE_VALIDITY, reader: true
41
+
42
+ # The token included in request header 'x-api-key'
43
+ setting :token, default: ENV['NUKI_API_TOKEN'] || API_TOKEN, reader: true
44
+
45
+ # The value sent in the http header for 'User-Agent' if none is set
46
+ setting :user_agent, default: "NukiApi API Ruby Gem #{NukiApi::VERSION}"
47
+
48
+ # By default uses the Faraday connection options if none is set
49
+ setting :connection_options, default: {}
50
+
51
+ # By default display 30 resources
52
+ setting :per_page, default: 10
53
+
54
+ # Add Faraday::RackBuilder to overwrite middleware
55
+ setting :stack
56
+
57
+ # Create new API
58
+ #
59
+ # @api public
60
+ def initialize(options = {}, &block)
61
+ configure do |c|
62
+ options.each_key do |key|
63
+ c.send("#{key}=", options[key])
64
+ end
65
+ end
66
+
67
+ yield_or_eval(&block) if block_given?
68
+ end
69
+
70
+ # Call block with argument
71
+ #
72
+ # @api private
73
+ def yield_or_eval(&block)
74
+ return unless block
75
+
76
+ block.arity.positive? ? yield(self) : instance_eval(&block)
77
+ end
78
+
79
+ private
80
+
81
+ def client
82
+ @client ||= Faraday.new(config.endpoint) do |client|
83
+ client.request :url_encoded
84
+ client.adapter Faraday.default_adapter
85
+ client.options.timeout = config.timeout
86
+ init_client_headers(client)
87
+ client.response :raise_error # raise Faraday::Error on status code 4xx or 5xx
88
+ client.response :logger, ::Logger.new($stdout), body: true, bodies: { request: true, response: true }
89
+ end
90
+ end
91
+
92
+ def init_client_headers(client)
93
+ client.headers['Content-Type'] = 'application/json'
94
+ client.headers['Authorization'] = "Bearer #{config.token}"
95
+ client.headers['User-Agent'] = config.user_agent
96
+ end
97
+
98
+ def request(http_method:, endpoint:, params: {}, query_params: {}, cache_ttl: 3600)
99
+ response = APICache.get(
100
+ Digest::SHA256.bubblebabble(config.token) + http_method.to_s + endpoint + params.to_s,
101
+ cache: cache_ttl,
102
+ valid: config.stale_validity,
103
+ timeout: config.timeout
104
+ ) do
105
+ request_send(client, http_method, endpoint, query_params: query_params, params: params)
106
+ end
107
+
108
+ process_http_response(response)
109
+ end
110
+
111
+ def request_send(client, http_method, endpoint, query_params: {}, params: {})
112
+ client.public_send(http_method, endpoint) do |req|
113
+ req.headers['Content-Type'] = 'application/json'
114
+ req.params = query_params
115
+ req.body = params.to_json if params.size
116
+ end
117
+ end
118
+
119
+ def process_http_response(response)
120
+ return Oj.load(response.body) if response_successful?(response)
121
+
122
+ raise error_class(response), "Code: #{response.status}, response: #{response.body}"
123
+ end
124
+
125
+ def error_class(response)
126
+ if HTTP_STATUS_MAPPING.include?(response.status)
127
+ HTTP_STATUS_MAPPING[response.status]
128
+ else
129
+ HTTP_STATUS_MAPPING['default']
130
+ end
131
+ end
132
+
133
+ def response_successful?(response)
134
+ response.status == HTTP_OK_CODE || response.status == HTTP_NO_CONTENT
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NukiApi
4
+ module ApiExceptions
5
+ APIExceptionError = Class.new(StandardError)
6
+ BadRequestError = Class.new(APIExceptionError)
7
+ UnauthorizedError = Class.new(APIExceptionError)
8
+ ForbiddenError = Class.new(APIExceptionError)
9
+ ApiRequestsQuotaReachedError = Class.new(APIExceptionError)
10
+ NotFoundError = Class.new(APIExceptionError)
11
+ UnprocessableEntityError = Class.new(APIExceptionError)
12
+ ApiError = Class.new(APIExceptionError)
13
+ end
14
+ end
@@ -0,0 +1,145 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'logger'
5
+ require_relative 'api'
6
+
7
+ module NukiApi
8
+ # Main client class that implements communication with the API
9
+ # global_params:
10
+ # - include_related_objects: int 0-1 0
11
+ # - page: int positive 1
12
+ # - per_page: int positive 20
13
+ class Client < API
14
+ def account
15
+ response = request(
16
+ http_method: :get,
17
+ endpoint: 'account'
18
+ )
19
+ process_response(response)
20
+ end
21
+
22
+ def smartlocks
23
+ response = request(
24
+ http_method: :get,
25
+ endpoint: 'smartlock'
26
+ )
27
+ process_response(response)
28
+ end
29
+
30
+ def smartlocks_log
31
+ response = request(
32
+ http_method: :get,
33
+ endpoint: 'smartlock/log'
34
+ )
35
+ process_response(response)
36
+ end
37
+
38
+ def smartlocks_auth
39
+ response = request(
40
+ http_method: :get,
41
+ endpoint: 'smartlock/auth'
42
+ )
43
+ process_response(response)
44
+ end
45
+
46
+ # SmartlockAuthCreate{
47
+ # name* string
48
+ # The name of the authorization (max 32 chars)
49
+ #
50
+ # allowedFromDate string($date-time)
51
+ # The allowed from date
52
+ #
53
+ # allowedUntilDate string($date-time)
54
+ # The allowed until date
55
+ #
56
+ # allowedWeekDays integer($int32)
57
+ # minimum: 0
58
+ # maximum: 127
59
+ # The allowed weekdays bitmask: 64 .. monday, 32 .. tuesday, 16 .. wednesday, 8 .. thursday, 4 .. friday, 2 .. saturday, 1 .. sunday
60
+ #
61
+ # allowedFromTime integer($int32)
62
+ # The allowed from time (in minutes from midnight)
63
+ #
64
+ # allowedUntilTime integer($int32)
65
+ # The allowed until time (in minutes from midnight)
66
+ #
67
+ # accountUserId integer($int32)
68
+ # The id of the linked account user (required if type is NOT 13 .. keypad)
69
+ #
70
+ # remoteAllowed* boolean
71
+ # True if the auth has remote access
72
+ #
73
+ # smartActionsEnabled boolean
74
+ # The smart actions enabled flag
75
+ #
76
+ # type integer($int32)
77
+ # The optional type of the auth 0 .. app (default), 2 .. fob, 13 .. keypad
78
+ #
79
+ # code integer($int32)
80
+ # The code of the keypad authorization (only for keypad)
81
+ #
82
+ # }
83
+ def smartlock_auth_create(params = {})
84
+ response = request(
85
+ http_method: :put,
86
+ endpoint: 'smartlock/auth',
87
+ params: params,
88
+ cache_ttl: 5 # Prevent "double click" for 5 seconds
89
+ )
90
+ process_response(response)
91
+ end
92
+
93
+ def smartlock(smartlock_id, global_params = {})
94
+ response = request(
95
+ http_method: :get,
96
+ endpoint: "smartlock/#{smartlock_id}",
97
+ params: global_params
98
+ )
99
+ process_response(response)
100
+ end
101
+
102
+ def smartlock_log(smartlock_id, global_params = {})
103
+ response = request(
104
+ http_method: :get,
105
+ endpoint: "smartlock/#{smartlock_id}/log",
106
+ params: global_params
107
+ )
108
+ process_response(response)
109
+ end
110
+
111
+ def smartlock_auth(smartlock_id, global_params = {})
112
+ response = request(
113
+ http_method: :get,
114
+ endpoint: "smartlock/#{smartlock_id}/auth",
115
+ params: global_params
116
+ )
117
+ process_response(response)
118
+ end
119
+
120
+ # NukiApi::Client.new.search(query)
121
+ def search(query, type = nil, global_params = {})
122
+ response = request(
123
+ http_method: :get,
124
+ endpoint: 'search',
125
+ params: { q: query, type: type }.merge!(global_params),
126
+ cache_ttl: 3600 * 24
127
+ )
128
+ process_response(response)
129
+ end
130
+
131
+ protected
132
+
133
+ def process_response(response)
134
+ result = response
135
+ case result
136
+ when Hash
137
+ result.transform_keys!(&:to_sym)
138
+ result.each_value { |r| process_response(r) }
139
+ when Array
140
+ result.each { |r| process_response(r) }
141
+ end
142
+ result
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NukiApi
4
+ # Constants
5
+ module Constants
6
+ # Response headers
7
+ RATELIMIT_REMAINING = 'X-RateLimit-Remaining'
8
+
9
+ RATELIMIT_LIMIT = 'X-RateLimit-Limit'
10
+
11
+ RATELIMIT_RESET = 'X-RateLimit-Reset'
12
+
13
+ CONTENT_TYPE = 'Content-Type'
14
+
15
+ CONTENT_LENGTH = 'content-length'
16
+
17
+ CACHE_CONTROL = 'cache-control'
18
+
19
+ ETAG = 'ETag'
20
+
21
+ SERVER = 'Server'
22
+
23
+ DATE = 'Date'
24
+
25
+ LOCATION = 'Location'
26
+
27
+ USER_AGENT = 'User-Agent'
28
+
29
+ ACCEPT = 'Accept'
30
+
31
+ ACCEPT_CHARSET = 'Accept-Charset'
32
+
33
+ OAUTH_SCOPES = 'X-OAuth-Scopes'
34
+
35
+ ACCEPTED_OAUTH_SCOPES = 'X-Accepted-Oauth-Scopes'
36
+
37
+ # Link headers
38
+ HEADER_LINK = 'Link'
39
+
40
+ HEADER_NEXT = 'X-Next'
41
+
42
+ HEADER_LAST = 'X-Last'
43
+
44
+ META_REL = 'rel'
45
+
46
+ META_LAST = 'last'
47
+
48
+ META_NEXT = 'next'
49
+
50
+ META_FIRST = 'first'
51
+
52
+ META_PREV = 'prev'
53
+
54
+ PARAM_PAGE = 'page'
55
+
56
+ PARAM_PER_PAGE = 'per_page'
57
+
58
+ PARAM_START_PAGE = 'start_page'
59
+
60
+ PARAM_INCLUDE_RELATED = 'include_related_objects'
61
+ end
62
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NukiApi
4
+ module HttpStatusCodes
5
+ HTTP_OK_CODE = 200
6
+ HTTP_NO_CONTENT = 204 # OK
7
+
8
+ HTTP_BAD_REQUEST_CODE = 400 # Bad parameter
9
+ HTTP_UNAUTHORIZED_CODE = 401 # Not authorized
10
+ HTTP_NOT_PAID_CODE = 402 # Account not payed
11
+ HTTP_FORBIDDEN_CODE = 403
12
+ HTTP_NOT_FOUND_CODE = 404
13
+ HTTP_PARAMETER_CONFLICT_CODE = 409 # Parameter conflicts
14
+ HTTP_LOCKED = 423 # Locked
15
+ HTTP_ACCOUNT_UPGRADE_REQUIRED = 426 # Account upgrade required
16
+ HTTP_UNPROCESSABLE_ENTITY_CODE = 429
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NukiApi
4
+ VERSION = '0.0.1'
5
+ end
data/lib/nuki_api.rb ADDED
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'oj'
5
+ require_relative 'nuki_api/version'
6
+
7
+ # Base module for Nuki API
8
+ module NukiApi
9
+ class Error < StandardError; end
10
+
11
+ LIBNAME = 'nuki_api'
12
+
13
+ LIBDIR = File.expand_path(LIBNAME.to_s, __dir__)
14
+
15
+ class << self
16
+ # Alias for NukiApi::Client.new
17
+ #
18
+ # @param [Hash] options
19
+ # the configuration options
20
+ #
21
+ # @return [NukiApi::Client]
22
+ #
23
+ # @api public
24
+ def new(options = {}, &block)
25
+ Client.new(options, &block)
26
+ end
27
+
28
+ # Default middleware stack that uses default adapter as specified
29
+ # by configuration setup
30
+ #
31
+ # @return [Proc]
32
+ #
33
+ # @api private
34
+ def default_middleware(options = {})
35
+ Middleware.default(options)
36
+ end
37
+
38
+ # Delegate to NukiApi::Client
39
+ #
40
+ # @api private
41
+ def method_missing(method_name, *args, &block)
42
+ if new.respond_to?(method_name)
43
+ new.send(method_name, *args, &block)
44
+ else
45
+ super.respond_to_missing?
46
+ end
47
+ end
48
+
49
+ def respond_to_missing?(method_name, include_private = false)
50
+ new.respond_to?(method_name, include_private) || super(method_name, include_private)
51
+ end
52
+ end
53
+ end
54
+
55
+ require_relative 'nuki_api/client'
data/nuki_api.gemspec ADDED
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/nuki_api/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'nuki_api'
7
+ spec.version = NukiApi::VERSION
8
+ spec.authors = ['Dinis']
9
+ spec.email = ['dinis@lage.pw']
10
+
11
+ spec.summary = 'Nuki API Wrapper.'
12
+ spec.description = 'A gem that implements functions from the FS API available for its users.'
13
+ spec.homepage = 'https://github.com/dlage/nuki_api_gem'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
16
+
17
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = 'https://github.com/dlage/nuki_api_gem'
21
+ spec.metadata['changelog_uri'] = 'https://github.com/dlage/nuki_api_gem/blob/master/CHANGELOG.md'
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ # Uncomment to register a new dependency of your gem
33
+ # spec.add_dependency "example-gem", "~> 1.0"
34
+ spec.add_dependency 'api_cache', '~> 0.3.0'
35
+ spec.add_dependency 'faraday', '~> 2.6.0'
36
+ spec.add_dependency 'oj', '~> 3.11'
37
+
38
+ spec.add_dependency 'dry-configurable', '~> 0.13'
39
+
40
+ # For more information and examples about making a new gem, checkout our
41
+ # guide at: https://bundler.io/guides/creating_gem.html
42
+ end
data/nuki_api_gem.iml ADDED
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="NewModuleRootManager" inherit-compiler-output="true">
4
+ <exclude-output />
5
+ <content url="file://$MODULE_DIR$" />
6
+ <orderEntry type="inheritedJdk" />
7
+ <orderEntry type="sourceFolder" forTests="false" />
8
+ </component>
9
+ </module>
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nuki_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dinis
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-02-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: api_cache
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.3.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.6.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.6.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: oj
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.11'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: dry-configurable
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.13'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.13'
69
+ description: A gem that implements functions from the FS API available for its users.
70
+ email:
71
+ - dinis@lage.pw
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".github/workflows/main.yml"
77
+ - ".gitignore"
78
+ - ".irbrc"
79
+ - ".rspec"
80
+ - ".rubocop.yml"
81
+ - CHANGELOG.md
82
+ - CODE_OF_CONDUCT.md
83
+ - Dockerfile
84
+ - Gemfile
85
+ - Gemfile.lock
86
+ - LICENSE.txt
87
+ - README.md
88
+ - Rakefile
89
+ - bin/console
90
+ - bin/setup
91
+ - docker-compose.yml
92
+ - lib/nuki_api.rb
93
+ - lib/nuki_api/api.rb
94
+ - lib/nuki_api/api_exceptions.rb
95
+ - lib/nuki_api/client.rb
96
+ - lib/nuki_api/constants.rb
97
+ - lib/nuki_api/http_status_codes.rb
98
+ - lib/nuki_api/version.rb
99
+ - nuki_api.gemspec
100
+ - nuki_api_gem.iml
101
+ homepage: https://github.com/dlage/nuki_api_gem
102
+ licenses:
103
+ - MIT
104
+ metadata:
105
+ allowed_push_host: https://rubygems.org
106
+ homepage_uri: https://github.com/dlage/nuki_api_gem
107
+ source_code_uri: https://github.com/dlage/nuki_api_gem
108
+ changelog_uri: https://github.com/dlage/nuki_api_gem/blob/master/CHANGELOG.md
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 2.4.0
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubygems_version: 3.2.3
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Nuki API Wrapper.
128
+ test_files: []