light_me_up 0.1.1 → 0.2.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: 1c0265f93a138b0f04533c48e8ee0db2ad960b0d9940d84030e40209af938bd9
4
- data.tar.gz: 57293c8bff4f7a51e1abdd959645dad756c7b2dc02b6d096211081620bdbe33e
3
+ metadata.gz: 500a673faf1257f9237ad66eb5c47cc852e3304f0aa458b137368afd720a9354
4
+ data.tar.gz: e9bd47febd94eb07be90b4914ad8cf5c2ad9b252751e98ab1fa989a64f77984b
5
5
  SHA512:
6
- metadata.gz: ad82872d763c4971ed34fd6eee722534de858ac2a98137ecef3625a5b516c118455c9f71c81350dc8e1d5ca69711ab9a6e32190781d1ce9c1e9c2fac3015814d
7
- data.tar.gz: ace52c60a228ef88631d6abc0f85b01df21b241b975d3c4fc277982810531147d4ed26691c988caa254ab0ac854c51b839030ee28c66e4ea57d7fe9c6896bd6a
6
+ metadata.gz: 39f34213c145f83d0dc2db551514599d5b66d0d1db5ea5275e64440b5273e7620c7de8d0d31fa67290a1621cf86c1d976e92ba710145629ad673d6004abaf9d2
7
+ data.tar.gz: e7b88500d35fe156b9c1fc22e9b9c19ce3cd58cf7b2a7ffdf8fed9a3200f149a306440492768a20f33d5a26755bbdd190c812e64608ff587d882634a2ec7592b
data/.rubocop.yml CHANGED
@@ -1,5 +1,6 @@
1
1
  AllCops:
2
2
  TargetRubyVersion: 2.6
3
+ NewCops: enable
3
4
 
4
5
  Style/StringLiterals:
5
6
  Enabled: true
@@ -11,3 +12,12 @@ Style/StringLiteralsInInterpolation:
11
12
 
12
13
  Layout/LineLength:
13
14
  Max: 120
15
+
16
+ Style/Documentation:
17
+ Enabled: false
18
+
19
+ Style/TrailingCommaInHashLiteral:
20
+ EnforcedStyleForMultiline: comma
21
+
22
+ Layout/MultilineAssignmentLayout:
23
+ EnforcedStyle: same_line
data/Gemfile CHANGED
@@ -10,3 +10,5 @@ gem "rake", "~> 13.0"
10
10
  gem "minitest", "~> 5.0"
11
11
 
12
12
  gem "rubocop", "~> 1.21"
13
+ gem "rubocop-minitest", "~> 0.17"
14
+ gem "rubocop-rake", "~> 0.6"
data/Gemfile.lock CHANGED
@@ -1,13 +1,15 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- light_me_up (0.1.0)
4
+ light_me_up (0.2.0)
5
+ optimist (~> 3.0)
5
6
 
6
7
  GEM
7
8
  remote: https://rubygems.org/
8
9
  specs:
9
10
  ast (2.4.2)
10
11
  minitest (5.15.0)
12
+ optimist (3.0.1)
11
13
  parallel (1.21.0)
12
14
  parser (3.0.3.2)
13
15
  ast (~> 2.4.1)
@@ -26,6 +28,10 @@ GEM
26
28
  unicode-display_width (>= 1.4.0, < 3.0)
27
29
  rubocop-ast (1.15.0)
28
30
  parser (>= 3.0.1.1)
31
+ rubocop-minitest (0.17.0)
32
+ rubocop (>= 0.90, < 2.0)
33
+ rubocop-rake (0.6.0)
34
+ rubocop (~> 1.0)
29
35
  ruby-progressbar (1.11.0)
30
36
  unicode-display_width (2.1.0)
31
37
 
@@ -37,6 +43,8 @@ DEPENDENCIES
37
43
  minitest (~> 5.0)
38
44
  rake (~> 13.0)
39
45
  rubocop (~> 1.21)
46
+ rubocop-minitest (~> 0.17)
47
+ rubocop-rake (~> 0.6)
40
48
 
41
49
  BUNDLED WITH
42
50
  2.2.32
data/bin/light-me-up CHANGED
@@ -1,19 +1,50 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require_relative "../lib/light_me_up"
5
+ require "optimist"
4
6
 
