Skip to content

Commit

Permalink
Added prev and next; refactors.
Browse files Browse the repository at this point in the history
  • Loading branch information
onewheelskyward committed Nov 19, 2015
1 parent dd4f676 commit 5d8691d
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions lib/lita/handlers/onewheel_xkcd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ class OnewheelXkcd < Handler
command: true,
help: {'xkcd prev' => 'return the previous XKCD comic by date.'}

route /^xkcd prev/i,
route /^xkcd next/i,
:find_next,
command: true,
help: {'xkcd prev' => 'return the next XKCD comic by date.'}

def find_by_keyword(response)
db = init_db
keywords = response.matches[0][0]
result = db["select
id, data->'img' as img, data->'title' as title, data->'alt' as alt
result = db["
select id, data->'img' as img, data->'title' as title, data->'alt' as alt
from comics
where data->>'title' ilike ? order by RANDOM() limit 1", "%#{keywords}%"]
if row = result[:data]
Expand All @@ -52,8 +52,8 @@ def find_by_keyword(response)
def find_by_number(response)
db = init_db
number = response.matches[0][0]
result = db["select
id, data->'img' as img, data->'title' as title, data->'alt' as alt
result = db["
select id, data->'img' as img, data->'title' as title, data->'alt' as alt
from comics
where id = ?", number]
if row = result[:data]
Expand All @@ -64,21 +64,45 @@ def find_by_number(response)

def random(response)
db = init_db
row = db["select
id, data->'img' as img, data->'title' as title, data->'alt' as alt
row = db["
select id, data->'img' as img, data->'title' as title, data->'alt' as alt
from comics
order by RANDOM()
limit 1"][:data]
comic = Comic.new(row[:id], row[:img], row[:title], row[:alt])
reply_with_comic response, comic
end

def find_next(response)
db = init_db
last_comic = get_last_comic(response.user)
last_comic += 1
comic = get_comic_by_id(db, last_comic)
reply_with_comic response, comic
end

def find_prev(response)
db = init_db
last_comic = get_last_comic(response.user)
last_comic -= 1
comic = get_comic_by_id(db, last_comic)
reply_with_comic response, comic
end

def get_comic_by_id(db, last_comic)
row = db["
select id, data->'img' as img, data->'title' as title, data->'alt' as alt
from comics
where id = ?", last_comic][:data]
comic = Comic.new(row[:id], row[:img], row[:title], row[:alt])
end

##
# Helper function to display comic and set timer for alt tag.
#
def reply_with_comic(response, comic)
set_state comic, response.user
response.reply "\"#{comic.title}\" #{comic.image}"
response.reply "XKCD #{comic.id} \"#{comic.title}\" #{comic.image}"
after(9) do |timer|
response.reply comic.alt
end
Expand All @@ -103,8 +127,8 @@ def set_state(comic, user)
#
def get_last_comic(user)
db = init_db
state = db[:state].filter(:user => user.name)
state[:last_comic]
dataset = db[:state].where(:user => user.name)
dataset.first[:last_comic]
end

def init_db
Expand Down

0 comments on commit 5d8691d

Please sign in to comment.