Skip to content

Commit

Permalink
Use :message_received event to trigger interpret_monitor instead of /./
Browse files Browse the repository at this point in the history
This change removes the catch-all `/./` chat route in favor of using a
`:message_received` event route to trigger `interpret_monitor`.
  • Loading branch information
t33chong committed May 31, 2016
1 parent 9896e08 commit 39102dd
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 34 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Expand Up @@ -3,6 +3,7 @@ rvm:
- 2.0.0
script: bundle exec rake
before_install:
- gem update --system
- gem update bundler
services:
- redis-server
sudo: false
3 changes: 3 additions & 0 deletions Gemfile
@@ -1,3 +1,6 @@
source "https://rubygems.org"

gemspec

# TODO: Remove this once a version supporting the :message_received event trigger has been released
gem 'lita', git: 'https://github.com/PagerDuty/lita.git', branch: 'v4.7.0-improved'
48 changes: 23 additions & 25 deletions lib/lita/handlers/google_translate.rb
Expand Up @@ -3,6 +3,7 @@
module Lita
module Handlers
class GoogleTranslate < Handler
README = "https://github.com/tristaneuan/lita-google-translate#supported-languages"

config :api_key, type: String, required: true
config :default_language, type: String, default: "en"
Expand All @@ -19,43 +20,44 @@ class GoogleTranslate < Handler
:interpret_start, command: true,
help: {t("help.interpret_key") => t("help.interpret_value")}
)
route(/./, :interpret_monitor, command: false)
route(/!interpret/i, :interpret_stop, command: false)
route(
/^languages/i, :languages, command: true,
help: {t("help.languages_key") => t("help.languages_value")}
)

on :message_received, :interpret_monitor

def initialize(robot)
super
@@codes = ToLang::CODEMAP.each_value.map {|v| v.downcase}.to_set
end

def translate(response)
begin
response.reply get_translation(*parse_command(response.match_data))
rescue InvalidLanguage => e
response.reply format_error(e.message)
end
response.reply get_translation(*parse_command(response.match_data))
rescue InvalidLanguage => e
response.reply t("invalid_language", code: e.message, url: README)
end

def interpret_start(response)
begin
from, to, text = parse_command(response.match_data)
redis.set("#{response.user.id}:from", from)
redis.set("#{response.user.id}:to", to)
response.reply get_translation(from, to, text) unless text.nil?
rescue InvalidLanguage => e
response.reply format_error(e.message)
end
from, to, text = parse_command(response.match_data)
redis.set("#{response.user.id}:from", from)
redis.set("#{response.user.id}:to", to)
response.reply get_translation(from, to, text) unless text.nil?
rescue InvalidLanguage => e
response.reply t("invalid_language", code: e.message, url: README)
end

def interpret_monitor(response)
return if response.message.body.include?("!interpret")
to = redis.get("#{response.user.id}:to")
unless to.nil? || response.message.command?
from = redis.get("#{response.user.id}:from")
response.reply get_translation(from, to, response.message.body)
def interpret_monitor(payload)
message = payload[:message]
source = message.source
user = source.user

return if message.body.include?("!interpret")
to = redis.get("#{user.id}:to")
unless to.nil? || message.command?
from = redis.get("#{user.id}:from")
robot.send_messages(source, get_translation(from, to, message.body))
end
end

Expand All @@ -65,7 +67,7 @@ def interpret_stop(response)
end

def languages(response)
response.reply_privately ToLang::CODEMAP.each.map {|k,v| "#{k}: #{v}"}.join("\n")
response.reply t("languages", url: README)
end

private
Expand All @@ -84,10 +86,6 @@ def get_translation(from, to, text)
falsy(from) ? text.translate(to) : text.translate(to, :from => from)
end

def format_error(code)
"'#{code}' is not a valid language code. For available languages, send me the command: languages"
end

