Skip to content

Instantly share code, notes, and snippets.

@mattyoho
Forked from ryanbriones/Campfireanize.rb
Created February 17, 2010 05:58
Show Gist options
  • Save mattyoho/306347 to your computer and use it in GitHub Desktop.
Save mattyoho/306347 to your computer and use it in GitHub Desktop.
# mechanize just enough of campfire to get what i need
require 'mechanize'
class Campfireanize
def initialize(subdomain)
@subdomain = subdomain
@client = Mechanize.new
@logged_in = false
end
def login(username, password)
@client.get("http://#{@subdomain}.campfirenow.com/login") do |login_page|
rooms_page = login_page.form_with(:action => 'https://launchpad.37signals.com/authenticate') do |form|
form.username = username
form.password = password
end.click_button
@logged_in = rooms_page.link_with(:text => 'Lobby') ? true : false
end
@logged_in
end
def create_room(room_name)
return nil unless @logged_in
room_id = nil
@client.get("http://#{@subdomain}.campfirenow.com/") do |rooms_page|
rjs_response = rooms_page.form_with(:action => "http://#{@subdomain}.campfirenow.com/account/create/room?from=lobby") do |form|
form.field_with(:name => 'room[name]') { |field| field.value = room_name }
end.click_button
room_base_url = "http://#{@subdomain}.campfirenow.com/room"
if rjs_response.body =~ %r%#{room_base_url}/(\d+)%
room_id = $1
end
end
room_id
end
def toggle_guest_access(room_id)
guest_room_url = nil
@client.post("http://#{@subdomain}.campfirenow.com/room/#{room_id}/toggle_guest_access", "")
@client.get("http://#{@subdomain}.campfirenow.com/room/#{room_id}") do |room_page|
finder = Nokogiri(room_page.body)
search = finder.search("h4/text()[contains('http://#{@subdomain}.campfirenow.com')]")
guest_room_url = search && search.text
end
guest_room_url
end
end
campfire = Campfireanize.new('mydomain')
campfire.login('myusername', 'mypassword')
room = campfire.create_room('new room')
guest_url = campfire.toggle_guest_access(room)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment