Skip to content

@batasrki /campfire.rb forked from tenderlove/campfire.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.