def falsy(code)
code.nil? || code.empty?
end
Expand Down
3 changes: 2 additions & 1 deletion lita-google-translate.gemspec
Expand Up @@ -14,7 +14,8 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_runtime_dependency "lita", ">= 4.4"
# TODO: Specify lita version supporting :message_received trigger once there's a release
spec.add_runtime_dependency "lita", ">= 4.7"
spec.add_runtime_dependency "to_lang", "1.0.0"

spec.add_development_dependency "bundler", "~> 1.3"
Expand Down
2 changes: 2 additions & 0 deletions locales/en.yml
Expand Up @@ -2,6 +2,7 @@ en:
lita:
handlers:
google_translate:
invalid_language: "'%{code}' is not a valid language code. A full list of supported languages can be found at %{url}"
help:
translate_key: "translate TEXT"
translate_value: "Translates TEXT to the default language."
Expand All @@ -11,3 +12,4 @@ en:
interpret_value: "Interprets your messages on the fly until you type !interpret. (FROM and TO are optional language codes)"
languages_key: "languages"
languages_value: "Sends a list of language codes supported by lita-google-translate."
languages: "A full list of supported languages can be found at %{url}"
11 changes: 5 additions & 6 deletions spec/lita/handlers/google_translate_spec.rb
Expand Up @@ -16,7 +16,6 @@
it { is_expected.to route_command("t() hola").to(:translate) }
it { is_expected.to route_command("interpret(es:en)").to(:interpret_start) }
it { is_expected.to route_command("interpret(es:en) hola").to(:interpret_start) }
it { is_expected.to route("hola").to(:interpret_monitor) }
it { is_expected.to route("!interpret").to(:interpret_stop) }
it { is_expected.to route_command("languages").to(:languages) }

Expand All @@ -33,7 +32,7 @@
it "translates with TO parameter" do
send_command "translate(EL) hello"
expect(replies.count).to eq 1
expect(replies.first).to match "Γεια σας"
expect(replies.first).to match "Χαίρετε"
end

it "translates with TO and FROM parameters" do
Expand All @@ -51,13 +50,13 @@
it "responds with an error message when an invalid source language is given" do
send_command "translate(asdf:en) hej"
expect(replies.count).to eq 1
expect(replies.first).to match /^'asdf' is not a valid language code\. For available languages, send me the command: languages$/
expect(replies.first).to match /^'asdf' is not a valid language code\. A full list of supported languages can be found at https:\/\/github\.com\/tristaneuan\/lita-google-translate#supported-languages$/
end

it "responds with an error message when an invalid target language is given" do
send_command "translate(es:hjkl) hola"
expect(replies.count).to eq 1
expect(replies.first).to match /^'hjkl' is not a valid language code\. For available languages, send me the command: languages$/
expect(replies.first).to match /^'hjkl' is not a valid language code\. A full list of supported languages can be found at https:\/\/github\.com\/tristaneuan\/lita-google-translate#supported-languages$/
end

end
Expand All @@ -77,7 +76,7 @@
it "responds with an error message when an invalid language is given" do
send_command "interpret(asdf)"
expect(replies.count).to eq 1
expect(replies.first).to match /^'asdf' is not a valid language code\. For available languages, send me the command: languages$/
expect(replies.first).to match /^'asdf' is not a valid language code\. A full list of supported languages can be found at https:\/\/github\.com\/tristaneuan\/lita-google-translate#supported-languages$/
end

after(:each) do
Expand Down Expand Up @@ -129,7 +128,7 @@
it "responds with a list of languages and their codes" do
send_command "languages"
expect(replies.count).to eq 1
expect(replies.first).to match /^afrikaans: af\nalbanian: sq\n/
expect(replies.first).to match /^A full list of supported languages can be found at https:\/\/github\.com\/tristaneuan\/lita-google-translate#supported-languages/
end

end
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
@@ -1,6 +1,6 @@
require "simplecov"
require "coveralls"
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
SimpleCov.formatters = [
SimpleCov::Formatter::HTMLFormatter,
Coveralls::SimpleCov::Formatter
]
Expand Down

0 comments on commit 39102dd

Please sign in to comment.