Skip to content

Commit

Permalink
Only append a fake image extension if it's necessary.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmycuadra committed May 8, 2014
1 parent 5f53f7b commit c28096a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/lita/handlers/google_images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def fetch(response)
if data["responseStatus"] == 200
choice = data["responseData"]["results"].sample
if choice
response.reply "#{choice["unescapedUrl"]}#.png"
response.reply ensure_extension(choice["unescapedUrl"])
else
response.reply %{No images found for "#{query}".}
end
Expand All @@ -43,6 +43,14 @@ def fetch(response)

private

def ensure_extension(url)
if [".gif", ".jpg", ".jpeg", ".png"].any? { |ext| url.end_with?(ext) }
url
else
"#{url}#.png"
end
end

def safe_value
safe = Lita.config.handlers.google_images.safe_search || "active"
safe = safe.to_s.downcase
Expand Down
22 changes: 21 additions & 1 deletion spec/lita/handlers/google_images_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@

it "replies with a matching image URL on success" do
allow(response).to receive(:body).and_return(<<-JSON.chomp
{
"responseStatus": 200,
"responseData": {
"results": [
{
"unescapedUrl": "http://www.example.com/path/to/an/image"
}
]
}
}
JSON
)
send_command("image carl")
expect(replies.last).to eq(
"http://www.example.com/path/to/an/image#.png"
)
end

it "doesn't append a fake file extension if the image URL has a common image extension" do
allow(response).to receive(:body).and_return(<<-JSON.chomp
{
"responseStatus": 200,
"responseData": {
Expand All @@ -31,7 +51,7 @@
)
send_command("image carl")
expect(replies.last).to eq(
"http://www.example.com/path/to/an/image.jpg#.png"
"http://www.example.com/path/to/an/image.jpg"
)
end

Expand Down

0 comments on commit c28096a

Please sign in to comment.