Skip to content

Commit

Permalink
Added prev and next bounds checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
onewheelskyward committed Jun 24, 2015
1 parent 06065da commit b7e951b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ gem 'lita-garfield'
!garfield first - Get the first garfield comic.
!garfield last - Get the last garfield comic.
!garfield today - Get today's garfield comic.
!garfield prev - Get the previous day's comic based on the last one you displayed.*
!garfield next - Get the next day's comic based on the last on you displayed.*
!garfield prev - Get the previous day's comic based on the last one you displayed.
!garfield next - Get the next day's comic based on the last on you displayed.

* Coming soon to a Lita handler near you.
10 changes: 8 additions & 2 deletions lib/lita/handlers/garfield.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,18 @@ def handle_mdy_garfield(response)

def handle_next_garfield(response)
date = Date.parse redis.get(response.user.name)
response.reply get_garfield_for_date(date + 1, response.user.name)
unless date == Date.today
date += 1
end
response.reply get_garfield_for_date(date, response.user.name)
end

def handle_prev_garfield(response)
date = Date.parse redis.get(response.user.name)
response.reply get_garfield_for_date(date - 1, response.user.name)
unless date == Date.civil(1978, 6, 19)
date -= 1
end
response.reply get_garfield_for_date(date, response.user.name)
end
end

Expand Down
18 changes: 17 additions & 1 deletion spec/lita/handlers/garfield_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,28 @@ def get_todays_image_filename
end

# Test the saved state of the last comic you requested.
it 'will return the first and then the next garfield comic' do
it 'will return the first and then the next and then the previous garfield comic' do
send_message '!garfield first'
expect(replies.last).to include('https://garfield.com/uploads/strips/1978-06-19.jpg')
send_message '!garfield next'
expect(replies.last).to include('https://garfield.com/uploads/strips/1978-06-20.jpg')
send_message '!garfield prev'
expect(replies.last).to include('https://garfield.com/uploads/strips/1978-06-19.jpg')
end

it 'will edge case prev and next' do
today = Date.today

first = 'https://garfield.com/uploads/strips/1978-06-19.jpg'
last = "https://garfield.com/uploads/strips/#{today.year}-#{zero_prefix today.month}-#{zero_prefix today.day}.jpg"

send_message '!garfield first'
expect(replies.last).to include(first)
send_message '!garfield prev'
expect(replies.last).to include(first)
send_message '!garfield last'
expect(replies.last).to include(last)
send_message '!garfield next'
expect(replies.last).to include(last)
end
end

0 comments on commit b7e951b

Please sign in to comment.