diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index de8334dd..ee33d6b5 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -2,13 +2,28 @@ name: Tests on: [push] jobs: test: - name: Test Swift Package + name: Test runs-on: ubuntu-latest timeout-minutes: 5 steps: - - uses: actions/checkout@v1 - - uses: actions/setup-ruby@v1 - - name: 'Install Dependencies' - run: sudo apt-get install libcurl4-openssl-dev && gem install bundler && bundle install - - name: 'Test' + - name: Checkout + uses: actions/checkout@v4 + - name: Setup Ruby + uses: ruby/setup-ruby@v1 + with: + bundler-cache: true + - name: Test run: bundle exec rake test + lint: + name: Lint + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Setup Ruby + uses: ruby/setup-ruby@v1 + with: + bundler-cache: true + - name: Test + run: bundle exec rake standard diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 00000000..be94e6f5 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +3.2.2 diff --git a/Gemfile b/Gemfile index d296794b..19f01f5f 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,8 @@ -source 'https://rubygems.org' +source "https://rubygems.org" gemspec -gem 'rake' +gem "rake", "~> 13.0" +gem "minitest", "~> 5.0" +gem "standard", "~> 1.3" +gem "webmock", "~> 3.19" diff --git a/Rakefile b/Rakefile index 121155ca..e41a92a6 100644 --- a/Rakefile +++ b/Rakefile @@ -1,10 +1,12 @@ -require 'bundler/gem_tasks' -require 'rake/testtask' +require "bundler/gem_tasks" +require "rake/testtask" Rake::TestTask.new(:test) do |t| - t.libs << 'test' - t.libs << 'lib' - t.test_files = FileList['test/**/*_test.rb'] + t.libs << "test" + t.libs << "lib" + t.test_files = FileList["test/**/*_test.rb"] end -task :default => :test +require "standard/rake" + +task default: :test diff --git a/hue.gemspec b/hue.gemspec index f115a20e..c1122b34 100644 --- a/hue.gemspec +++ b/hue.gemspec @@ -1,27 +1,23 @@ -# coding: utf-8 -lib = File.expand_path('../lib', __FILE__) +lib = File.expand_path("../lib", __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'hue/version' +require "hue/version" Gem::Specification.new do |spec| - spec.name = 'hue' - spec.version = Hue::VERSION - spec.authors = ['Sam Soffes'] - spec.email = ['sam@soff.es'] - spec.description = 'Work with Philips Hue light bulbs.' - spec.summary = 'Work with Philips Hue light bulbs from Ruby.' - spec.homepage = 'https://github.com/soffes/hue' - spec.license = 'MIT' + spec.name = "hue" + spec.version = Hue::VERSION + spec.authors = ["Sam Soffes"] + spec.email = ["sam@soff.es"] + spec.description = "Work with Philips Hue light bulbs." + spec.summary = "Work with Philips Hue light bulbs from Ruby." + spec.homepage = "https://github.com/soffes/hue" + spec.license = "MIT" - spec.files = `git ls-files`.split($/) - spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } - spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) - spec.require_paths = ['lib'] + spec.files = `git ls-files`.split($/) + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.require_paths = ["lib"] - spec.required_ruby_version = '>= 2.1.0' - spec.add_dependency 'thor' - spec.add_dependency 'json' - spec.add_dependency 'color_conversion' - spec.add_development_dependency 'minitest', '~> 5.0' - spec.add_development_dependency 'webmock' + spec.required_ruby_version = ">= 2.1.0" + spec.add_dependency "thor" + spec.add_dependency "json" + spec.add_dependency "color_conversion" end diff --git a/lib/hue.rb b/lib/hue.rb index aaf55cf1..8aab388e 100644 --- a/lib/hue.rb +++ b/lib/hue.rb @@ -1,12 +1,12 @@ -require 'hue/version' -require 'hue/errors' -require 'hue/client' -require 'hue/bridge' -require 'hue/editable_state' -require 'hue/translate_keys' -require 'hue/light' -require 'hue/group' -require 'hue/scene' +require "hue/version" +require "hue/errors" +require "hue/client" +require "hue/bridge" +require "hue/editable_state" +require "hue/translate_keys" +require "hue/light" +require "hue/group" +require "hue/scene" module Hue USERNAME_RANGE = 10..40 diff --git a/lib/hue/bridge.rb b/lib/hue/bridge.rb index 5a7ef780..85fcd4ea 100644 --- a/lib/hue/bridge.rb +++ b/lib/hue/bridge.rb @@ -46,21 +46,21 @@ def initialize(client, hash) # Current time stored on the bridge. def utc json = get_configuration - DateTime.parse(json['utc']) + DateTime.parse(json["utc"]) end # Indicates whether the link button has been pressed within the last 30 # seconds. def link_button_pressed? json = get_configuration - json['linkbutton'] + json["linkbutton"] end # This indicates whether the bridge is registered to synchronize data with a # portal account. def has_portal_services? json = get_configuration - json['portalservices'] + json["portalservices"] end def refresh @@ -74,7 +74,7 @@ def refresh def lights @lights ||= begin json = JSON(Net::HTTP.get(URI.parse(base_url))) - json['lights'].map do |key, value| + json["lights"].map do |key, value| Light.new(@client, self, key, value) end end @@ -84,7 +84,7 @@ def add_lights uri = URI.parse("#{base_url}/lights") http = Net::HTTP.new(uri.host) response = http.request_post(uri.path, nil) - (response.body).first + response.body.first end def groups @@ -105,27 +105,27 @@ def scenes end end - private + private KEYS_MAP = { - :id => :id, - :ip => :internalipaddress, - :name => :name, - :proxy_port => :proxyport, - :software_update => :swupdate, - :ip_whitelist => :whitelist, - :software_version => :swversion, - :proxy_address => :proxyaddress, - :mac_address => :macaddress, - :network_mask => :netmask, - :portal_services => :portalservices, + id: :id, + ip: :internalipaddress, + name: :name, + proxy_port: :proxyport, + software_update: :swupdate, + ip_whitelist: :whitelist, + software_version: :swversion, + proxy_address: :proxyaddress, + mac_address: :macaddress, + network_mask: :netmask, + portal_services: :portalservices } def unpack(hash) KEYS_MAP.each do |local_key, remote_key| value = hash[remote_key.to_s] next unless value - instance_variable_set("@#{local_key}", value) + instance_variable_set(:"@#{local_key}", value) end end diff --git a/lib/hue/cli.rb b/lib/hue/cli.rb index 2169b15b..91c8c5e1 100644 --- a/lib/hue/cli.rb +++ b/lib/hue/cli.rb @@ -1,23 +1,23 @@ -require 'thor' +require "thor" module Hue class Cli < Thor - desc 'lights', 'Find all of the lights on your network' + desc "lights", "Find all of the lights on your network" def lights client.lights.each do |light| puts light.id.to_s.ljust(6) + light.name end end - desc 'add LIGHTS', 'Search for new lights' + desc "add LIGHTS", "Search for new lights" def add(thing) case thing - when 'lights' + when "lights" client.add_lights end end - desc 'light ID STATE [COLOR]', 'Access a light' + desc "light ID STATE [COLOR]", "Access a light" long_desc <<-LONGDESC Examples: \n hue all on \n @@ -27,20 +27,20 @@ def add(thing) hue all --hue 50000 --brightness 200 --saturation 240 \n hue all --alert lselect \n LONGDESC - option :hue, :type => :numeric - option :sat, :type => :numeric, :aliases => '--saturation' - option :bri, :type => :numeric, :aliases => '--brightness' - option :alert, :type => :string - desc 'all STATE', 'Send commands to all lights' - def all(state = 'on') + option :hue, type: :numeric + option :sat, type: :numeric, aliases: "--saturation" + option :bri, type: :numeric, aliases: "--brightness" + option :alert, type: :string + desc "all STATE", "Send commands to all lights" + def all(state = "on") body = options.dup - body[:on] = state == 'on' + body[:on] = state == "on" client.lights.each do |light| puts light.set_state body end end - desc 'light ID STATE [COLOR]', 'Access a light' + desc "light ID STATE [COLOR]", "Access a light" long_desc <<-LONGDESC Examples: \n hue light 1 on --hue 12345 \n @@ -48,20 +48,20 @@ def all(state = 'on') hue light 1 --alert lselect \n hue light 1 off LONGDESC - option :hue, :type => :numeric - option :sat, :type => :numeric, :aliases => '--saturation' - option :bri, :type => :numeric, :aliases => '--brightness' - option :alert, :type => :string + option :hue, type: :numeric + option :sat, type: :numeric, aliases: "--saturation" + option :bri, type: :numeric, aliases: "--brightness" + option :alert, type: :string def light(id, state = nil) light = client.light(id) puts light.name body = options.dup - body[:on] = (state == 'on' || !(state == 'off')) + body[:on] = (state == "on" || !(state == "off")) puts light.set_state(body) if body.length > 0 end - desc 'groups', 'Find all light groups on your network' + desc "groups", "Find all light groups on your network" def groups client.groups.each do |group| puts group.id.to_s.ljust(6) + group.name @@ -71,7 +71,7 @@ def groups end end - desc 'group ID STATE [COLOR]', 'Update a group of lights' + desc "group ID STATE [COLOR]", "Update a group of lights" long_desc <<-LONGDESC Examples: \n hue groups 1 on --hue 12345 @@ -79,20 +79,20 @@ def groups hue groups 1 --alert lselect hue groups 1 off LONGDESC - option :hue, :type => :numeric - option :sat, :type => :numeric, :aliases => '--saturation' - option :bri, :type => :numeric, :aliases => '--brightness' - option :alert, :type => :string + option :hue, type: :numeric + option :sat, type: :numeric, aliases: "--saturation" + option :bri, type: :numeric, aliases: "--brightness" + option :alert, type: :string def group(id, state = nil) group = client.group(id) puts group.name body = options.dup - body[:on] = (state == 'on' || !(state == 'off')) + body[:on] = (state == "on" || !(state == "off")) puts group.set_state(body) if body.length > 0 end - private + private def client @client ||= Hue::Client.new diff --git a/lib/hue/client.rb b/lib/hue/client.rb index 32c72f0c..4b656a04 100644 --- a/lib/hue/client.rb +++ b/lib/hue/client.rb @@ -1,6 +1,6 @@ -require 'net/http' -require 'json' -require 'resolv' +require "net/http" +require "json" +require "resolv" module Hue class Client @@ -23,11 +23,11 @@ def initialize(username = nil, use_mdns: true) end def bridge - @bridge_id = find_bridge_id unless @bridge_id - if @bridge_id - bridge = bridges.select { |b| b.id == @bridge_id }.first + @bridge_id ||= find_bridge_id + bridge = if @bridge_id + bridges.find { |b| b.id == @bridge_id } else - bridge = bridges.first + bridges.first end raise NoBridgeFound unless bridge bridge @@ -52,7 +52,7 @@ def add_lights def light(id) id = id.to_s - lights.select { |l| l.id == id }.first + lights.find { |l| l.id == id } end def groups @@ -63,7 +63,7 @@ def group(id = nil) return Group.new(self, bridge) if id.nil? id = id.to_s - groups.select { |g| g.id == id }.first + groups.find { |g| g.id == id } end def scenes @@ -72,18 +72,18 @@ def scenes def scene(id) id = id.to_s - scenes.select { |s| s.id == id }.first + scenes.find { |s| s.id == id } end private def find_username - return ENV['HUE_USERNAME'] if ENV['HUE_USERNAME'] + return ENV["HUE_USERNAME"] if ENV["HUE_USERNAME"] - json = JSON(File.read(File.expand_path('~/.hue'))) - json['username'] + json = JSON(File.read(File.expand_path("~/.hue"))) + json["username"] rescue - return nil + nil end def validate_user @@ -93,38 +93,38 @@ def validate_user response = response.first end - if error = response['error'] + if (error = response["error"]) raise get_error(error) end - response['success'] + response["success"] end def register_user body = JSON.dump({ - devicetype: 'Ruby' + devicetype: "Ruby" }) uri = URI.parse("http://#{bridge.ip}/api") http = Net::HTTP.new(uri.host) response = JSON(http.request_post(uri.path, body).body).first - if error = response['error'] + if (error = response["error"]) raise get_error(error) end - if @username = response['success']['username'] - File.write(File.expand_path('~/.hue'), JSON.dump({username: @username})) + if (@username = response["success"]["username"]) + File.write(File.expand_path("~/.hue"), JSON.dump({username: @username})) end end def find_bridge_id - return ENV['HUE_BRIDGE_ID'] if ENV['HUE_BRIDGE_ID'] + return ENV["HUE_BRIDGE_ID"] if ENV["HUE_BRIDGE_ID"] - json = JSON(File.read(File.expand_path('~/.hue'))) - json['bridge_id'] + json = JSON(File.read(File.expand_path("~/.hue"))) + json["bridge_id"] rescue - return nil + nil end def discovery_mdns(bs) @@ -135,8 +135,8 @@ def discovery_mdns(bs) bridge_target = resolver.getresource(bridge_ptr.name, Resolv::DNS::Resource::IN::SRV).target bridge_hash = { - 'id' => resolver.getresource(bridge_ptr.name, Resolv::DNS::Resource::IN::TXT).strings[0].split('=')[1], - 'internalipaddress' => resolver.getresource(bridge_target, Resolv::DNS::Resource::IN::A).address + "id" => resolver.getresource(bridge_ptr.name, Resolv::DNS::Resource::IN::TXT).strings[0].split("=")[1], + "internalipaddress" => resolver.getresource(bridge_target, Resolv::DNS::Resource::IN::A).address } bs << Bridge.new(self, bridge_hash) @@ -144,9 +144,9 @@ def discovery_mdns(bs) end def discovery_meethue(bs) - uri = URI('https://discovery.meethue.com/') + uri = URI("https://discovery.meethue.com/") response = Net::HTTP.get(uri) - + JSON(response).each do |hash| bs << Bridge.new(self, hash) end @@ -154,8 +154,8 @@ def discovery_meethue(bs) def get_error(error) # Find error class and return instance - klass = Hue::ERROR_MAP[error['type']] || UnknownError unless klass - klass.new(error['description']) + klass ||= Hue::ERROR_MAP[error["type"]] || UnknownError + klass.new(error["description"]) end end end diff --git a/lib/hue/editable_state.rb b/lib/hue/editable_state.rb index c6bc05e1..a8225884 100644 --- a/lib/hue/editable_state.rb +++ b/lib/hue/editable_state.rb @@ -1,4 +1,4 @@ -require 'color_conversion' +require "color_conversion" module Hue module EditableState @@ -21,21 +21,21 @@ def off! # Turn the light on if it's off and vice versa def toggle! if @on - self.off! + off! else - self.on! + on! end end - %w{on hue saturation brightness color_temperature alert effect}.each do |key| - define_method "#{key}=".to_sym do |value| + %w[on hue saturation brightness color_temperature alert effect].each do |key| + define_method :"#{key}=" do |value| set_state({key.to_sym => value}) - instance_variable_set("@#{key}".to_sym, value) + instance_variable_set(:"@#{key}", value) end end def set_xy(x, y) - set_state({:xy => [x, y]}) + set_state({xy: [x, y]}) @x, @y = x, y end @@ -44,7 +44,7 @@ def hex end def hex=(hex) - hex = "##{hex}" unless hex.start_with?('#') + hex = "##{hex}" unless hex.start_with?("#") hsb = ColorConversion::Color.new(hex).hsb # Map values from standard HSB to what Hue wants and update state diff --git a/lib/hue/errors.rb b/lib/hue/errors.rb index b9231822..af85e7c9 100644 --- a/lib/hue/errors.rb +++ b/lib/hue/errors.rb @@ -2,33 +2,47 @@ module Hue class Error < StandardError; end class UnauthorizedUser < Error; end + class InvalidJSON < Error; end + class ResourceNotAvailable < Error; end + class MethodNotAvailable < Error; end + class MissingBody < Error; end + class ParameterNotAvailable < Error; end + class InvalidValueForParameter < Error; end + class ParameterNotModifiable < Error; end + class InternalError < Error; end + class LinkButtonNotPressed < Error; end + class ParameterNotModifiableWhileOff < ParameterNotModifiable; end + class TooManyGroups < Error; end + class GroupTooFull < Error; end class InvalidUsername < Error; end + class UnknownError < Error; end + class NoBridgeFound < Error; end # Status code to exception map ERROR_MAP = { - 1 => Hue::UnauthorizedUser, - 2 => Hue::InvalidJSON, - 3 => Hue::ResourceNotAvailable, - 4 => Hue::MethodNotAvailable, - 5 => Hue::MissingBody, - 6 => Hue::ParameterNotAvailable, - 7 => Hue::InvalidValueForParameter, - 8 => Hue::ParameterNotModifiable, + 1 => Hue::UnauthorizedUser, + 2 => Hue::InvalidJSON, + 3 => Hue::ResourceNotAvailable, + 4 => Hue::MethodNotAvailable, + 5 => Hue::MissingBody, + 6 => Hue::ParameterNotAvailable, + 7 => Hue::InvalidValueForParameter, + 8 => Hue::ParameterNotModifiable, 901 => Hue::InternalError, 101 => Hue::LinkButtonNotPressed, 201 => Hue::ParameterNotModifiableWhileOff, diff --git a/lib/hue/group.rb b/lib/hue/group.rb index 0ea4dc59..3036fcce 100644 --- a/lib/hue/group.rb +++ b/lib/hue/group.rb @@ -58,16 +58,14 @@ def each(&block) end def lights - @lights ||= begin - @light_ids.map do |light_id| - @client.light(light_id) - end + @lights ||= @light_ids.map do |light_id| + @client.light(light_id) end end def name=(name) - resp = set_group_state({:name => name}) - @name = new? ? name : resp[0]['success']["/groups/#{id}/name"] + resp = set_group_state({name: name}) + @name = new? ? name : resp[0]["success"]["/groups/#{id}/name"] end def lights=(light_ids) @@ -78,17 +76,17 @@ def lights=(light_ids) @light_ids = light_ids.uniq @lights = nil # resets the memoization - set_group_state({:lights => @light_ids}) + set_group_state({lights: @light_ids}) end def scene=(scene) scene_id = scene.is_a?(Scene) ? scene.id : scene - set_group_state({:scene => scene_id}) + set_group_state({scene: scene_id}) end def <<(light_id) @light_ids << light_id - set_group_state({:lights => @light_ids}) + set_group_state({lights: @light_ids}) end alias_method :add_light, :<< @@ -120,8 +118,8 @@ def refresh def create! body = { - :name => @name, - :lights => @light_ids, + name: @name, + lights: @light_ids } uri = URI.parse("http://#{@bridge.ip}/api/#{@client.username}/groups") @@ -129,7 +127,7 @@ def create! response = http.request_post(uri.path, JSON.dump(body)) json = JSON(response.body) - @id = json[0]['success']['id'] + @id = json[0]["success"]["id"] end def destroy! @@ -137,7 +135,7 @@ def destroy! http = Net::HTTP.new(uri.host) response = http.delete(uri.path) json = JSON(response.body) - @id = nil if json[0]['success'] + @id = nil if json[0]["success"] end def new? @@ -147,22 +145,22 @@ def new? private GROUP_KEYS_MAP = { - :name => :name, - :light_ids => :lights, - :type => :type, - :state => :action + name: :name, + light_ids: :lights, + type: :type, + state: :action } STATE_KEYS_MAP = { - :on => :on, - :brightness => :bri, - :hue => :hue, - :saturation => :sat, - :xy => :xy, - :color_temperature => :ct, - :alert => :alert, - :effect => :effect, - :color_mode => :colormode, + on: :on, + brightness: :bri, + hue: :hue, + saturation: :sat, + xy: :xy, + color_temperature: :ct, + alert: :alert, + effect: :effect, + color_mode: :colormode } def unpack(data) @@ -170,7 +168,7 @@ def unpack(data) unless new? unpack_hash(@state, STATE_KEYS_MAP) - @x, @y = @state['xy'] + @x, @y = @state["xy"] end end diff --git a/lib/hue/light.rb b/lib/hue/light.rb index 99bf8384..015a66b2 100644 --- a/lib/hue/light.rb +++ b/lib/hue/light.rb @@ -98,21 +98,21 @@ def initialize(client, bridge, id, hash) end def name=(new_name) - unless (1..32).include?(new_name.length) - raise InvalidValueForParameter, 'name must be between 1 and 32 characters.' + unless (1..32).cover?(new_name.length) + raise InvalidValueForParameter, "name must be between 1 and 32 characters." end body = { - :name => new_name + name: new_name } uri = URI.parse(base_url) http = Net::HTTP.new(uri.host) response = http.request_put(uri.path, JSON.dump(body)) response = JSON(response.body).first - if response['success'] + if response["success"] @name = new_name - # else + # else # TODO: Error end end @@ -121,7 +121,7 @@ def name=(new_name) # always returns true, functionality will be added in a future # patch. def reachable? - @state['reachable'] + @state["reachable"] end # @param transition The duration of the transition from the light’s current @@ -132,7 +132,7 @@ def set_state(attributes, transition = nil) body = translate_keys(attributes, STATE_KEYS_MAP) # Add transition - body.merge!({:transitiontime => transition}) if transition + body[:transitiontime] = transition if transition uri = URI.parse("#{base_url}/state") http = Net::HTTP.new(uri.host) @@ -146,37 +146,37 @@ def refresh unpack(json) end - private + private KEYS_MAP = { - :state => :state, - :type => :type, - :name => :name, - :model => :modelid, - :software_version => :swversion, - :point_symbol => :pointsymbol, - :uid => :uniqueid, - :capabilities => :capabilities, - :config => :config + state: :state, + type: :type, + name: :name, + model: :modelid, + software_version: :swversion, + point_symbol: :pointsymbol, + uid: :uniqueid, + capabilities: :capabilities, + config: :config } STATE_KEYS_MAP = { - :on => :on, - :brightness => :bri, - :hue => :hue, - :saturation => :sat, - :xy => :xy, - :color_temperature => :ct, - :alert => :alert, - :effect => :effect, - :color_mode => :colormode, - :reachable => :reachable, + on: :on, + brightness: :bri, + hue: :hue, + saturation: :sat, + xy: :xy, + color_temperature: :ct, + alert: :alert, + effect: :effect, + color_mode: :colormode, + reachable: :reachable } def unpack(hash) unpack_hash(hash, KEYS_MAP) unpack_hash(@state, STATE_KEYS_MAP) - @x, @y = @state['xy'] + @x, @y = @state["xy"] end def base_url diff --git a/lib/hue/scene.rb b/lib/hue/scene.rb index 65dc98f5..daf10c31 100644 --- a/lib/hue/scene.rb +++ b/lib/hue/scene.rb @@ -24,19 +24,17 @@ def initialize(client, bridge, id, data) end def lights - @lights ||= begin - @light_ids.map do |light_id| - @client.light(light_id) - end + @lights ||= @light_ids.map do |light_id| + @client.light(light_id) end end private SCENE_KEYS_MAP = { - :name => :name, - :light_ids => :lights, - :active => :active, + name: :name, + light_ids: :lights, + active: :active } def unpack(data) diff --git a/lib/hue/translate_keys.rb b/lib/hue/translate_keys.rb index 886375af..027da2e6 100644 --- a/lib/hue/translate_keys.rb +++ b/lib/hue/translate_keys.rb @@ -14,7 +14,7 @@ def unpack_hash(hash, map) map.each do |local_key, remote_key| value = hash[remote_key.to_s] next unless value - instance_variable_set("@#{local_key}", value) + instance_variable_set(:"@#{local_key}", value) end end end diff --git a/lib/hue/version.rb b/lib/hue/version.rb index 809b6090..bf2c407e 100644 --- a/lib/hue/version.rb +++ b/lib/hue/version.rb @@ -1,3 +1,3 @@ module Hue - VERSION = '0.3.0' + VERSION = "0.3.0" end diff --git a/test/hue/client_test.rb b/test/hue/client_test.rb index 678a7255..05df4ee7 100644 --- a/test/hue/client_test.rb +++ b/test/hue/client_test.rb @@ -1,25 +1,26 @@ -require 'test_helper' +require "test_helper" class ClientTest < Minitest::Test def before_setup super - stub_request(:get, "https://discovery.meethue.com/"). - to_return(:body => '[{"id":"ffa57b3b257200065704","internalipaddress":"192.168.0.1"},{"id":"63c2fc01391276a319f9","internalipaddress":"192.168.0.2"}]') + stub_request(:get, "https://discovery.meethue.com/") + .to_return(body: '[{"id":"ffa57b3b257200065704","internalipaddress":"192.168.0.1"},{"id":"63c2fc01391276a319f9","internalipaddress":"192.168.0.2"}]') - stub_request(:get, %r{http://192.168.0.1/api/*}).to_return(:body => '[{"success":true}]') - stub_request(:get, %r{http://192.168.0.2/api/*}).to_return(:body => '[{"success":true}]') + stub_request(:get, %r{http://192.168.0.1/api}).to_return(body: '[{"success":true}]') + stub_request(:get, %r{http://192.168.0.1/api/*}).to_return(body: '[{"success":true}]') + stub_request(:get, %r{http://192.168.0.2/api/*}).to_return(body: '[{"success":true}]') end def test_with_bridge_id client = Hue::Client.new(use_mdns: false) - client.stub :find_bridge_id, '63c2fc01391276a319f9' do - assert_equal '63c2fc01391276a319f9', client.bridge.id + client.stub :find_bridge_id, "63c2fc01391276a319f9" do + assert_equal "63c2fc01391276a319f9", client.bridge.id end end def test_without_bridge_id client = Hue::Client.new(use_mdns: false) - assert_equal 'ffa57b3b257200065704', client.bridge.id + assert_equal "ffa57b3b257200065704", client.bridge.id end end diff --git a/test/hue/light_test.rb b/test/hue/light_test.rb index 7d03b0b6..7910da1d 100644 --- a/test/hue/light_test.rb +++ b/test/hue/light_test.rb @@ -1,23 +1,23 @@ -require 'test_helper' +require "test_helper" class LightTest < Minitest::Test def before_setup super - stub_request(:get, "https://discovery.meethue.com/"). - to_return(:body => '[{"internalipaddress":"localhost"}]') + stub_request(:get, "https://discovery.meethue.com/") + .to_return(body: '[{"internalipaddress":"localhost"}]') - stub_request(:get, %r{http://localhost/api/*}).to_return(:body => '[{"success":true}]') - stub_request(:post, 'http://localhost/api').to_return(:body => '[{"success":{"username":"ruby"}}]') - stub_request(:put, %r{http://localhost/api*}).to_return(:body => '[{}]') + stub_request(:get, %r{http://localhost/api/*}).to_return(body: '[{"success":true}]') + stub_request(:post, "http://localhost/api").to_return(body: '[{"success":{"username":"ruby"}}]') + stub_request(:put, %r{http://localhost/api*}).to_return(body: "[{}]") end - %w{on hue saturation brightness color_temperature alert effect}.each do |attribute| - define_method "test_setting_#{attribute}" do + %w[on hue saturation brightness color_temperature alert effect].each do |attribute| + define_method :"test_setting_#{attribute}" do client = Hue::Client.new(use_mdns: false) light = Hue::Light.new(client, client.bridge, 0, {"state" => {}}) - light.send("#{attribute}=", 24) + light.send(:"#{attribute}=", 24) assert_requested :put, %r{http://localhost/api/.*/lights/0} end end @@ -34,7 +34,7 @@ def test_toggle_while_off def test_toggle_while_on client = Hue::Client.new(use_mdns: false) - light = Hue::Light.new(client, client.bridge, 0, {"state" => {'on' => true}}) + light = Hue::Light.new(client, client.bridge, 0, {"state" => {"on" => true}}) assert_equal true, light.on? light.toggle! diff --git a/test/test_helper.rb b/test/test_helper.rb index 41e290db..e6234d32 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,6 +1,6 @@ -$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) -require 'hue' +$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) +require "hue" -require 'minitest' -require 'webmock/minitest' -require 'minitest/autorun' +require "minitest" +require "webmock/minitest" +require "minitest/autorun"