Skip to content

Commit

Permalink
lots of stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Oga authored and Emmanuel Oga committed May 31, 2010
1 parent 7cb69e5 commit 7e04a5c
Show file tree
Hide file tree
Showing 58 changed files with 1,142 additions and 222 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,3 +1,5 @@
doc
.yardoc
bin
*.swp
coverage
Expand Down
68 changes: 67 additions & 1 deletion README.rdoc
Expand Up @@ -2,6 +2,72 @@

Campfire API interface powered by EventMachine and Yajl.

require 'firering'

Firering.subdomain = "subdomain"
Firering.token = "token"

EM.run do
Firering.rooms do |rooms|
rooms.each do |room|
if room.name == "RoomName"

puts "Joining #{room.name}"

Firering.room_join(room.id) do
Firering.stream(room.id) do |message|

if message.from_user?
Firering.user(message.user_id) do |user|
puts "( #{user.name} ) #{message.body}"
end
else
puts message.body if message.body.to_s !~ /^\s*$/
end

end
end

end
end
end

trap("INT") { EM.stop }
end

= campf-notify

The gem bundles an executable script for spawning libnotify powered notifications.
To be able to use it, check your distro package repositories for the apropriate
package containing the "notify-send" command line utility. In the case of archlinux,
the package name is "libnotify".

The script needs the following environment variables in place:

CAMPFIRE_SUBDOMAIN
CAMPFIRE_TOKEN

Once the variables are set, run the script as follows:

campf-notify room-name /path/to/an/icon.png

And watch the lovely notifications each time something is posted to a room.

== Running the specs

For running the specs, I opted for running a sinatra application for serving
the fixture data. The server runs automatically when "rake spec" is executed,
it runs on the standard sinatra port (4567), so you should make sure that
port is free when running the specs.

For more details take a look at spec/fixtures/load_server.rb file.

== TODO

* Better API documentation
* Post files to a room
* Retrieve recently uploaded files

== Note on Patches/Pull Requests

* Fork the project.
Expand All @@ -14,4 +80,4 @@ Campfire API interface powered by EventMachine and Yajl.

== Copyright

Copyright (c) 2009 Emmanuel Oga. See LICENSE for details.
Copyright (c) 2010 Emmanuel Oga. See LICENSE for details.
22 changes: 22 additions & 0 deletions examples/authenticate.rb
@@ -0,0 +1,22 @@
require 'firering'

puts "If you prefer it, you can authenticate your user with u/p instead of the campfire token."

print "Enter subdomain: "
subdomain = gets.chomp

print "Enter user: "
user = gets.chomp

print "Enter password: "
password = gets.chomp

EM.run do
Firering.authenticate(subdomain, user, password) do |user|

puts "Token for user #{user.name} is #{user.token}"

EM.stop

end
end
55 changes: 26 additions & 29 deletions examples/events.rb
@@ -1,41 +1,38 @@
$LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))

require 'firering'

EM.run do
Firering.authenticate("subdomain", "user", "password") do |user|

Firering.rooms do |rooms|
rooms.each do |room|
if room.name =~ /test/
Firering.room_join(room.id) do
Firering.stream(room.id) do |message|
Firering.subdomain = ENV["CAMPFIRE_SUBDOMAIN"]
Firering.token = ENV["CAMPFIRE_TOKEN"]

case
when message.advertisement? then puts "Handle Ad"
when message.allow_guests? then puts "Handle Allow Guests"
when message.disallow_guests? then puts "Handle Disallow Guests"
when message.idle? then puts "Handle Idle"
when message.kick? then puts "Handle Kick"
when message.leave? then puts "Handle Leave"
when message.paste? then puts "Handle Paste"
when message.sound? then puts "Handle Sound"
when message.system? then puts "Handle System"
when message.text? then puts "Handle Text"
when message.timestamp? then puts "Handle Timestamp"
when message.topic_change? then puts "Handle Topic Change"
when message.unidle? then puts "Handle Un Idle"
when message.unlock? then puts "Handle Unlock"
when message.upload? then puts "Handle Upload"
end
EM.run do
Firering.rooms do |rooms|
rooms.each do |room|
if room.name == "test2"
Firering.room_join(room.id) do
Firering.stream(room.id) do |message|

puts message.to_s
case
when message.advertisement? then puts "Handle Ad"
when message.allow_guests? then puts "Handle Allow Guests"
when message.disallow_guests? then puts "Handle Disallow Guests"
when message.idle? then puts "Handle Idle"
when message.kick? then puts "Handle Kick"
when message.leave? then puts "Handle Leave"
when message.paste? then puts "Handle Paste"
when message.sound? then puts "Handle Sound"
when message.system? then puts "Handle System"
when message.text? then puts "Handle Text"
when message.timestamp? then puts "Handle Timestamp"
when message.topic_change? then puts "Handle Topic Change"
when message.unidle? then puts "Handle Un Idle"
when message.unlock? then puts "Handle Unlock"
when message.upload? then puts "Handle Upload"
end

puts message.to_s
end
end
end
end

end

trap("INT") { EM.stop }
Expand Down
42 changes: 28 additions & 14 deletions examples/recent_messages.rb
@@ -1,27 +1,41 @@
$LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))

require 'firering'

Firering.subdomain = ENV["CAMPFIRE_SUBDOMAIN"]
Firering.token = ENV["CAMPFIRE_TOKEN"]

EM.run do
Firering.authenticate("subdomain", "user", "password") do |user|
Firering.rooms do |rooms|
Firering.rooms do |rooms|

rooms.each do |room|
puts "Users in room #{room.name} (#{room.topic})"
rooms.each do |room|
puts "Users in room #{room.name} (#{room.topic})"