5
- parser = LightMeUp::OptionParser.new(
6
- program_name: File.basename(__FILE__),
7
- starting_options: {
8
- ip_address: ENV["ELGATO_IP_ADDRESS"],
9
- },
10
- default_settings: {
11
- brightness: ENV["ELGATO_DEFAULT_BRIGHTNESS"].to_i,
12
- temperature: ENV["ELGATO_DEFAULT_TEMPERATURE"].to_i,
13
- }
7
+ brightness_help = format(
8
+ "Set the brightness (%<min>d to %<max>d)",
9
+ min: LightMeUp::Light.min_brightness,
10
+ max: LightMeUp::Light.max_brightness
11
+ )
12
+ temperature_help = format(
13
+ "Set the temperature (%<min>d to %<max>d)",
14
+ min: LightMeUp::Light.min_temperature,
15
+ max: LightMeUp::Light.max_temperature
14
16
  )
15
17
 
16
- options = parser.parse(ARGV)
18
+ options = Optimist.options do
19
+ banner "Usage: #{$PROGRAM_NAME} [options]"
20
+
21
+ opt :toggle, "Toggle whether the lights are on or off", short: "T"
22
+ opt :on, "Turn the light on"
23
+ opt :off, "Turn the light off", short: "O"
24
+ opt :ip_address, "Specify the IP address", type: :string, default: ENV["ELGATO_IP_ADDRESS"]
25
+ opt :defaults, "Set to default values"
26
+ opt :brightness, brightness_help, type: :integer
27
+ opt :temperature, temperature_help, type: :integer
28
+ end
29
+
30
+ options = options.select { |_k, v| v }
31
+ options[:on] = false if options.delete(:off)
32
+ if options.delete(:defaults)
33
+ options.reverse_merge!(
34
+ brightness: ENV["ELGATO_DEFAULT_BRIGHTNESS"].to_i,
35
+ temperature: ENV["ELGATO_DEFAULT_TEMPERATURE"].to_i
36
+ )
37
+ end
38
+
17
39
  api_client = LightMeUp::ApiClient.new(ip_address: options[:ip_address])
18
- success = LightMeUp::SettingsUpdate.new(api_client, options, parser.help_message).perform
19
- exit 1 unless success
40
+
41
+ begin
42
+ LightMeUp::SettingsUpdate.new(api_client, options).perform
43
+ rescue LightMeUp::SettingsUpdate::ToggleIncompatible => e
44
+ Optimist.die :toggle, e.message
45
+ rescue LightMeUp::NoOptionsGiven
46
+ Optimist.educate
47
+ rescue LightMeUp::Error => e
48
+ warn e.message
49
+ exit 1
50
+ end
@@ -1,8 +1,10 @@
1
- require 'net/http'
2
- require 'json'
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "json"
3
5
 
4
6
  module LightMeUp
5
- class ApiClient
7
+ class ApiClient # rubocop:disable Metrics/ClassLength
6
8
  DEFAULT_PORT = 9123
7
9
  LIGHTS_PATH = "/elgato/lights"
8
10
  OPEN_TIMEOUT = 2 # seconds
@@ -10,6 +12,7 @@ module LightMeUp
10
12
 
11
13
  def initialize(ip_address:, port: DEFAULT_PORT)
12
14
  raise Error, "No ip_address specified." unless ip_address && ip_address != ""
15
+
13
16
  @ip_address = ip_address
14
17
  @port = port
15
18
  end
@@ -20,7 +23,7 @@ module LightMeUp
20
23
  end
21
24
 
22
25
  def toggle
23
- with_connection do |http|
26
+ with_connection do |_http|
24
27
  current_status = status
25
28
 
26
29
  if current_status.on
@@ -31,24 +34,24 @@ module LightMeUp
31
34
  end
32
35
  end
33
36
 
34
- def set(on: nil, brightness: nil, temperature: nil)
37
+ def update(on: nil, brightness: nil, temperature: nil)
35
38
  update_light(Light.new(on: on, brightness: brightness, temperature: temperature))
36
39
  end
37
40
 
38
41
  def turn_light_on
39
- set(on: true)
42
+ update(on: true)
40
43
  end
41
44
 
42
45
  def turn_light_off
43
- set(on: false)
46
+ update(on: false)
44
47
  end
45
48
 
46
- def set_brightness(brightness)
47
- set(brightness: brightness)
49
+ def update_brightness(brightness)
50
+ update(brightness: brightness)
48
51
  end
49
52
 
50
- def set_temperature(temperature)
51
- set(temperature: temperature)
53
+ def update_temperature(temperature)
54
+ update(temperature: temperature)
52
55
  end
53
56
 
54
57
  private
@@ -67,7 +70,7 @@ module LightMeUp
67
70
  def build_light_configuration_data(lights)
68
71
  {
69
72
  numberOfLights: lights.size,
70
- lights: lights.map { |l| LightSerializer.serialize(l) }
73
+ lights: lights.map { |l| LightSerializer.serialize(l) },
71
74
  }
72
75
  end
73
76
 
@@ -108,7 +111,7 @@ module LightMeUp
108
111
 
109
112
  def perform_request_with_connection(method, uri, &block)
110
113
  request = build_request(method, uri)
111
- request['Content-Type'] = "application/json"
114
+ request["Content-Type"] = "application/json"
112
115
  block.call(request) if block_given?
113
116
  response = connection.request(request)
114
117
 
@@ -118,7 +121,6 @@ module LightMeUp
118
121
  else
119
122
  raise Error, response.body
120
123
  end
121
-
122
124
  end
123
125
 
124
126
  def build_request(method, uri)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module LightMeUp
2
4
  class Error < StandardError
3
5
  end
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module LightMeUp
2
4
  class Light
3
5
  attr_reader :on, :brightness, :temperature
4
6
 
5
- BRIGHTNESS_RANGE = (0..100)
6
- TEMPERATURE_RANGE = (143..344)
7
+ BRIGHTNESS_RANGE = (0..100).freeze
8
+ TEMPERATURE_RANGE = (143..344).freeze
7
9
 
8
10
  class << self
9
11
  def max_brightness
@@ -45,6 +47,7 @@ module LightMeUp
45
47
  def validate_range(value, range, name)
46
48
  return if value.nil?
47
49
  return if range.include?(value)
50
+
48
51
  raise Error, "#{name} must be between #{range.first} and #{range.last}"
49
52
  end
50
53
  end
@@ -1,12 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module LightMeUp
2
4
  class LightSerializer
3
5
  class << self
4
6
  def serialize(light)
5
7
  on_as_int = if light.on.nil?
6
- nil
7
- else
8
- light.on ? 1 : 0
9
- end
8
+ nil
9
+ else
10
+ light.on ? 1 : 0
11
+ end
10
12
 
11
13
  {
12
14
  on: on_as_int,
@@ -20,7 +22,7 @@ module LightMeUp
20
22
  Light.new(
21
23
  on: light["on"] == 1,
22
24
  brightness: light["brightness"],
23
- temperature: light["temperature"],
25
+ temperature: light["temperature"]
24
26
  )
25
27
  end
26
28
  end
@@ -1,42 +1,27 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module LightMeUp
2
4
  class SettingsUpdate
3
- attr_reader :api_client, :options, :help_message, :output_stream, :error_output_stream
5
+ attr_reader :api_client, :options
6
+
7
+ class ToggleIncompatible < Error; end
8
+ class NoOptionsGiven < Error; end
4
9
 
5
- def initialize(api_client, options, help_message, output_stream=STDOUT, error_output_stream=STDERR)
10
+ def initialize(api_client, options)
6
11
  @api_client = api_client
7
12
  @options = options
8
- @help_message = help_message
9
- @output_stream = output_stream
10
- @error_output_stream = error_output_stream
11
13
  end
12
14
 
13
15
  def perform
14
- if options[:help]
15
- puts parser.help_message
16
- return true
17
- end
18
-
19
16
  if options[:toggle]
20
- if settings_options.any?
21
- error_output_stream.puts "Toggle is not compatible with setting other options."
22
- output_stream.puts help_message
23
- return false
24
- end
17
+ raise ToggleIncompatible, "is not compatible with setting other options" if settings_options.any?
25
18
 
26
19
  api_client.toggle
27
- return true
28
20
  end
29
21
 
30
- if settings_options.any?
31
- api_client.set(**settings_options)
32
- return true
33
- end
22
+ api_client.set(**settings_options) if settings_options.any?
34
23
 
35
- output_stream.puts help_message
36
- return false
37
- rescue LightMeUp::Error => e
38
- error_output_stream.puts e.message
39
- return false
24
+ raise NoOptionsGiven, "provide at least one option"
40
25
  end
41
26
 
42
27
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LightMeUp
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/light_me_up.rb CHANGED
@@ -4,6 +4,5 @@ require_relative "light_me_up/version"
4
4
  require_relative "light_me_up/error"
5
5
  require_relative "light_me_up/light"
6
6
  require_relative "light_me_up/light_serializer"
7
- require_relative "light_me_up/option_parser"
8
7
  require_relative "light_me_up/settings_update"
9
8
  require_relative "light_me_up/api_client"
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: light_me_up
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter McCracken
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-06 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2022-01-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: optimist
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '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: '3.0'
13
27
  description: Provides the ability to turn on and off, and adjust brightness and temperature,
14
28
  on a key light.
15
29
  email:
@@ -35,7 +49,6 @@ files:
35
49
  - lib/light_me_up/error.rb
36
50
  - lib/light_me_up/light.rb
37
51
  - lib/light_me_up/light_serializer.rb
38
- - lib/light_me_up/option_parser.rb
39
52
  - lib/light_me_up/settings_update.rb
40
53
  - lib/light_me_up/version.rb
41
54
  homepage: https://github.com/peterjm/light_me_up
@@ -45,6 +58,7 @@ metadata:
45
58
  homepage_uri: https://github.com/peterjm/light_me_up
46
59
  source_code_uri: https://github.com/peterjm/light_me_up
47
60
  changelog_uri: https://github.com/peterjm/light_me_up/CHANGELOG.md
61
+ rubygems_mfa_required: 'true'
48
62
  post_install_message:
49
63
  rdoc_options: []
50
64
  require_paths:
@@ -1,75 +0,0 @@
1
- require 'optparse'
2
-
3
- module LightMeUp
4
- class OptionParser
5
- MESSAGES = {
6
- help: "Prints this help message",
7
- toggle: "Toggle whether the lights are on or off",
8
- on: "Turn the light on",
9
- off: "Turn the light off",
10
- ip_address: "Specify the IP address",
11
- defaults: "Set to default values",
12
- brightness: "Set the brightness (%d to %d)" %
13
- [LightMeUp::Light.min_brightness, LightMeUp::Light.max_brightness],
14
- temperature: "Set the temperature (%d to %d)" %
15
- [LightMeUp::Light.min_temperature, LightMeUp::Light.max_temperature],
16
- }
17
-
18
- attr_reader :program_name, :options, :default_settings
19
-
20
- def initialize(program_name:, starting_options:, default_settings:)
21
- @program_name = program_name
22
- @options = starting_options.dup
23
- @default_settings = default_settings.dup
24
- end
25
-
26
- def parse(input_options)
27
- parser.parse!(input_options)
28
- options
29
- end
30
-
31
- def help_message
32
- parser.to_s
33
- end
34
-
35
- private
36
-
37
- def parser
38
- @parser ||= ::OptionParser.new do |opts|
39
- opts.banner = "Usage: #{program_name} [options]"
40
-
41
- opts.on("-h", "--help", MESSAGES[:help]) do
42
- options[:help] = true
43
- end
44
-
45
- opts.on("-T", "--toggle", MESSAGES[:toggle]) do
46
- options[:toggle] = true
47
- end
48
-
49
- opts.on("-o", "--on", MESSAGES[:on]) do
50
- options[:on] = true
51
- end
52
-
53
- opts.on("-O", "--off", MESSAGES[:off]) do
54
- options[:on] = false
55
- end
56
-
57
- opts.on("-i", "--ip-address=IP_ADDRESS", MESSAGES[:ip_address]) do |ip_address|
58
- options[:ip_address] = ip_address
59
- end
60
-
61
- opts.on("-d", "--defaults", MESSAGES[:defaults]) do
62
- options.merge!(default_settings)
63
- end
64
-
65
- opts.on("-bBRIGHTNESS", "--brightness=BRIGHTNESS", MESSAGES[:brightness]) do |brightness|
66
- options[:brightness] = brightness.to_i
67
- end
68
-
69
- opts.on("-tTEMPERATURE", "--temperature=TEMPERATURE", MESSAGES[:temperature]) do |temperature|
70
- options[:temperature] = temperature.to_i
71
- end
72
- end
73
- end
74
- end
75
- end