From 1f1868d1eefe4f6e96991de352e4f571450236a8 Mon Sep 17 00:00:00 2001 From: Hannes Schaller Date: Mon, 16 Jan 2017 21:53:05 +0100 Subject: [PATCH 1/4] Updated Gemspec for the currently published plugin --- lita-wit.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lita-wit.gemspec b/lita-wit.gemspec index 98361b9..c9eb096 100644 --- a/lita-wit.gemspec +++ b/lita-wit.gemspec @@ -15,7 +15,7 @@ Gem::Specification.new do |spec| spec.require_paths = ['lib'] spec.add_runtime_dependency 'lita', '>= 4.7' - spec.add_runtime_dependency 'wit', '>= 3.3.1' + spec.add_runtime_dependency 'wit', '>= 3.3.1', '< 4.0.0' spec.add_development_dependency 'bundler', '>= 1.3' spec.add_development_dependency 'pry-byebug' From 1e877bf1f82dc30ad0fdbd6dea92f3453cf0ca0d Mon Sep 17 00:00:00 2001 From: Hannes Schaller Date: Mon, 16 Jan 2017 22:03:14 +0100 Subject: [PATCH 2/4] first pass rubocop --- .gitignore | 1 + .rubocop.yml | 30 ++++++++++++++++++++++++++++ Gemfile | 3 ++- Rakefile | 5 +++-- lib/lita-wit.rb | 1 + lib/lita/actions/base.rb | 9 +++++---- lib/lita/actions/weather.rb | 22 ++++++++++---------- lib/lita/handlers/wit.rb | 1 + lib/lita/services/wit_client.rb | 8 ++++---- lib/lita/utils/alias_stripper.rb | 3 ++- lib/lita/utils/aliases.rb | 3 ++- lib/lita/utils/bickle.rb | 2 +- lib/lita/utils/context_piper.rb | 3 ++- lib/lita/utils/entities_navigator.rb | 5 +++-- lita-wit.gemspec | 5 +++-- spec/lita/handlers/wit_spec.rb | 2 +- spec/lita_config.rb | 1 + spec/spec_helper.rb | 3 ++- 18 files changed, 74 insertions(+), 33 deletions(-) create mode 100755 .rubocop.yml diff --git a/.gitignore b/.gitignore index e1d8bf1..572aaf1 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ spec/reports test/tmp test/version_tmp tmp +lita_config.rb diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100755 index 0000000..b622b34 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,30 @@ +# This is the configuration used to check the rubocop source code. + +AllCops: + Exclude: + - 'vendor/**/*' + - 'spec/fixtures/**/*' + - 'tmp/**/*' + TargetRubyVersion: 2.1 + +Style/Encoding: + EnforcedStyle: when_needed + Enabled: true + +Style/Documentation: + Enabled: false + +Style/FileName: + Enabled: false + +Style/FrozenStringLiteralComment: + EnforcedStyle: always + +Metrics/BlockLength: + Exclude: + - 'Rakefile' + - '**/*.rake' + - 'spec/**/*.rb' + +Metrics/LineLength: + Enabled: false diff --git a/Gemfile b/Gemfile index b4e2a20..e5ad85c 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,4 @@ -source "https://rubygems.org" +# frozen_string_literal: true +source 'https://rubygems.org' gemspec diff --git a/Rakefile b/Rakefile index c92b11e..ceaa74c 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,6 @@ -require "bundler/gem_tasks" -require "rspec/core/rake_task" +# frozen_string_literal: true +require 'bundler/gem_tasks' +require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) diff --git a/lib/lita-wit.rb b/lib/lita-wit.rb index e970d66..edaaa7c 100644 --- a/lib/lita-wit.rb +++ b/lib/lita-wit.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require 'lita' require 'wit' diff --git a/lib/lita/actions/base.rb b/lib/lita/actions/base.rb index 59aac10..19e64ef 100644 --- a/lib/lita/actions/base.rb +++ b/lib/lita/actions/base.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Lita module Actions class Base @@ -5,13 +6,13 @@ def initialize(robot) @robot = robot end - def actions(source) + def actions(_source) { - :error => -> (session_id, context, error) { + error: lambda do |session_id, context, error| # Required, but is never ever called - } + end } end end end -end \ No newline at end of file +end diff --git a/lib/lita/actions/weather.rb b/lib/lita/actions/weather.rb index dc718f2..dade64a 100644 --- a/lib/lita/actions/weather.rb +++ b/lib/lita/actions/weather.rb @@ -1,19 +1,17 @@ +# frozen_string_literal: true module Lita module Actions class Weather < Base - def actions(source) - super.merge({ - :say => -> (session_id, context, msg) { - @robot.send_message(source, msg) - }, - :merge => -> (session_id, context, entities, msg) { - Utils::ContextPiper.pipe(context, entities, 'intent', 'intent') - Utils::ContextPiper.pipe(context, entities, 'location', 'loc') - context - } - }) + super.merge(say: lambda do |_session_id, _context, msg| + @robot.send_message(source, msg) + end, + merge: lambda do |_session_id, context, entities, _msg| + Utils::ContextPiper.pipe(context, entities, 'intent', 'intent') + Utils::ContextPiper.pipe(context, entities, 'location', 'loc') + context + end) end end end -end \ No newline at end of file +end diff --git a/lib/lita/handlers/wit.rb b/lib/lita/handlers/wit.rb index 048a56a..9dac2a1 100644 --- a/lib/lita/handlers/wit.rb +++ b/lib/lita/handlers/wit.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Lita module Handlers class Wit < Handler diff --git a/lib/lita/services/wit_client.rb b/lib/lita/services/wit_client.rb index 79a525d..6706ac3 100644 --- a/lib/lita/services/wit_client.rb +++ b/lib/lita/services/wit_client.rb @@ -1,19 +1,19 @@ +# frozen_string_literal: true module Lita module Services class WitClient - def initialize(robot) @robot = robot @token = robot.config.handlers.wit.server_access_token @actions_class = robot.config.handlers.wit.actions_class end - def run_actions(session_id, message, context={}, max_steps=DEFAULT_MAX_STEPS) + def run_actions(session_id, message, context = {}, max_steps = DEFAULT_MAX_STEPS) actions = @actions_class.new(@robot).actions(message.source) - @wit = ::Wit.new(@token, actions) + @wit = ::Wit.new(access_token: @token, actions: actions) stripped = Utils::AliasStripper.strip(@robot, message) @wit.run_actions(session_id, stripped.body, context, max_steps) end end end -end \ No newline at end of file +end diff --git a/lib/lita/utils/alias_stripper.rb b/lib/lita/utils/alias_stripper.rb index b978527..6dc2dbe 100644 --- a/lib/lita/utils/alias_stripper.rb +++ b/lib/lita/utils/alias_stripper.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Lita module Utils class AliasStripper @@ -7,4 +8,4 @@ def self.strip(robot, message) end end end -end \ No newline at end of file +end diff --git a/lib/lita/utils/aliases.rb b/lib/lita/utils/aliases.rb index bc0bc93..090c7b4 100644 --- a/lib/lita/utils/aliases.rb +++ b/lib/lita/utils/aliases.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Lita module Utils class Aliases @@ -6,4 +7,4 @@ def self.values(robot) end end end -end \ No newline at end of file +end diff --git a/lib/lita/utils/bickle.rb b/lib/lita/utils/bickle.rb index 0f6a2a5..e754b53 100644 --- a/lib/lita/utils/bickle.rb +++ b/lib/lita/utils/bickle.rb @@ -1,7 +1,7 @@ +# frozen_string_literal: true module Lita module Utils class Bickle - def initialize(robot) @robot = robot end diff --git a/lib/lita/utils/context_piper.rb b/lib/lita/utils/context_piper.rb index 8495205..62061c7 100644 --- a/lib/lita/utils/context_piper.rb +++ b/lib/lita/utils/context_piper.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true module Lita module Utils class ContextPiper @@ -7,4 +8,4 @@ def self.pipe(context, entities, input, output) end end end -end \ No newline at end of file +end diff --git a/lib/lita/utils/entities_navigator.rb b/lib/lita/utils/entities_navigator.rb index ebf6e50..871552c 100644 --- a/lib/lita/utils/entities_navigator.rb +++ b/lib/lita/utils/entities_navigator.rb @@ -1,12 +1,13 @@ +# frozen_string_literal: true module Lita module Utils class EntitiesNavigator def self.first_value(entities, entity) - return nil unless entities.has_key? entity + return nil unless entities.key? entity val = entities[entity][0]['value'] return nil if val.nil? val.is_a?(Hash) ? val['value'] : val end end end -end \ No newline at end of file +end diff --git a/lita-wit.gemspec b/lita-wit.gemspec index c9eb096..6fbfdb2 100644 --- a/lita-wit.gemspec +++ b/lita-wit.gemspec @@ -1,3 +1,4 @@ +# frozen_string_literal: true Gem::Specification.new do |spec| spec.name = 'lita-wit' spec.version = '0.1.2' @@ -7,9 +8,9 @@ Gem::Specification.new do |spec| spec.summary = 'Receive structured intentions from unstructured sentences using a Lita bot and Wit.' spec.homepage = 'https://github.com/dbastin/lita-wit' spec.license = 'MIT' - spec.metadata = { 'lita_plugin_type' => 'handler'} + spec.metadata = { 'lita_plugin_type' => 'handler' } - spec.files = `git ls-files`.split($/) + spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR) 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'] diff --git a/spec/lita/handlers/wit_spec.rb b/spec/lita/handlers/wit_spec.rb index b326312..8cdd65e 100644 --- a/spec/lita/handlers/wit_spec.rb +++ b/spec/lita/handlers/wit_spec.rb @@ -1,7 +1,7 @@ +# frozen_string_literal: true require 'spec_helper' describe Lita::Handlers::Wit, lita_handler: true do - describe 'handling unhandled_message' do let(:source) { double(:source) } diff --git a/spec/lita_config.rb b/spec/lita_config.rb index f5a29a4..7bcdf55 100644 --- a/spec/lita_config.rb +++ b/spec/lita_config.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true Lita.configure do |config| config.robot.name = 'Lita' config.robot.log_level = :info diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 154a1f0..cd2afa2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true require 'simplecov' require 'coveralls' @@ -32,4 +33,4 @@ config.configure_rspec_metadata! end -SESSION_ID = 'unique-1234' +SESSION_ID = 'unique-1234'.freeze From 66359b06cbcbf97aac9932af2d1d4cd3abffc168 Mon Sep 17 00:00:00 2001 From: Hannes Schaller Date: Mon, 16 Jan 2017 22:37:19 +0100 Subject: [PATCH 3/4] update to version 4.1 --- lib/lita/actions/base.rb | 2 +- lib/lita/actions/weather.rb | 14 +++++++------- lita-wit.gemspec | 2 +- spec/spec_helper.rb | 1 - 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/lita/actions/base.rb b/lib/lita/actions/base.rb index 19e64ef..af875ef 100644 --- a/lib/lita/actions/base.rb +++ b/lib/lita/actions/base.rb @@ -8,7 +8,7 @@ def initialize(robot) def actions(_source) { - error: lambda do |session_id, context, error| + error: lambda do |request| # Required, but is never ever called end } diff --git a/lib/lita/actions/weather.rb b/lib/lita/actions/weather.rb index dade64a..a6dbd8b 100644 --- a/lib/lita/actions/weather.rb +++ b/lib/lita/actions/weather.rb @@ -3,13 +3,13 @@ module Lita module Actions class Weather < Base def actions(source) - super.merge(say: lambda do |_session_id, _context, msg| - @robot.send_message(source, msg) - end, - merge: lambda do |_session_id, context, entities, _msg| - Utils::ContextPiper.pipe(context, entities, 'intent', 'intent') - Utils::ContextPiper.pipe(context, entities, 'location', 'loc') - context + super.merge(send: lambda do |_context, msg| + @robot.send_message(source, msg['text']) + end, + merge: lambda do |r| + Utils::ContextPiper.pipe(r['context'], r['entities'], 'intent', 'intent') + Utils::ContextPiper.pipe(r['context'], r['entities'], 'location', 'loc') + r['context'] end) end end diff --git a/lita-wit.gemspec b/lita-wit.gemspec index 6fbfdb2..2d9db87 100644 --- a/lita-wit.gemspec +++ b/lita-wit.gemspec @@ -16,7 +16,7 @@ Gem::Specification.new do |spec| spec.require_paths = ['lib'] spec.add_runtime_dependency 'lita', '>= 4.7' - spec.add_runtime_dependency 'wit', '>= 3.3.1', '< 4.0.0' + spec.add_runtime_dependency 'wit', '>= 4.0.0', '< 5.0.0' spec.add_development_dependency 'bundler', '>= 1.3' spec.add_development_dependency 'pry-byebug' diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index cd2afa2..2c7400a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -14,7 +14,6 @@ end require 'lita-wit' -Wit.logger.level = Logger::WARN require 'lita/rspec' require 'lita_config' From dd0e3446eab1a214838b6ac910b1b5611affeaab Mon Sep 17 00:00:00 2001 From: Hannes Schaller Date: Mon, 16 Jan 2017 23:29:45 +0100 Subject: [PATCH 4/4] fix frozen_string_literal error on ruby 2.3 --- spec/lita/handlers/wit_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/lita/handlers/wit_spec.rb b/spec/lita/handlers/wit_spec.rb index 8cdd65e..4f70088 100644 --- a/spec/lita/handlers/wit_spec.rb +++ b/spec/lita/handlers/wit_spec.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: true +# frozen_string_literal: false require 'spec_helper' describe Lita::Handlers::Wit, lita_handler: true do