Class: Tinder::Campfire

Inherits:
Object
  • Object
show all
Defined in:
lib/tinder/campfire.rb

Overview

Usage

campfire = Tinder::Campfire.new 'mysubdomain'
campfire. '[email protected]', 'mypassword'
room = campfire.create_room 'New Room', 'My new campfire room to test tinder'
room.speak 'Hello world!'
room.destroy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subdomain, options = {}) ⇒ Campfire

Create a new connection to the campfire account with the given subdomain. There’s an :ssl option to use SSL for the connection.

c = Tinder::Campfire.new("mysubdomain", :ssl => true)


17
18
19
20
21
22
23
# File 'lib/tinder/campfire.rb', line 17

def initialize(subdomain, options = {})
  options = { :ssl => false }.merge(options)
  @cookie = nil
  @subdomain = subdomain
  @uri = URI.parse("#{options[:ssl] ? 'https' : 'http' }://#{subdomain}.campfirenow.com")
  @logged_in = false
end

Instance Attribute Details

#subdomainObject (readonly)

Returns the value of attribute subdomain.



11
12
13
# File 'lib/tinder/campfire.rb', line 11

def subdomain
  @subdomain
end

#uriObject (readonly)

Returns the value of attribute uri.



11
12
13
# File 'lib/tinder/campfire.rb', line 11

def uri
  @uri
end

Instance Method Details

#available_transcripts(room = nil) ⇒ Object

Get the dates of the available transcripts by room

campfire.available_transcripts
#=> {"15840" => [#<Date: 4908311/2,0,2299161>, #<Date: 4908285/2,0,2299161>]}


81
82
83
84
85
86
87
88
89
90
# File 'lib/tinder/campfire.rb', line 81

def available_transcripts(room = nil)
  url = "files%2Btranscripts"
  url += "?room_id#{room}" if room
  transcripts = (Hpricot(get(url).body) / ".transcript").inject({}) do |result,transcript|
    link = (transcript / "a").first.attributes['href']
    (result[room_id_from_url(link)] ||= []) << Date.parse(link.scan(/\/transcript\/(\d{4}\/\d{2}\/\d{2})/).to_s)
    result
  end
  room ? transcripts[room.to_s] : transcripts
end

#create_room(name, topic = nil) ⇒ Object

Creates and returns a new Room with the given name and optionally a topic



58
59
60
# File 'lib/tinder/campfire.rb', line 58

def create_room(name, topic = nil)
  find_room_by_name(name) if verify_response(post("account/create/room?from=lobby", {:room => {:name => name, :topic => topic}}, :ajax => true), :success)
end

#find_or_create_room_by_name(name) ⇒ Object



62
63
64
# File 'lib/tinder/campfire.rb', line 62

def find_or_create_room_by_name(name)
  find_room_by_name(name) || create_room(name)
end

#find_room_by_name(name) ⇒ Object

Find a campfire room by name



53
54
55
# File 'lib/tinder/campfire.rb', line 53

def find_room_by_name(name)
  rooms.detect {|room| room.name == name }
end

#hostObject

Deprecated: only included for backwards compatability



93
94
95
# File 'lib/tinder/campfire.rb', line 93

def host #:nodoc:
  uri.host
end

#logged_in?Boolean

Returns true when successfully logged in

Returns:

  • (Boolean)


34
35
36
# File 'lib/tinder/campfire.rb', line 34

def logged_in?
  @logged_in === true
end

#login(email, password) ⇒ Object

Log in to campfire using your email and password



26
27
28
29
30
31
# File 'lib/tinder/campfire.rb', line 26

def (email, password)
  unless verify_response(post("login", :email_address => email, :password => password), :redirect_to => url_for(:only_path => false))
    raise Error, "Campfire login failed"
  end
  @logged_in = true
end

#logoutObject



38
39
40
41
42
# File 'lib/tinder/campfire.rb', line 38

def logout
  returning verify_response(get("logout"), :redirect) do |result|
    @logged_in = !result
  end
end

#roomsObject

Get an array of all the available rooms TODO: detect rooms that are full (no link)



46
47
48
49
50
# File 'lib/tinder/campfire.rb', line 46

def rooms
  Hpricot(get.body).search("//h2/a").collect do |a|
    Room.new(self, room_id_from_url(a.attributes['href']), a.inner_html)
  end
end

#ssl?Boolean

Is the connection to campfire using ssl?

Returns:

  • (Boolean)


98
99
100
# File 'lib/tinder/campfire.rb', line 98

def ssl?
  uri.scheme == 'https'
end

#users(*room_names) ⇒ Object

List the users that are currently chatting in any room



67
68
69
70
71
72
73
74
# File 'lib/tinder/campfire.rb', line 67

def users(*room_names)
  users = Hpricot(get.body).search("div.room").collect do |room|
    if room_names.empty? || room_names.include?((room/"h2/a").inner_html)
      room.search("//li.user").collect { |user| user.inner_html }
    end
  end
  users.flatten.compact.uniq.sort
end