Sha256: 8a84077324388798604255f6515f402d50ba70d1f6182f56706411ef303f0f00

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

module Pyre
  # Pyre::Room is for interacting with a room!
  class Room
    attr_reader :name, :id
    
    def initialize(name, url, id, campfire) #:nodoc:
      @name     = name
      @url      = url
      @id       = id
      @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

1 entries across 1 versions & 1 rubygems

Version Path
pyre-0.2.0 lib/pyre/room.rb