From 935b9fb814ea6aee3e93b68d1fb0ae5749ffccaa Mon Sep 17 00:00:00 2001 From: Jimmy Cuadra Date: Mon, 8 Jul 2013 11:14:46 -0700 Subject: [PATCH] Make terms case-insensitive. --- lib/lita/handlers/karma.rb | 16 ++++++++++------ lita-karma.gemspec | 2 +- spec/lita/handlers/karma_spec.rb | 6 ++++++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/lita/handlers/karma.rb b/lib/lita/handlers/karma.rb index 5ee5a65..f4a86c5 100644 --- a/lib/lita/handlers/karma.rb +++ b/lib/lita/handlers/karma.rb @@ -39,7 +39,7 @@ def check(response) output = [] response.matches.each do |match| - term = match[0] + term = normalize_term(match[0]) own_score = score = redis.zscore("terms", term).to_i links = [] redis.smembers("links:#{term}").each do |link| @@ -69,7 +69,7 @@ def list_worst(response) def link(response) response.matches.each do |match| - term1, term2 = match + term1, term2 = normalize_term(match[0]), normalize_term(match[1]) if redis.sadd("links:#{term1}", term2) response.reply "#{term2} has been linked to #{term1}." @@ -81,7 +81,7 @@ def link(response) def unlink(response) response.matches.each do |match| - term1, term2 = match + term1, term2 = normalize_term(match[0]), normalize_term(match[1]) if redis.srem("links:#{term1}", term2) response.reply "#{term2} has been unlinked from #{term1}." @@ -92,9 +92,9 @@ def unlink(response) end def modified(response) - term = response.args[1] + term = normalize_term(response.args[1]) - if term.nil? || term.strip.empty? + if term.empty? response.reply "Format: #{robot.name}: karma modified TERM" return end @@ -115,7 +115,7 @@ def modified(response) def modify(response, delta) response.matches.each do |match| - term = match[0] + term = normalize_term(match[0]) ttl = redis.ttl("cooldown:#{response.user.id}:#{term}") if ttl >= 0 @@ -141,6 +141,10 @@ def modify(response, delta) check(response) end + def normalize_term(term) + term.to_s.downcase.strip + end + def list(response, redis_command) n = (response.args[1] || 5).to_i - 1 diff --git a/lita-karma.gemspec b/lita-karma.gemspec index 09eba56..19aeb4e 100644 --- a/lita-karma.gemspec +++ b/lita-karma.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |spec| spec.name = "lita-karma" - spec.version = "1.0.0" + spec.version = "1.0.1" spec.authors = ["Jimmy Cuadra"] spec.email = ["jimmy@jimmycuadra.com"] spec.description = %q{A Lita handler for tracking karma points for arbitrary terms.} diff --git a/spec/lita/handlers/karma_spec.rb b/spec/lita/handlers/karma_spec.rb index c1e9015..be3617f 100644 --- a/spec/lita/handlers/karma_spec.rb +++ b/spec/lita/handlers/karma_spec.rb @@ -36,6 +36,12 @@ send_message("foo++") expect(replies.last).to match(/cannot modify foo/) end + + it "is case insensitive" do + send_message("foo++") + send_message("FOO++") + expect(replies.last).to eq("foo: 2") + end end describe "#decrement" do