Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

Adds "clipart me" command that returns clipart images from the openclipart API #1183

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/scripts/clipart.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Description:
# Returns clip art images from openclipart.org API.
#
# Commands:
# hubot clipart me <query> - Queries openclipart for <query> and returns a random result.

module.exports = (robot) ->
robot.respond /(clipart)( me)? (.*)/i, (msg) ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably not worth capturing (clipart), since you don't use it. Nor is the optional me used. That means you could refactor to:

/clipart(?: me)? (.*)/

And then you'd be able to use msg.match[1] below.

clipartMe msg, msg.match[3], (url) ->
msg.send encodeURI(url)

clipartMe = (msg, query, cb) ->
q = query: query

msg.http('http://openclipart.org/search/json/')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can call robot.http, if it's simpler. You wouldn't need to pass a msg, since you don't use it in this method anyways. That would mean this would need to be defined under the modules.export = (robot) -> block.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am refactoring this now. I think I need to stick with the msg object though as I need to use its "random" method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, missed that. It's probably fine then. Or alternatively, you could return all the art from this callback, and let the thing consuming it call msg.random.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yah thats probably a smarter approach. good call

.query(q)
.get() (err, res, body) ->
data = JSON.parse(body)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might want to check that res.statusCode is 200, and that there isn't an err before trying to parse.

images = data.payload
if images?.length > 0
image = msg.random images
cb "#{image.svg.png_full_lossy}"