Sha256: 1befb00143d27b1a70d1cbb566cdaa11112a2b1905c52e27b6a160b87d5a525b

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

module Pyre
  # Pyre::Campfire is for general Campfire tasks. Logging in, logging out, and finding rooms.
  class Campfire
    attr_reader :subdomain, :uri, :agent
    
    # Create a new Pyre::Campfire object.
    # Pass in your subdomain and maybe some options.
    # Of course, the only option is whether or not to use ssl.
    # Pass along <tt>:ssl => true</tt> if you need it.
    def initialize(subdomain, options={})
      @subdomain = subdomain
      protocol   = 'http'
      protocol  << 's' if options[:ssl]
      @uri       = "#{protocol}://#{subdomain}.campfirenow.com"
      @agent     = WWW::Mechanize.new
    end
    
    # Login with the supplied credentials. Returns true on success.
    def login(email, password)
      page = @agent.get(@uri + '/login')
      login_form = page.forms.first
      login_form.fields.name('email_address').value = email
      login_form.fields.name('password').value      = password
      @agent.submit(login_form)
      logged_in?
    end
    
    # Log out. Returns true on success.
    def logout
      page = @agent.get(@uri + '/logout')
      not logged_in?
    end
    
    def logged_in?
      @agent.current_page.links.any? {|link| link.text == 'Logout'} rescue false
    end
    
    # Get a list of all the rooms as Pyre::Room objects.
    def rooms
      if logged_in?
        @agent.get(@uri)
        @rooms = @agent.current_page.parser.search('div[@id="rooms"]//h2').map do |h2|
          name = h2.inner_text.strip
          url  = h2.at('a')[:href]
          Pyre::Room.new(name, url, self)
        end
      end
    end
    
    # Know the name of the room you're looking for? Try it here.
    def find_room_by_name(name)
      @rooms ||= rooms
      @rooms.detect {|room| room.name == name}
    end
    
    def inspect #:nodoc:
      "#<#{self.class} \"#{@subdomain}\" \"#{uri}\">"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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