Skip to content

Instantly share code, notes, and snippets.

@zmack
Created February 16, 2011 01:36
Show Gist options
  • Save zmack/828676 to your computer and use it in GitHub Desktop.
Save zmack/828676 to your computer and use it in GitHub Desktop.
Haters gonna hate
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
x = Net::HTTP.new(uri.host, uri.port)
x.use_ssl = true
x.verify_mode = OpenSSL::SSL::VERIFY_NONE
x.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')
x = Net::HTTP.new(uri.host, uri.port)
x.use_ssl = true
x.verify_mode = OpenSSL::SSL::VERIFY_NONE
x.start do |http|
req = Net::HTTP::Get.new "/room/#{room_id}/live.json"
req.basic_auth token, pass
http.request(req) do |res|
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'
x = Net::HTTP.new(uri.host, uri.port)
x.use_ssl = true
x.verify_mode = OpenSSL::SSL::VERIFY_NONE
data = Psych.to_json('message' => {
'body' => message,
'type' => type
})
x.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
end
end
url = 'http://zzz.campfirenow.com'
key = '123'
room_id = 1234
streaming = Campfire::API.new url, key
streaming.join room_id
streaming.watch(room_id) do |event|
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
streaming.post id, ticket['ticket']['original_body']
streaming.post id, ticket['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
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment