public
Created — forked from tenderlove/campfire.rb

  • Download Gist
campfire.rb
Ruby
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
require 'uri'
require 'psych'
require 'net/http'
 
require 'meme' # Install meme_generator
 
module Campfire
class API
attr_reader :uri, :token, :pass
 
def initialize uri, token, pass = 'x'
@uri = URI.parse uri
@token = token
@pass = pass
end
 
def join room_id
connect.start do |http|
req = Net::HTTP::Post.new "/room/#{room_id}/join.xml"
req.basic_auth token, pass
http.request(req)
end
end
 
def watch room_id
uri = URI.parse('https://streaming.campfirenow.com')
 
connect(uri).start do |http|
req = Net::HTTP::Get.new "/room/#{room_id}/live.json"
req.basic_auth token, pass
http.request(req) do |res|
 
# FIXME: This will break on large chunks. It would be nicer
# to grab an IO from net/http and feed the IO directly to psych.
res.read_body do |chunk|
unless chunk.strip.empty?
chunk.split("\r").each do |message|
yield Psych.load(message)
end
end
end
end
end
end
 
def post room_id, message, type = 'Textmessage'
data = Psych.to_json('message' => {
'body' => message,
'type' => type
})
 
connect.start do |http|
req = Net::HTTP::Post.new "/room/#{room_id}/speak.json"
req['Content-Type'] = 'application/json'
req.basic_auth token, pass
http.request(req, data)
end
end
 
private
def connect uri = uri
x = Net::HTTP.new(uri.host, uri.port)
x.use_ssl = true
x
end
end
end
 
url = ARGV[0] || 'omg'
key = ARGV[1] || 'lol'
roomid = ARGV[2] || "<3<3"
 
streaming = Campfire::API.new url, key
 
streaming.join roomid
streaming.watch(roomid) do |event|
begin
id = event['room_id']
case event['body']
when /(\by\s+u\b)/i
meme = Meme.new 'Y_U_NO'
url = meme.generate $`, ($1 + $')
streaming.post id, url
when /^lh>\s*(\d+)$/
res = Net::HTTP.get_response URI.parse "http://rails.lighthouseapp.com/projects/8994/tickets/#{$1}.json"
ticket = Psych.load(res.body)['ticket']
streaming.post id, "##{ticket['number']} - #{ticket['title']}"
streaming.post id, ticket['original_body']
streaming.post id, ticket['url']
when /^pull>\s*(\d+)$/
res = Net::HTTP.get_response URI.parse "http://github.com/api/v2/json/pulls/rails/rails/#{$1}"
pull = Psych.load res.body
streaming.post id, pull['pull']['body']
streaming.post id, "Patch: #{pull['pull']['patch_url']}"
when '/r/pics'
res = Net::HTTP.get_response URI.parse "http://www.reddit.com/r/pics.json"
pics = Psych.load res.body
pic = pics['data']['children'].reject { |x|
x['over_18']
}.sort_by { rand }.first
 
streaming.post id, pic['data']['title']
streaming.post id, pic['data']['url']
when /^common$/i
streaming.post id, 'https://img.skitch.com/20110215-qh6q2a4sfug3maxea9c5tquasr.jpg'
when /I have a doubt/i
streaming.post id, 'https://img.skitch.com/20110215-jwsbq45ck1af8rihdijj171qa3.jpg'
end
rescue Exception => e
streaming.post id, e.message
streaming.post id, e.backtrace.join("\n")
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.