Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 41 lines (36 sloc) 1.192 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
# Description:
# Loads images from Dribbble URL's
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# hubot (depress|dribbble|inspire) me - brings up popular images
# [dribbble URL] - brings up image from the URL
#
# Author:
# mattgraham

requestImage = (msg, url) ->
  msg.http(url).get() (err, res, body) ->
    if res.statusCode == 302 && res.headers.location
      requestImage msg, res.headers.location
    
    else
      data = JSON.parse(body)
      msg.send data.image_url
      msg.send '"' + data.title + '"' + " by " + data.player.name

module.exports = (robot) ->
  robot.respond /(depress|dribbble|inspire)( me)? (.*)/i, (msg) ->
    query = msg.match[4]
    query ||= 'popular'
    msg.http("http://api.dribbble.com/shots/#{query}")
      .get() (err, res, body) ->
        data = JSON.parse(body)
        idx = Math.floor(Math.random() * (data.shots.length - 2))
        for i in [0..2]
          msg.send data.shots[idx + i].image_url
  
  robot.hear /^https?:\/\/((www\.)?dribbble\.com\/shots\/?([0-9]+))|(drbl\.in\/([a-zA-Z0-9]+))/, (msg) ->
    query = msg.match[3] || msg.match[5]
    requestImage msg, "http://api.dribbble.com/shots/#{query}"
Something went wrong with that request. Please try again.