Skip to content

Commit

Permalink
Make terms case-insensitive.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmycuadra committed Jul 8, 2013
1 parent 3b37b0f commit 935b9fb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
16 changes: 10 additions & 6 deletions lib/lita/handlers/karma.rb
Expand Up @@ -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|
Expand Down Expand Up @@ -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}."
Expand All @@ -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}."
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion 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.}
Expand Down
6 changes: 6 additions & 0 deletions spec/lita/handlers/karma_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 935b9fb

Please sign in to comment.