Class: Pyre::Campfire

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

Overview

Pyre::Campfire is for general Campfire tasks. Logging in, logging out, and finding rooms.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

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 :ssl => true if you need it. Give it a block for self-contained wonderfulness (logs out for you).



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pyre/campfire.rb', line 12

def initialize(subdomain, options={})
  @subdomain = subdomain
  protocol   = 'http'
  protocol  << 's' if options[:ssl]
  @uri       = "#{protocol}://#{subdomain}.campfirenow.com"
  @agent     = WWW::Mechanize.new
  
  if block_given?
    yield self
    self.logout
  end
  
  self
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



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

def agent
  @agent
end

#subdomainObject (readonly)

Returns the value of attribute subdomain.



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

def subdomain
  @subdomain
end

#uriObject (readonly)

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

#find_room_by_id(id) ⇒ Object

Know the id of the room you’re looking for? Try it here.



67
68
69
70
# File 'lib/pyre/campfire.rb', line 67

def find_room_by_id(id)
  @rooms ||= rooms
  @rooms.detect {|room| room.id == id}
end

#find_room_by_name(name) ⇒ Object

Know the name of the room you’re looking for? Try it here.



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

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

#inspectObject

:nodoc:



92
93
94
# File 'lib/pyre/campfire.rb', line 92

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

#logged_in?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/pyre/campfire.rb', line 43

def logged_in?
  @agent.current_page.links.any? {|link| link.text == 'Logout'} rescue false
end

#login(email, password) ⇒ Object

Login with the supplied credentials. Returns true on success.



28
29
30
31
32
33
34
35
# File 'lib/pyre/campfire.rb', line 28

def (email, password)
  page = @agent.get(@uri + '/login')
   = page.forms.first
  .fields.name('email_address').value = email
  .fields.name('password').value      = password
  @agent.submit()
  logged_in? or raise LoginError
end

#logoutObject

Log out. Returns true on success.



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

def logout
  page = @agent.get(@uri + '/logout')
  not logged_in?
end

#room(identifier) ⇒ Object

Pass the name or id of the room and get it back. Pass a block too! If you pass a block, the room will be left when the block completes.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pyre/campfire.rb', line 74

def room(identifier)
  room = case identifier
  when String
    find_room_by_name(identifier)
  when Integer
    find_room_by_id(identifier)
  end
  if block_given?
    if room
      yield room
      room.leave
    else
      raise NavigationError, "No such room: #{identifier}"
    end
  end
  room
end

#roomsObject

Get a list of all the rooms as Pyre::Room objects.



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

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]
      id   = h2.at('a')[:href].match(/room\/(\d+)/)[1].to_i
      Pyre::Room.new(name, url, id, self)
    end
  end
end