switchbot 0.8.0 → 0.9.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cc3479417cb8f17cdb727ce13981d7bca88163f75bc14419d7fcb1c446a355a0
4
- data.tar.gz: a8fb35982f2b10684f89ad3a7bb20604198d24b2966cec99ca81aff5cfc51ce7
3
+ metadata.gz: be1e25f69c499cba70f375ebb2d95ba8603620f72a1c4288318a36f01f2ab7d1
4
+ data.tar.gz: ae15d1012b74b95b0e930a914201d3ba5bfcd62636894d5efb952d0af60b2452
5
5
  SHA512:
6
- metadata.gz: a6f2df352088e371a871e10c8685139a33320c92254fd8ee9231dfc8927090e8fc3643d1dd706e09ca71ccce64646c0b5fceba7e2404e6c45bf38093dd7edef7
7
- data.tar.gz: 725ea36d3fedf1f53d383e60dff9fd246865e920d023f98d4978a1f43f0ad68f8c977f28e839d8cc77002bfca9923cf0f7895eb5ff98f7dc8daaf264ca9b78f8
6
+ metadata.gz: 842235f1b0db8564903ab0650d84a01a6312b9f1def3fbdce62c1ba2aee02c66ac1f8828f7144c565b1e2e63b045c311fc43a7068dc52e454426e7c6568c77ca
7
+ data.tar.gz: e59de5df165265cbbdab81c333c26196a193d3f1386413aa87375ae499235bd4ef6e212566326df2aafef349b6a101aec5ef1b113a7ea33ec54d85265a9bfdac
@@ -0,0 +1 @@
1
+ * @ytkg
@@ -0,0 +1,11 @@
1
+ # To get started with Dependabot version updates, you'll need to specify which
2
+ # package ecosystems to update and where the package manifests are located.
3
+ # Please see the documentation for all configuration options:
4
+ # https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
5
+
6
+ version: 2
7
+ updates:
8
+ - package-ecosystem: "github-actions"
9
+ directory: "/"
10
+ schedule:
11
+ interval: "daily"
@@ -1,6 +1,6 @@
1
1
  name: Ruby
2
2
 
3
- on: [push,pull_request]
3
+ on: push
4
4
 
5
5
  jobs:
6
6
  build:
@@ -8,10 +8,10 @@ jobs:
8
8
  strategy:
9
9
  matrix:
10
10
  # Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
11
- ruby: [2.6, 2.7, '3.0', 3.1, 3.2]
11
+ ruby: [2.6, 2.7, '3.0', 3.1, 3.2, 3.3]
12
12
  name: Ruby ${{ matrix.ruby }}
13
13
  steps:
14
- - uses: actions/checkout@v3
14
+ - uses: actions/checkout@v6
15
15
  - name: Set up Ruby
16
16
  uses: ruby/setup-ruby@v1
17
17
  with:
data/CHANGELOG.md CHANGED
@@ -1,72 +1,97 @@
1
- ## Unreleased
2
- [full changelog](http://github.com/ytkg/switchbot/compare/v0.8.0...main)
1
+ # Changelog
3
2
 
4
- ## v0.8.0
5
- [full changelog](http://github.com/ytkg/switchbot/compare/v0.7.0...v0.8.0)
3
+ All notable changes to this project will be documented in this file.
6
4
 
7
- * Add Plug Mini
8
- * https://github.com/ytkg/switchbot/pull/21
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
9
7
 
10
- ## v0.7.0
11
- [full changelog](http://github.com/ytkg/switchbot/compare/v0.6.0...v0.7.0)
8
+ ## [Unreleased]
12
9
 
13
- * Add Lock
14
- * https://github.com/ytkg/switchbot/pull/16
15
- * :bomb: **[BREAKING CHANGE]** Support SwitchBot API v1.1
16
- * https://github.com/ytkg/switchbot/pull/15
17
- * The authentication method has been changed.
10
+ ## [0.9.0] - 2026-02-23
18
11
 
19
- ```ruby
20
- client = Switchbot::Client.new('YOUR_TOKEN', 'YOUR_SECRET')
21
- ```
12
+ ### Added
13
+ - Allow `Switchbot::Client.new` to read `SWITCHBOT_API_TOKEN` and `SWITCHBOT_API_SECRET` when arguments are omitted. ([#25](https://github.com/ytkg/switchbot/pull/25))
14
+ Usage:
22
15
 
23
- You must update the app to the latest version, V6.14 or later, in order to get the secret key.
16
+ ```ruby
17
+ ENV['SWITCHBOT_API_TOKEN'] = 'YOUR_TOKEN'
18
+ ENV['SWITCHBOT_API_SECRET'] = 'YOUR_SECRET'
19
+ client = Switchbot::Client.new
20
+ ```
24
21
 
25
- ## v0.6.0
26
- [full changelog](http://github.com/ytkg/switchbot/compare/v0.5.1...v0.6.0)
22
+ ## [0.8.0] - 2023-03-21
27
23
 
28
- * Add ColorBulb
29
- * https://github.com/ytkg/switchbot/pull/14
30
- * :bomb: **[BREAKING CHANGE]** Update faraday v2 and Drop support ruby 2.5
31
- * https://github.com/ytkg/switchbot/pull/12
24
+ ### Added
25
+ - Add Plug Mini support. ([#21](https://github.com/ytkg/switchbot/pull/21))
32
26
 
33
- ## v0.5.1
34
- [full changelog](http://github.com/ytkg/switchbot/compare/v0.5.0...v0.5.1)
27
+ ## [0.7.0] - 2022-10-27
35
28
 
36
- * Enable MFA requirement for gem releasing
37
- * https://github.com/ytkg/switchbot/pull/11
29
+ ### Added
30
+ - Add Lock support. ([#16](https://github.com/ytkg/switchbot/pull/16))
38
31
 
39
- ## v0.5.0
40
- [full changelog](http://github.com/ytkg/switchbot/compare/v0.4.0...v0.5.0)
32
+ ### Changed
33
+ - **BREAKING:** Support SwitchBot API v1.1. ([#15](https://github.com/ytkg/switchbot/pull/15))
34
+ The authentication method has changed:
41
35
 
42
- * Add Bot
43
- * https://github.com/ytkg/switchbot/pull/9
36
+ ```ruby
37
+ client = Switchbot::Client.new('YOUR_TOKEN', 'YOUR_SECRET')
38
+ ```
44
39
 
45
- ## v0.4.0
46
- [full changelog](http://github.com/ytkg/switchbot/compare/v0.3.0...v0.4.0)
40
+ App version 6.14 or later is required to get the secret key.
47
41
 
48
- * Add Humidifier
49
- * https://github.com/ytkg/switchbot/pull/7
50
- * Add Light
51
- * https://github.com/ytkg/switchbot/pull/6
52
- * Add Device#on? and Device#off?
53
- * https://github.com/ytkg/switchbot/pull/5
42
+ ## [0.6.0] - 2022-05-25
54
43
 
55
- ## v0.3.0
56
- [full changelog](http://github.com/ytkg/switchbot/compare/v0.2.0...v0.3.0)
44
+ ### Added
45
+ - Add ColorBulb support. ([#14](https://github.com/ytkg/switchbot/pull/14))
57
46
 
58
- * Add `Device#on` and `Device#off`
59
- * https://github.com/ytkg/switchbot/pull/4
60
- * Add Device#commands
61
- * https://github.com/ytkg/switchbot/pull/3
47
+ ### Changed
48
+ - Update Faraday to v2. ([#12](https://github.com/ytkg/switchbot/pull/12))
62
49
 
63
- ## v0.2.0
64
- [full changelog](http://github.com/ytkg/switchbot/compare/v0.1.0...v0.2.0)
50
+ ### Removed
51
+ - **BREAKING:** Drop support for Ruby 2.5. ([#12](https://github.com/ytkg/switchbot/pull/12))
65
52
 
66
- * Add resource methods
67
- * https://github.com/ytkg/switchbot/pull/2
68
- * Support scenes endpoint
69
- * https://github.com/ytkg/switchbot/pull/1
53
+ ## [0.5.1] - 2022-01-04
70
54
 
71
- ## v0.1.0
72
- * first release
55
+ ### Security
56
+ - Enable MFA requirement for gem releasing. ([#11](https://github.com/ytkg/switchbot/pull/11))
57
+
58
+ ## [0.5.0] - 2021-06-25
59
+
60
+ ### Added
61
+ - Add Bot support. ([#9](https://github.com/ytkg/switchbot/pull/9))
62
+
63
+ ## [0.4.0] - 2021-04-23
64
+
65
+ ### Added
66
+ - Add Humidifier support. ([#7](https://github.com/ytkg/switchbot/pull/7))
67
+ - Add Light support. ([#6](https://github.com/ytkg/switchbot/pull/6))
68
+ - Add `Device#on?` and `Device#off?`. ([#5](https://github.com/ytkg/switchbot/pull/5))
69
+
70
+ ## [0.3.0] - 2021-03-07
71
+
72
+ ### Added
73
+ - Add `Device#on` and `Device#off`. ([#4](https://github.com/ytkg/switchbot/pull/4))
74
+ - Add `Device#commands`. ([#3](https://github.com/ytkg/switchbot/pull/3))
75
+
76
+ ## [0.2.0] - 2021-03-05
77
+
78
+ ### Added
79
+ - Add resource methods. ([#2](https://github.com/ytkg/switchbot/pull/2))
80
+ - Support scenes endpoint. ([#1](https://github.com/ytkg/switchbot/pull/1))
81
+
82
+ ## [0.1.0] - 2021-03-02
83
+
84
+ ### Added
85
+ - First release.
86
+
87
+ [Unreleased]: https://github.com/ytkg/switchbot/compare/v0.9.0...HEAD
88
+ [0.9.0]: https://github.com/ytkg/switchbot/compare/v0.8.0...v0.9.0
89
+ [0.8.0]: https://github.com/ytkg/switchbot/compare/v0.7.0...v0.8.0
90
+ [0.7.0]: https://github.com/ytkg/switchbot/compare/v0.6.0...v0.7.0
91
+ [0.6.0]: https://github.com/ytkg/switchbot/compare/v0.5.1...v0.6.0
92
+ [0.5.1]: https://github.com/ytkg/switchbot/compare/v0.5.0...v0.5.1
93
+ [0.5.0]: https://github.com/ytkg/switchbot/compare/v0.4.0...v0.5.0
94
+ [0.4.0]: https://github.com/ytkg/switchbot/compare/v0.3.0...v0.4.0
95
+ [0.3.0]: https://github.com/ytkg/switchbot/compare/v0.2.0...v0.3.0
96
+ [0.2.0]: https://github.com/ytkg/switchbot/compare/v0.1.0...v0.2.0
97
+ [0.1.0]: https://github.com/ytkg/switchbot/releases/tag/v0.1.0
data/README.md CHANGED
@@ -22,10 +22,16 @@ Or install it yourself as:
22
22
  ```ruby
23
23
  require 'switchbot'
24
24
 
25
+ # pass token and secret directly
25
26
  client = Switchbot::Client.new('YOUR_TOKEN', 'YOUR_SECRET')
27
+
28
+ # or use environment variables
29
+ ENV['SWITCHBOT_API_TOKEN'] = 'YOUR_TOKEN'
30
+ ENV['SWITCHBOT_API_SECRET'] = 'YOUR_SECRET'
31
+ client = Switchbot::Client.new
26
32
  ```
27
33
 
28
- For v0.6.0 and below, refer to README-v0.6.0-and-below.md.
34
+ For v0.6.0 and below, refer to [README-v0.6.0-and-below.md](https://github.com/ytkg/switchbot/blob/main/README-v0.6.0-and-below.md).
29
35
 
30
36
  ### Get device list
31
37
  GET https://api.switch-bot.com/v1.1/devices
@@ -114,7 +120,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
114
120
 
115
121
  ## Contributing
116
122
 
117
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/switchbot. 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]/switchbot/blob/master/CODE_OF_CONDUCT.md).
123
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ytkg/switchbot. 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/ytkg/switchbot/blob/master/CODE_OF_CONDUCT.md).
118
124
 
119
125
  ## License
120
126
 
@@ -122,4 +128,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
122
128
 
123
129
  ## Code of Conduct
124
130
 
125
- Everyone interacting in the Switchbot project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/switchbot/blob/master/CODE_OF_CONDUCT.md).
131
+ Everyone interacting in the Switchbot project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ytkg/switchbot/blob/master/CODE_OF_CONDUCT.md).
@@ -2,19 +2,15 @@
2
2
 
3
3
  module Switchbot
4
4
  class Client
5
- API_ENDPOINT = 'https://api.switch-bot.com'
6
5
  API_VERSION = 'v1.1'
7
6
 
8
- def initialize(token, secret)
9
- @token = token
10
- @secret = secret
7
+ def initialize(token = nil, secret = nil)
8
+ @token = token || ENV.fetch('SWITCHBOT_API_TOKEN', '')
9
+ @secret = secret || ENV.fetch('SWITCHBOT_API_SECRET', '')
11
10
  end
12
11
 
13
12
  def devices
14
- request(
15
- http_method: :get,
16
- endpoint: "/#{API_VERSION}/devices"
17
- )
13
+ request.get("/#{API_VERSION}/devices")
18
14
  end
19
15
 
20
16
  def device(device_id)
@@ -22,29 +18,16 @@ module Switchbot
22
18
  end
23
19
 
24
20
  def status(device_id:)
25
- request(
26
- http_method: :get,
27
- endpoint: "/#{API_VERSION}/devices/#{device_id}/status"
28
- )
21
+ request.get("/#{API_VERSION}/devices/#{device_id}/status")
29
22
  end
30
23
 
31
24
  def commands(device_id:, command:, parameter: 'default', command_type: 'command')
32
- request(
33
- http_method: :post,
34
- endpoint: "/#{API_VERSION}/devices/#{device_id}/commands",
35
- params: {
36
- command: command,
37
- parameter: parameter,
38
- commandType: command_type
39
- }
40
- )
25
+ request.post("/#{API_VERSION}/devices/#{device_id}/commands",
26
+ params: { command: command, parameter: parameter, commandType: command_type })
41
27
  end
42
28
 
43
29
  def scenes
44
- request(
45
- http_method: :get,
46
- endpoint: "/#{API_VERSION}/scenes"
47
- )
30
+ request.get("/#{API_VERSION}/scenes")
48
31
  end
49
32
 
50
33
  def scene(scene_id)
@@ -52,10 +35,7 @@ module Switchbot
52
35
  end
53
36
 
54
37
  def execute(scene_id:)
55
- request(
56
- http_method: :post,
57
- endpoint: "/#{API_VERSION}/scenes/#{scene_id}/execute"
58
- )
38
+ request.post("/#{API_VERSION}/scenes/#{scene_id}/execute")
59
39
  end
60
40
 
61
41
  def bot(device_id)
@@ -84,31 +64,8 @@ module Switchbot
84
64
 
85
65
  private
86
66
 
87
- def headers
88
- t = "#{Time.now.to_i}000"
89
- nonce = SecureRandom.alphanumeric
90
- sign = Base64.strict_encode64(OpenSSL::HMAC.digest('sha256', @secret, "#{@token}#{t}#{nonce}"))
91
-
92
- {
93
- 'User-Agent' => "Switchbot v#{Switchbot::VERSION} (https://github.com/ytkg/switchbot)",
94
- 'Authorization' => @token,
95
- 'T' => t,
96
- 'Sign' => sign,
97
- 'Nonce' => nonce
98
- }
99
- end
100
-
101
- def connection
102
- Faraday.new(url: API_ENDPOINT, headers: headers) do |conn|
103
- conn.request :json
104
- conn.response :json
105
- conn.adapter :typhoeus, http_version: :httpv2_0 # rubocop:disable Naming/VariableNumber
106
- end
107
- end
108
-
109
- def request(http_method:, endpoint:, params: {})
110
- response = connection.public_send(http_method, endpoint, params)
111
- response.body.deep_transform_keys(&:underscore).deep_symbolize_keys
67
+ def request
68
+ @request ||= Request.new(@token, @secret)
112
69
  end
113
70
  end
114
71
  end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Switchbot
4
+ class Request
5
+ API_ENDPOINT = 'https://api.switch-bot.com'
6
+
7
+ def initialize(token, secret)
8
+ @token = token
9
+ @secret = secret
10
+ end
11
+
12
+ def get(endpoint)
13
+ request(http_method: :get, endpoint: endpoint)
14
+ end
15
+
16
+ def post(endpoint, params: {})
17
+ request(http_method: :post, endpoint: endpoint, params: params)
18
+ end
19
+
20
+ private
21
+
22
+ def headers
23
+ t = "#{Time.now.to_i}000"
24
+ nonce = SecureRandom.alphanumeric
25
+ sign = Base64.strict_encode64(OpenSSL::HMAC.digest('sha256', @secret, "#{@token}#{t}#{nonce}"))
26
+
27
+ {
28
+ 'User-Agent' => "Switchbot v#{Switchbot::VERSION} (https://github.com/ytkg/switchbot)",
29
+ 'Authorization' => @token,
30
+ 'T' => t,
31
+ 'Sign' => sign,
32
+ 'Nonce' => nonce
33
+ }
34
+ end
35
+
36
+ def connection
37
+ Faraday.new(url: API_ENDPOINT, headers: headers) do |conn|
38
+ conn.request :json
39
+ conn.response :json
40
+ conn.adapter :typhoeus, http_version: :httpv2_0 # rubocop:disable Naming/VariableNumber
41
+ end
42
+ end
43
+
44
+ def request(http_method:, endpoint:, params: {})
45
+ response = connection.public_send(http_method, endpoint, params)
46
+ response.body.deep_transform_keys(&:underscore).deep_symbolize_keys
47
+ end
48
+ end
49
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Switchbot
4
- VERSION = '0.8.0'
4
+ VERSION = '0.9.0'
5
5
  end
data/lib/switchbot.rb CHANGED
@@ -8,6 +8,7 @@ require 'faraday/typhoeus'
8
8
  require 'active_support/all'
9
9
  require_relative 'switchbot/version'
10
10
  require_relative 'switchbot/client'
11
+ require_relative 'switchbot/request'
11
12
  require_relative 'switchbot/device'
12
13
  require_relative 'switchbot/scene'
13
14
  require_relative 'switchbot/bot'
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switchbot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshiki Takagi
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2023-03-21 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: activesupport
@@ -129,6 +128,8 @@ executables: []
129
128
  extensions: []
130
129
  extra_rdoc_files: []
131
130
  files:
131
+ - ".github/CODEOWNERS"
132
+ - ".github/dependabot.yml"
132
133
  - ".github/workflows/main.yml"
133
134
  - ".gitignore"
134
135
  - ".rspec"
@@ -151,6 +152,7 @@ files:
151
152
  - lib/switchbot/light.rb
152
153
  - lib/switchbot/lock.rb
153
154
  - lib/switchbot/plug_mini.rb
155
+ - lib/switchbot/request.rb
154
156
  - lib/switchbot/scene.rb
155
157
  - lib/switchbot/version.rb
156
158
  - switchbot.gemspec
@@ -162,7 +164,6 @@ metadata:
162
164
  source_code_uri: https://github.com/ytkg/switchbot
163
165
  changelog_uri: https://github.com/ytkg/switchbot/blob/main/CHANGELOG.md
164
166
  rubygems_mfa_required: 'true'
165
- post_install_message:
166
167
  rdoc_options: []
167
168
  require_paths:
168
169
  - lib
@@ -177,8 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
177
178
  - !ruby/object:Gem::Version
178
179
  version: '0'
179
180
  requirements: []
180
- rubygems_version: 3.3.22
181
- signing_key:
181
+ rubygems_version: 4.0.6
182
182
  specification_version: 4
183
183
  summary: SwitchBot API client for Ruby
184
184
  test_files: []