nanoleaf_ruby 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7fbdf59685d75b0b2ddca3e5de9fec5c6fdb9309
4
+ data.tar.gz: 65726a9b5c783296eaa7d05fd7cfab856792bbfe
5
+ SHA512:
6
+ metadata.gz: 3446f90408297878360ef0736b2739144794a09e26437f84475609e5d49558eefed4354187cf03257571a6865da877570d0fb0360b6822c9de5c6a3d67355749
7
+ data.tar.gz: dd1d41edaabc8bad952af535baf920163c246ea99989f2d3feac115d5f29fbbe050c41aa9410109331fbcf590e7e7613e0a398f0f2c54f30866f8f572670f3f3
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.gem
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,89 @@
1
+ require:
2
+ - rubocop-rspec
3
+
4
+ AllCops:
5
+ Exclude:
6
+ - 'vendor/**/*'
7
+ - 'spec/fixtures/**/*'
8
+ - 'tmp/**/*'
9
+ TargetRubyVersion: 2.3
10
+
11
+ Naming/PredicateName:
12
+ MethodDefinitionMacros:
13
+ - define_method
14
+ - define_singleton_method
15
+ - def_node_matcher
16
+ - def_node_search
17
+
18
+ Metrics/LineLength:
19
+ Max: 100
20
+
21
+ Metrics/ClassLength:
22
+ Exclude:
23
+ - 'lib/nanoleaf_ruby.rb'
24
+
25
+ Style/Documentation:
26
+ Enabled: false
27
+
28
+ Naming/AccessorMethodName:
29
+ Enabled: false
30
+
31
+ Style/FrozenStringLiteralComment:
32
+ Enabled: false
33
+
34
+ Layout/EndOfLine:
35
+ EnforcedStyle: lf
36
+
37
+ Layout/ClassStructure:
38
+ Enabled: true
39
+ Categories:
40
+ module_inclusion:
41
+ - include
42
+ - prepend
43
+ - extend
44
+ ExpectedOrder:
45
+ - module_inclusion
46
+ - constants
47
+ - public_class_methods
48
+ - initializer
49
+ - instance_methods
50
+ - protected_methods
51
+ - private_methods
52
+
53
+ Layout/IndentHeredoc:
54
+ EnforcedStyle: powerpack
55
+
56
+ Lint/AmbiguousBlockAssociation:
57
+ Exclude:
58
+ - 'spec/*.rb'
59
+
60
+ Lint/InterpolationCheck:
61
+ Exclude:
62
+ - 'spec/*.rb'
63
+
64
+ Lint/UselessAccessModifier:
65
+ MethodCreatingMethods:
66
+ - 'def_matcher'
67
+ - 'def_node_matcher'
68
+
69
+ Lint/BooleanSymbol:
70
+ Enabled: false
71
+
72
+ Metrics/BlockLength:
73
+ Exclude:
74
+ - 'Rakefile'
75
+ - 'lib/tasks/*.rake'
76
+ - 'spec/*.rb'
77
+
78
+ Metrics/ModuleLength:
79
+ Exclude:
80
+ - 'spec/*.rb'
81
+
82
+ RSpec/PredicateMatcher:
83
+ EnforcedStyle: explicit
84
+
85
+ RSpec/ContextWording:
86
+ Enabled: false
87
+
88
+ RSpec/NestedGroups:
89
+ Max: 7
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ cache: bundler
3
+
4
+ rvm:
5
+ - 2.3.6
6
+
7
+ script: 'bundle exec rake spec'
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at estiens@users.noreply.github.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,79 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ nanoleaf_ruby (1.0.0)
5
+ chroma (~> 0.2)
6
+ httparty (~> 0.15)
7
+ pry (~> 0.11)
8
+ ssdp (~> 1.1)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ addressable (2.5.2)
14
+ public_suffix (>= 2.0.2, < 4.0)
15
+ ast (2.4.0)
16
+ chroma (0.2.0)
17
+ coderay (1.1.2)
18
+ crack (0.4.3)
19
+ safe_yaml (~> 1.0.0)
20
+ diff-lcs (1.3)
21
+ hashdiff (0.3.7)
22
+ httparty (0.16.2)
23
+ multi_xml (>= 0.5.2)
24
+ jaro_winkler (1.5.1)
25
+ method_source (0.9.0)
26
+ multi_xml (0.6.0)
27
+ parallel (1.12.1)
28
+ parser (2.5.1.0)
29
+ ast (~> 2.4.0)
30
+ powerpack (0.1.1)
31
+ pry (0.11.3)
32
+ coderay (~> 1.1.0)
33
+ method_source (~> 0.9.0)
34
+ public_suffix (3.0.2)
35
+ rainbow (3.0.0)
36
+ rake (10.5.0)
37
+ rspec (3.7.0)
38
+ rspec-core (~> 3.7.0)
39
+ rspec-expectations (~> 3.7.0)
40
+ rspec-mocks (~> 3.7.0)
41
+ rspec-core (3.7.1)
42
+ rspec-support (~> 3.7.0)
43
+ rspec-expectations (3.7.0)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.7.0)
46
+ rspec-mocks (3.7.0)
47
+ diff-lcs (>= 1.2.0, < 2.0)
48
+ rspec-support (~> 3.7.0)
49
+ rspec-support (3.7.1)
50
+ rubocop (0.57.1)
51
+ jaro_winkler (~> 1.5.1)
52
+ parallel (~> 1.10)
53
+ parser (>= 2.5)
54
+ powerpack (~> 0.1)
55
+ rainbow (>= 2.2.2, < 4.0)
56
+ ruby-progressbar (~> 1.7)
57
+ unicode-display_width (~> 1.0, >= 1.0.1)
58
+ ruby-progressbar (1.9.0)
59
+ safe_yaml (1.0.4)
60
+ ssdp (1.1.7)
61
+ unicode-display_width (1.4.0)
62
+ webmock (3.4.2)
63
+ addressable (>= 2.3.6)
64
+ crack (>= 0.3.2)
65
+ hashdiff
66
+
67
+ PLATFORMS
68
+ ruby
69
+
70
+ DEPENDENCIES
71
+ bundler (~> 1.16)
72
+ nanoleaf_ruby!
73
+ rake (~> 10.0)
74
+ rspec (~> 3.0)
75
+ rubocop (~> 0.57)
76
+ webmock (~> 3.4)
77
+
78
+ BUNDLED WITH
79
+ 1.16.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Eric Stiens
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.
@@ -0,0 +1,190 @@
1
+ # Nanoleaf Ruby
2
+ [![Build Status](https://travis-ci.org/estiens/nanoleaf_ruby.svg?branch=master)](https://travis-ci.org/estiens/nanoleaf_ruby)
3
+
4
+ This gem is a wrapper around the Nanoleaf Open API (http://forum.nanoleaf.me/docs/openapi). It provides a wrapper around all the main API functions and also adds some advanced color commands so you can set the color of the tiles by named colors, hex values or rgb values. It is a work in progress and will be kept up to date with the Open API.
5
+
6
+ Rhythm commands and the ability to upload animations will be added shortly.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'nanoleaf_ruby'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install nanoleaf_ruby
23
+
24
+ ## Version
25
+
26
+ Gem version 1.0.x is up to date with Nanoleaf Open API v1 (2.2.0)
27
+
28
+ ## Usage
29
+
30
+ ### Initialize API and call it
31
+
32
+ ```ruby
33
+ require 'nanoleaf_ruby'
34
+
35
+ api = NanoleafRuby::Api.new(ip_address: 191.161.1.1:16021, token: 'foobar')
36
+
37
+ api.brightness_up
38
+ ```
39
+
40
+ Note that you can initialize the api client with the IP address and token or you can include them in your env by setting `ENV['NANOLEAF_API_TOKEN']` and `ENV['NANOLEAF_IP_ADDRESS']` and it will be pulled in automatically.
41
+
42
+ ### Method Responses
43
+
44
+ Most successful PUT responses from the Open API come back as 204.
45
+
46
+ Method responses from the API client will always be a ruby hash and look something like
47
+
48
+ ```ruby
49
+ {:data=>{},
50
+ :success=>true,
51
+ :code=>204,
52
+ :raw=>{:body=>nil, :params=>{:brightness=>{:value=>10, :duration=>10}}}}
53
+ ```
54
+
55
+ If a method sends back a JSON object, you'll get it under `[:data]` (for example, `#effect_list` sends back an array of effect titles). You'll also always have access to the original response under `[:raw][:body]` and be able to inspect the params sent to the Open API under `[:raw][:params]`. If `[:success]` is false there will be an error message included as well.
56
+
57
+ ### Methods
58
+
59
+ #### info
60
+ `api.info` returns all the info from your panels
61
+
62
+ #### power on/off
63
+ `api.on` turns the Nanoleaf panel on
64
+
65
+ `api.off` turns the Nanoleaf panel off
66
+
67
+ `api.toggle` toggles the current power status of the Nanoleaf panel
68
+
69
+ #### color mode
70
+ `api.color_mode` returns the current color mode
71
+
72
+ #### identify
73
+ `api.identify` flashes your panel
74
+
75
+ #### brightness
76
+ `api.set_brightness(value, duration = nil)` sets the brightness of the panel. When passed a second argument it will transition to that brightness over that number of seconds.
77
+
78
+ `api.brightness_increment(increment = 1)` allows you to pass a positive or negative value to increment the current brightness
79
+
80
+ `api.brightness_up(increment = 1)` pass a number to increase the brightness by that amount, defaults to 1 without an argument
81
+
82
+ `api.brightness_down(increment = 1)` pass a number to decrease the brightness by that amount, defaults to 1 without an argument
83
+
84
+ `api.get_brightness` gets the current brightness level of the panel
85
+
86
+ #### hue
87
+ `api.set_hue(value)` sets the hue of the panel.
88
+
89
+ `api.hue_increment(increment = 1)` allows you to pass a positive or negative value to increment the current hue
90
+
91
+ `api.hue_up(increment = 1)` pass a number to increase the hue by that amount, defaults to 1 without an argument
92
+
93
+ `api.hue_down(increment = 1)` pass a number to decrease the hue by that amount, defaults to 1 without an argument
94
+
95
+ `api.get_hue` gets the current hue level of the panel
96
+
97
+ #### color temperature
98
+ `api.set_ct(value)` sets the ct of the panel.
99
+
100
+ `api.ct_increment(increment = 1)` allows you to pass a positive or negative value to increment the current ct
101
+
102
+ `api.ct_up(increment = 1)` pass a number to increase the ct by that amount, defaults to 1 without an argument
103
+
104
+ `api.ct_down(increment = 1)` pass a number to decrease the ct by that amount, defaults to 1 without an argument
105
+
106
+ `api.get_ct` gets the current ct level of the panel
107
+
108
+
109
+ #### saturation
110
+ `api.set_ct(value)` sets the ct of the panel.
111
+
112
+ `api.ct_increment(increment = 1)` allows you to pass a positive or negative value to increment the current ct
113
+
114
+ `api.ct_up(increment = 1)` pass a number to increase the ct by that amount, defaults to 1 without an argument
115
+
116
+ `api.ct_down(increment = 1)` pass a number to decrease the ct by that amount, defaults to 1 without an argument
117
+
118
+ `api.get_ct` gets the current ct level of the panel
119
+
120
+
121
+ #### advanced color
122
+ `api.get_rgb` returns the current r,g,b value of the panel
123
+
124
+ `api.set_color(string)` allows you to set a color value from a name or a hex value -- for example `set_color('red')` or `set_color('#FFF')` or `set_color('#33FF33')`
125
+
126
+ `api.set_rgb(r, g, b)` allows you to pass an rgb value directly -- for example `set_rgb(123,100,200)`
127
+
128
+
129
+ #### effects
130
+ `api.effects_list` returns an array of strings that correspond to the names of all the effects on your Nanoleaf
131
+
132
+ `api.choose_effect(string)` changes the effect if it matches the name of one of the effects
133
+
134
+ `api.random_effect` changes the effect to a random effect
135
+
136
+ #### user commands
137
+ `api.delete_auth_token` removes the auth token you are using from the Nanoleaf (danger!)
138
+
139
+ `api.generate_auth_token` returns a new auth token if the Nanoleaf has been set into the new user mode (hold the power button for 5 seconds until the light starts blinking)
140
+
141
+ note: If you are setting up the panel for the first time and you know the IP address but don't have a token, you can instantiate the API with a bogus string for the token and still run this command, however no other command will work
142
+
143
+ ```ruby
144
+ api = NanoleafRuby::Api.new(ip_address: '192.161.1.1:16021', token: 'foobar')
145
+ api.generate_auth_token
146
+ ```
147
+
148
+ ## Rake Tasks
149
+
150
+ To include rake tasks in a ruby app, create a Rakefile and put the following in it. Rails should get the task automatically.
151
+
152
+ ```ruby
153
+ spec = Gem::Specification.find_by_name 'nanoleaf_ruby'
154
+ load "#{spec.gem_dir}/lib/tasks/rake_tasks.rb"
155
+ ```
156
+ ##### nanoleaf:generate_token
157
+ `rake 'nanoleaf:generate_token['192.168.1.1:16021']'`
158
+
159
+ If you want to generate an auth token via rake, just enter the ip address of your panel here (or have it set in `ENV['NANOLEAF_API_TOKEN']`
160
+
161
+ ##### nanoleaf::autodiscover
162
+ `rake 'nanoleaf:autodiscover` will attempt to find your panel(s) via SSDP and print out information about them to the screen (including their IP addresses). I've had mixed luck with it showing up consistently.
163
+
164
+ ## Console
165
+
166
+ If you want to drop straight into a console just run `bin/console`
167
+
168
+ ## Contributing
169
+
170
+ Bug reports and pull requests are welcome on GitHub at https://github.com/estiens/nanoleaf_ruby.
171
+
172
+ Please branch from dev for all pull requests.
173
+ * Fork it
174
+ * Checkout dev (`git checkout dev`)
175
+ * Create your feature branch (`git checkout -b my-new-feature`)
176
+ * Commit your changes (`git commit -am 'Add some feature'`)
177
+ * Push to the branch (`git push origin my-new-feature`)
178
+ * Create a new pull request against dev
179
+
180
+ ## Tests
181
+
182
+ You can run tests with `rake spec`.
183
+
184
+ ## License
185
+
186
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
187
+
188
+ ## Code of Conduct
189
+
190
+ Everyone interacting in the nanoleaf_ruby project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/estiens/nanoleaf_ruby/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'nanoleaf_ruby'
5
+ require 'pry'
6
+
7
+ Pry.start
@@ -0,0 +1,5 @@
1
+ module NanoleafRuby
2
+ module Errors
3
+ class Configuration < StandardError; end
4
+ end
5
+ end
@@ -0,0 +1,216 @@
1
+ require 'nanoleaf_ruby/version'
2
+ require 'nanoleaf_ruby/color_utils'
3
+ require 'nanoleaf_ruby/base_requester'
4
+ require 'errors/configuration_error'
5
+ require 'railtie' if defined?(Rails)
6
+
7
+ module NanoleafRuby
8
+ class Api
9
+ def initialize(token: nil, ip_address: nil, raise_errors: false)
10
+ @access_token = ENV['NANOLEAF_API_TOKEN'] || token
11
+ @nanoleaf_ip = ENV['NANOLEAF_IP_ADDRESS'] || ip_address
12
+ @api_url = get_api_url
13
+ @requester = BaseRequester.new(raise_errors: raise_errors)
14
+ end
15
+
16
+ # info dump from panels
17
+ def info
18
+ @requester.get(url: @api_url)
19
+ end
20
+
21
+ # power on/off
22
+ def on
23
+ params = { on: true }
24
+ @requester.put(url: "#{@api_url}/state", params: params)
25
+ end
26
+
27
+ def off
28
+ params = { on: false }
29
+ @requester.put(url: "#{@api_url}/state", params: params)
30
+ end
31
+
32
+ def toggle
33
+ response = @requester.get(url: "#{@api_url}/state/on/value")
34
+ on = response.dig(:raw, :body) == 'true'
35
+ params = { on: !on }
36
+ @requester.put(url: "#{@api_url}/state", params: params)
37
+ end
38
+
39
+ # color mode
40
+ def color_mode
41
+ @requester.get(url: "#{@api_url}/state/colorMode")
42
+ end
43
+
44
+ # flash panels
45
+ def identify
46
+ @requester.put(url: "#{@api_url}/identify")
47
+ end
48
+
49
+ # brightness commands
50
+ def brightness_increment(increment = 1)
51
+ params = { brightness: {} }
52
+ params[:brightness][:increment] = increment
53
+ @requester.put(url: "#{@api_url}/state", params: params)
54
+ end
55
+
56
+ def brightness_up(increment = 1)
57
+ brightness_increment(increment)
58
+ end
59
+
60
+ def brightness_down(increment = 1)
61
+ brightness_increment(-increment)
62
+ end
63
+
64
+ def set_brightness(value, duration = nil)
65
+ params = { brightness: {} }
66
+ params[:brightness][:value] = value
67
+ params[:brightness][:duration] = duration if duration
68
+ @requester.put(url: "#{@api_url}/state", params: params)
69
+ end
70
+
71
+ def get_brightness
72
+ @requester.get(url: "#{@api_url}/state/brightness")
73
+ end
74
+
75
+ # hue commands
76
+ def hue_increment(increment = 1)
77
+ params = { hue: {} }
78
+ params[:hue][:increment] = increment
79
+ @requester.put(url: "#{@api_url}/state", params: params)
80
+ end
81
+
82
+ def hue_up(increment = 1)
83
+ hue_increment(increment)
84
+ end
85
+
86
+ def hue_down(increment = 1)
87
+ hue_increment(-increment)
88
+ end
89
+
90
+ def set_hue(value)
91
+ params = { hue: {} }
92
+ params[:hue][:value] = value
93
+ @requester.put(url: "#{@api_url}/state", params: params)
94
+ end
95
+
96
+ def get_hue
97
+ @requester.get(url: "#{@api_url}/state/hue")
98
+ end
99
+
100
+ # color temperature commands
101
+ def ct_increment(increment = 1)
102
+ params = { ct: {} }
103
+ params[:ct][:increment] = increment
104
+ @requester.put(url: "#{@api_url}/state", params: params)
105
+ end
106
+
107
+ def ct_up(increment = 1)
108
+ ct_increment(increment)
109
+ end
110
+
111
+ def ct_down(increment = 1)
112
+ ct_increment(-increment)
113
+ end
114
+
115
+ def set_ct(value)
116
+ params = { ct: {} }
117
+ params[:ct][:value] = value
118
+ @requester.put(url: "#{@api_url}/state", params: params)
119
+ end
120
+
121
+ def get_ct
122
+ @requester.get(url: "#{@api_url}/state/ct")
123
+ end
124
+
125
+ # saturation commands
126
+ def sat_increment(increment = 1)
127
+ params = { sat: {} }
128
+ params[:sat][:increment] = increment
129
+ @requester.put(url: "#{@api_url}/state", params: params)
130
+ end
131
+
132
+ def sat_up(increment = 1)
133
+ sat_increment(increment)
134
+ end
135
+
136
+ def sat_down(increment = 1)
137
+ sat_increment(-increment)
138
+ end
139
+
140
+ def set_sat(value)
141
+ params = { sat: {} }
142
+ params[:sat][:value] = value
143
+ @requester.put(url: "#{@api_url}/state", params: params)
144
+ end
145
+
146
+ def get_sat
147
+ @requester.get(url: "#{@api_url}/state/sat")
148
+ end
149
+
150
+ # color_commands
151
+ def get_rgb
152
+ hue = get_hue.dig(:data, 'value')
153
+ saturation = get_sat.dig(:data, 'value')
154
+ brightness = get_brightness.dig(:data, 'value')
155
+ ColorUtils.hsv_to_rgb(hue, saturation, brightness)
156
+ end
157
+
158
+ def set_color(string)
159
+ hsb = ColorUtils.get_color(string)
160
+ write_color(hsb)
161
+ end
162
+
163
+ def set_rgb(red, green, blue)
164
+ hsb = ColorUtils.get_color("rgb(#{red},#{green},#{blue})")
165
+ write_color(hsb)
166
+ end
167
+
168
+ def write_color(hsb)
169
+ params = { hue: { value: hsb[0] } }
170
+ params[:sat] = { value: hsb[1] }
171
+ params[:brightness] = { value: hsb[2] }
172
+ @requester.put(url: "#{@api_url}/state", params: params)
173
+ end
174
+
175
+ # effects
176
+ def effects_list
177
+ response = @requester.get(url: "#{@api_url}/effects/effectsList")
178
+ response[:data]
179
+ end
180
+
181
+ def choose_effect(name)
182
+ params = { select: name }
183
+ @requester.put(url: "#{@api_url}/effects", params: params)
184
+ end
185
+
186
+ def random_effect
187
+ choose_effect(effects_list.sample)
188
+ end
189
+
190
+ # user commands
191
+ def delete_auth_token
192
+ @requester.delete(url: @api_url)
193
+ end
194
+
195
+ def generate_auth_token
196
+ response = @requester.post(url: "http://#{@nanoleaf_ip}/api/v1/new")
197
+ if response[:success]
198
+ puts 'Auth token successfully generated!'
199
+ puts "Token: #{response.dig(:data, 'auth_token')}"
200
+ elsif response[:code] == 403
201
+ puts 'Press and hold the power button for 5-7 seconds first!'
202
+ puts '(Light will begin flashing)'
203
+ else
204
+ puts "Sorry, something went wrong and I don't know what!"
205
+ end
206
+ end
207
+
208
+ private
209
+
210
+ def get_api_url
211
+ raise Errors::Configuration, 'Nanoleaf IP address missing' unless @nanoleaf_ip
212
+ raise Errors::Configuration, 'Nanoleaf access token missing' unless @access_token
213
+ "http://#{@nanoleaf_ip}/api/v1/#{@access_token}"
214
+ end
215
+ end
216
+ end
@@ -0,0 +1,81 @@
1
+ require 'httparty'
2
+
3
+ module NanoleafRuby
4
+ class BaseRequester
5
+ include HTTParty
6
+
7
+ def initialize(raise_errors: false)
8
+ @raise_errors = raise_errors
9
+ end
10
+
11
+ def get(url:)
12
+ response = self.class.get(url)
13
+ parse_response(response: response)
14
+ end
15
+
16
+ def put(url:, params: {}, headers: {})
17
+ body = params.to_json
18
+ headers = headers.merge('Content-Type' => 'application/json',
19
+ 'Content-Length' => body.length.to_s)
20
+ response = self.class.put(url, body: body, headers: headers)
21
+ parse_response(response: response, params: params)
22
+ end
23
+
24
+ def post(url:, params: {}, headers: {})
25
+ response = self.class.post(url, body: params.to_json, headers: headers)
26
+ parse_response(response: response, params: params)
27
+ end
28
+
29
+ def delete(url:, params: {})
30
+ self.class.delete(url, body: params.to_json)
31
+ end
32
+
33
+ private
34
+
35
+ # rubocop:disable Metrics/MethodLength
36
+ def error_lookup(error_code)
37
+ case error_code
38
+ when 401
39
+ 'Not authorized! This is an invalid token. Try calling generate_auth_token'
40
+ when 404
41
+ 'Resource not found'
42
+ when 422
43
+ 'Unprocessible Entity (check your params?)'
44
+ when 500
45
+ 'Internal Server Error'
46
+ else
47
+ 'Something went wrong'
48
+ end
49
+ end
50
+ # rubocop:enable Metrics/MethodLength
51
+
52
+ def parse_response(response:, params: nil, body: {})
53
+ if response.code < 299
54
+ body[:data] = parse_json(response.body)
55
+ body[:success] = true
56
+ else
57
+ error = write_error_message(response)
58
+ body[:error] = error
59
+ body[:success] = false
60
+ end
61
+ body[:code] = response.code
62
+ body.merge!(raw: { body: response.body, params: params })
63
+ end
64
+
65
+ def write_error_message(response)
66
+ error = "Error #{response.code}: #{error_lookup(response.code)}"
67
+ raise error if @raise_errors
68
+ error
69
+ end
70
+
71
+ def parse_json(json_string)
72
+ body = {}
73
+ begin
74
+ body = JSON.parse(json_string) if json_string && !json_string.empty?
75
+ rescue JSON::ParserError
76
+ return {}
77
+ end
78
+ body
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,17 @@
1
+ require 'chroma'
2
+
3
+ module NanoleafRuby
4
+ class ColorUtils
5
+ def self.get_color(value)
6
+ color = Chroma.paint(value).to_hsv
7
+ magic_regex = /hsv\((?<hue>\d+),\s(?<sat>\d+)%,\s(?<bright>\d+)%\)/i
8
+ values = magic_regex.match(color)
9
+ [values[:hue].to_i, values[:sat].to_i, values[:bright].to_i]
10
+ end
11
+
12
+ def self.hsv_to_rgb(hue, sat, val)
13
+ color = Chroma.paint("hsv(#{hue}, #{sat}%, #{val}%)")
14
+ color.to_rgb
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module NanoleafRuby
2
+ VERSION = '1.0.0'.freeze
3
+ end
@@ -0,0 +1,15 @@
1
+ # lib/railtie.rb
2
+
3
+ require 'nanoleaf_ruby'
4
+ require 'rails'
5
+
6
+ module NanoleafRuby
7
+ class Railtie < Rails::Railtie
8
+ railtie_name :nanoleaf_ruby
9
+
10
+ rake_tasks do
11
+ path = File.expand_path(__dir__)
12
+ Dir.glob("#{path}/tasks/**/*.rake").each { |f| load f }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ require 'ssdp'
2
+
3
+ namespace :nanoleaf do
4
+ desc 'tries to find your nanoleaf'
5
+ task :autodiscover do |_task|
6
+ finder = SSDP::Consumer.new(timeout: 30)
7
+ broadcast = '239.255.255.250'
8
+ port = 1900
9
+ service = 'nanoleaf_aurora:light'
10
+ result = finder.search(broadcast: broadcast, port: port, service: service)
11
+ puts result
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ namespace :nanoleaf do
2
+ desc 'generates an auth token if the nanoleaf is in the right mode'
3
+ task :generate_token, [:ip_address] do |_task, args|
4
+ nanoleaf_ip = ENV['NANOLEAF_IP_ADDRESS'] || args[:ip_address]
5
+ api = NanoleafRuby::Api.new(ip_address: nanoleaf_ip, token: 'null')
6
+ response = api.generate_auth_token
7
+ puts response
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ require 'rake'
2
+
3
+ path = File.dirname(__FILE__)
4
+ tasks = Dir.glob("#{path}/*.rake")
5
+ tasks.each { |file| load file }
@@ -0,0 +1,33 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'nanoleaf_ruby/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'nanoleaf_ruby'
7
+ spec.version = NanoleafRuby::VERSION
8
+ spec.authors = ['Eric Stiens']
9
+ spec.email = ['estiens@gmail.com']
10
+ spec.summary = 'This gem provides a ruby wrapper for the Nanoleaf Open Api'
11
+ spec.homepage = 'https://www.github.com/estiens/nanoleaf_ruby'
12
+ spec.license = 'MIT'
13
+
14
+ # Specify which files should be added to the gem when it is released.
15
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
16
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ end
19
+ spec.bindir = 'bin'
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.required_ruby_version = '>= 2.3.0'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.16'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rubocop', '~> 0.57'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'webmock', '~> 3.4'
29
+ spec.add_dependency 'chroma', '~> 0.2'
30
+ spec.add_dependency 'httparty', '~> 0.15'
31
+ spec.add_dependency 'pry', '~> 0.11'
32
+ spec.add_dependency 'ssdp', '~> 1.1'
33
+ end
metadata ADDED
@@ -0,0 +1,191 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nanoleaf_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Eric Stiens
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.57'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.57'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: chroma
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.2'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: httparty
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.15'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.15'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.11'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.11'
125
+ - !ruby/object:Gem::Dependency
126
+ name: ssdp
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.1'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.1'
139
+ description:
140
+ email:
141
+ - estiens@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".rubocop.yml"
149
+ - ".travis.yml"
150
+ - CODE_OF_CONDUCT.md
151
+ - Gemfile
152
+ - Gemfile.lock
153
+ - LICENSE.txt
154
+ - README.md
155
+ - Rakefile
156
+ - bin/console
157
+ - lib/errors/configuration_error.rb
158
+ - lib/nanoleaf_ruby.rb
159
+ - lib/nanoleaf_ruby/base_requester.rb
160
+ - lib/nanoleaf_ruby/color_utils.rb
161
+ - lib/nanoleaf_ruby/version.rb
162
+ - lib/railtie.rb
163
+ - lib/tasks/autodiscover.rake
164
+ - lib/tasks/generate_token.rake
165
+ - lib/tasks/rake_tasks.rb
166
+ - nanoleaf_ruby.gemspec
167
+ homepage: https://www.github.com/estiens/nanoleaf_ruby
168
+ licenses:
169
+ - MIT
170
+ metadata: {}
171
+ post_install_message:
172
+ rdoc_options: []
173
+ require_paths:
174
+ - lib
175
+ required_ruby_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: 2.3.0
180
+ required_rubygems_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ requirements: []
186
+ rubyforge_project:
187
+ rubygems_version: 2.5.2.2
188
+ signing_key:
189
+ specification_version: 4
190
+ summary: This gem provides a ruby wrapper for the Nanoleaf Open Api
191
+ test_files: []