if room.users.empty?
puts " empty"
else
room.users.each do |u|
puts " #{ u.name }. Admin: #{ user.admin? }"
end
if room.users.empty?
puts " empty (locked: #{room.locked?})"
else
room.users.each do |u|
puts " #{ u.name }. Admin: #{ u.admin? }"
end
end

if room.locked?
puts " can't get recent messages in a locked room'"
else
Firering.room_recent_messages(room.id) do |messages|
puts messages
end

puts "-" * 80
puts "recent message on #{room.name}"
puts "-" * 80

messages.slice(0, 4).each do |m|
puts "\n (#{m.user_id})"

m.body.to_s.split("\n").each do |chunk|
puts " > #{chunk}"
end
end
end
end

end
end

Expand Down
33 changes: 13 additions & 20 deletions examples/rooms.rb
@@ -1,31 +1,24 @@
$LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))

require 'firering'

$count = 0
Firering.subdomain = ENV["CAMPFIRE_SUBDOMAIN"]
Firering.token = ENV["CAMPFIRE_TOKEN"]

EM.run do
Firering.authenticate("subdomain", "user", "password") do |user|

Firering.rooms do |rooms|

$count = rooms.length

rooms.each do |room|
$count -= 1
Firering.rooms do |rooms|

puts "Users in room #{room.name}"
rooms.each do |room|
puts "Users in room #{room.name}"

if room.users.empty?
puts " empty"
else
room.users.each do |u|
puts " #{ u.name }"
end
if room.users.empty?
puts " empty (locked: #{room.locked?})"
else
room.users.each do |u|
puts " #{ u.name }"
end

EM.stop if $count == 0
end

end
end

trap("INT") { EM.stop }
end
25 changes: 8 additions & 17 deletions examples/update_room.rb
@@ -1,25 +1,16 @@
$LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")))

require 'firering'

EM.run do
Firering.authenticate("subdomain", "user", "password") do |user|

Firering.rooms do |rooms|

rooms.each do |room|
Firering.subdomain = ENV["CAMPFIRE_SUBDOMAIN"]
Firering.token = ENV["CAMPFIRE_TOKEN"]

puts "Users in room #{room.name} (#{room.topic})"
ROOM = "test2"

if room.users.empty?
puts " empty"
else
room.users.each do |u|
puts " #{ u.name }. Admin: #{ user.admin? }"
end
end
EM.run do
Firering.rooms do |rooms|
rooms.each do |room|
if room.name == ROOM
Firering.room_join(room.id) do

if room.name =~ /test/
Firering.update_room(room.id, "topic" => "test test test") do |response|
puts " * Updating topic: "
puts response.response_header.status
Expand Down
1 change: 1 addition & 0 deletions lib/firering.rb
@@ -1,6 +1,7 @@
require 'eventmachine'
require 'yajl'
require 'em-http'
require 'date'

module Firering
VERSION = '0.1.0'
Expand Down
4 changes: 4 additions & 0 deletions lib/firering/data.rb
Expand Up @@ -21,6 +21,10 @@ def #{key}
def initialize(data, base_key = nil)
@data = data.is_a?(Hash) ? data : Yajl::Parser.parse(data, :symbolize_keys => true)
@data = @data[base_key] if base_key

@data.each do |key, val|
@data[key] = Date.parse(val) rescue val if key.to_s =~ /(_at|_on)$/
end
end

def inspect
Expand Down
18 changes: 2 additions & 16 deletions lib/firering/data/message.rb
@@ -1,17 +1,3 @@
=begin
<message>
<id type="integer">1</id>
<room-id type="integer">1</room-id>
<user-id type="integer">2</user-id>
<body>Hello Room</body>
<created-at type="datetime">2009-11-22T23:46:58Z</created-at>
<type>#{TextMessage || PasteMessage || SoundMessage || AdvertisementMessage ||
AllowGuestsMessage || DisallowGuestsMessage || IdleMessage || KickMessage ||
LeaveMessage || SystemMessage || TimestampMessage || TopicChangeMessage ||
UnidleMessage || UnlockMessage || UploadMessage}</type>
</message>
=end

module Firering
class Message < Firering::Data
key :id, :room_id, :user_id, :body, :created_at, :type
Expand All @@ -35,8 +21,8 @@ class Message < Firering::Data
end
end

def to_s
"#{body} (#{user_id}, #{created_at})"
def from_user?
!user_id.nil? && (!user_id.instance_of?(String) || user_id !~ /~\s*$/)
end

end
Expand Down
25 changes: 6 additions & 19 deletions lib/firering/data/room.rb
@@ -1,26 +1,13 @@
=begin
<room>
<id type="integer">1</id>
<name>North May St.</name>
<topic>37signals HQ</topic>
<membership-limit type="integer">60</membership-limit>
<full type="boolean">false</full>
<open-to-guests type="boolean">true</open-to-guests>
<active-token-value>#{ 4c8fb -- requires open-to-guests is true}</active-token-value>
<updated-at type="datetime">2009-11-17T19:41:38Z</updated-at>
<created-at type="datetime">2009-11-17T19:41:38Z</created-at>
<users type="array">
...
</users>
</room>
=end

module Firering
class Room < Firering::Data
key :id, :name, :topic, :membership_limit, :full, :open_to_guests, :active_token_value, :updated_at, :created_at, :users
key :id, :name, :topic, :membership_limit, :full, :open_to_guests, :active_token_value, :updated_at, :created_at, :users, :locked

alias_method :locked?, :locked
alias_method :full?, :full
alias_method :open_to_guests?, :open_to_guests

def users
@users ||= super.map { |u| Firering::User.new(u, false) }
@users ||= (super || []).map { |u| Firering::User.new(u, false) }
end
end
end

0 comments on commit 7e04a5c

Please sign in to comment.