Class: Pyre::Room

Inherits:
Object
  • Object
show all
Defined in:
lib/pyre/room.rb

Overview

Pyre::Room is for interacting with a room!

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, url, id, campfire) ⇒ Room

:nodoc:



6
7
8
9
10
11
# File 'lib/pyre/room.rb', line 6

def initialize(name, url, id, campfire) #:nodoc:
  @name     = name
  @url      = url
  @id       = id
  @campfire = campfire
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/pyre/room.rb', line 4

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/pyre/room.rb', line 4

def name
  @name
end

Instance Method Details

#inspectObject

:nodoc:



60
61
62
# File 'lib/pyre/room.rb', line 60

def inspect #:nodoc:
  "#<#{self.class} \"#{@name}\" \"#{@url}\">"
end

#joinObject

Enter the room. Returns true on success.



14
15
16
17
# File 'lib/pyre/room.rb', line 14

def join
  @campfire.agent.get(@url)
  joined? or raise NavigationError
end

#joined?Boolean

If you’re in the room right now.

Returns:

  • (Boolean)


29
30
31
# File 'lib/pyre/room.rb', line 29

def joined?
  @campfire.agent.current_page.at('h1[@id="room_name"]').inner_text == @name rescue false
end

#leaveObject

Leave the room. Returns true on success.



20
21
22
23
24
25
26
# File 'lib/pyre/room.rb', line 20

def leave
  if joined?
    leave = @campfire.agent.current_page.links.detect {|link| link.text == 'Leave'}
    @campfire.agent.post(leave.uri)
    not joined? or raise NavigationError
  end
end

#paste(message) ⇒ Object

Paste message to the room. Joins the room if necessary. Returns true on success.



42
43
44
45
46
47
# File 'lib/pyre/room.rb', line 42

def paste(message)
  result = submit_chat_form(message, 'paste' => 'true')
  success = (result.links.detect {|link| link.text == 'View paste'} and result.uri.to_s == "#{@url}/speak")
  join
  success or raise CommunicationError
end

#speak(message) ⇒ Object

Send message to the room. Joins the room if necessary. Returns true on success.



34
35
36
37
38
39
# File 'lib/pyre/room.rb', line 34

def speak(message)
  result = submit_chat_form(message)
  success = (result.uri.to_s == "#{@url}/speak")
  join
  success or raise CommunicationError
end

#upload(filename) ⇒ Object

Upload a file (named filename) to the room. Joins the room if necessary. Returns true on success.



50
51
52
53
54
55
56
57
58
# File 'lib/pyre/room.rb', line 50

def upload(filename)
  join unless joined?
  upload_form = @campfire.agent.current_page.forms.detect {|form| form.form_node[:id] == 'upload_form_tag'}
  upload_form.file_uploads.first.file_name = filename
  result = @campfire.agent.submit(upload_form)
  join
  success = @campfire.agent.current_page.search(%Q{ul[@id="file_list"]//a[@href$="/#{File.basename(filename)}"]}).size > 0
  success or raise UploadError
end