Sha256: 4f280734751500c77e8e7f536424f65db7517be2d3c3efcfaa929774e7498cca

Contents?: true

Size: 1.66 KB

Versions: 2

Compression:

Stored size: 1.66 KB

Contents

module Pyre
  # Pyre::Room is for interacting with a room!
  class Room
    attr_reader :name
    
    def initialize(name, url, campfire) #:nodoc:
      @name     = name
      @url      = url
      @campfire = campfire
    end
    
    # Enter the room. Returns true on success.
    def join
      @campfire.agent.get(@url)
      joined?
    end
    
    # Leave the room. Returns true on success.
    def leave
      if joined?
        leave = @campfire.agent.current_page.links.detect {|link| link.text == 'Leave'}
        @campfire.agent.post(leave.uri)
        not joined?
      end
    end
    
    # If you're in the room right now.
    def joined?
      @campfire.agent.current_page.at('h1[@id="room_name"]').inner_text == @name rescue false
    end
    
    # Send +message+ to the room. Joins the room if necessary. 
    def speak(message)
      result = submit_chat_form(message)
      success = (result.uri.to_s == "#{@url}/speak")
      join
      success
    end
    
    # Paste +message+ to the room. Joins the room if necessary.
    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
    end
    
    def inspect #:nodoc:
      "#<#{self.class} \"#{@name}\" \"#{@url}\">"
    end
    
    protected
    
    def submit_chat_form(message, options={}) #:nodoc:
      join unless joined?
      speak_form = @campfire.agent.current_page.forms.detect {|form| form.form_node[:id] == 'chat_form'}
      @campfire.agent.post(@url + '/speak', {'message' => message, 't' => Time.now.to_i.to_s}.merge(options))
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pyre-0.1.1 lib/pyre/room.rb
pyre-0.1.0 lib/pyre/room.rb