-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Adds "clipart me" command that returns clipart images from the openclipart API #1183
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) -> | ||
clipartMe msg, msg.match[3], (url) -> | ||
msg.send encodeURI(url) | ||
|
||
clipartMe = (msg, query, cb) -> | ||
q = query: query | ||
|
||
msg.http('http://openclipart.org/search/json/') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can call There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might want to check that |
||
images = data.payload | ||
if images?.length > 0 | ||
image = msg.random images | ||
cb "#{image.svg.png_full_lossy}" | ||
|
There was a problem hiding this comment.
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:And then you'd be able to use
msg.match[1]
below.