Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Added Campfire widget
Browse files Browse the repository at this point in the history
  • Loading branch information
William Roe committed Apr 23, 2010
1 parent 12710d4 commit 12fcbb9
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ db/*.sqlite3
log/*.log
tmp/**/*
.DS_Store
*~

1 change: 1 addition & 0 deletions .rvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rvm ruby-1.8.7@sonia
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ group :runtime do
gem "rake"
gem "configliere"
gem "optiflag"
gem "httparty"
end

group :test do
Expand Down
7 changes: 7 additions & 0 deletions example/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ Tfl:
:name: tfl
:title: TfL Status
:url: "http://api.tubeupdates.com/?method=get.status&lines=all&format=json"

Campfire:
:name: campfire
:title: Campfire
:room_id: 1
:url: "http://your-group-subdomain.campfirenow.com"
:token: your-api-token
1 change: 1 addition & 0 deletions lib/sonia/widgets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ module Widgets
#end
autoload :Twitter, "sonia/widgets/twitter"
autoload :Tfl, "sonia/widgets/tfl"
autoload :Campfire, "sonia/widgets/campfire"
end
end
73 changes: 73 additions & 0 deletions lib/sonia/widgets/campfire.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require 'yajl'
require 'twitter/json_stream'
require 'uri'
require 'httparty'
require 'em-http'
require 'digest/md5'

module Sonia
module Widgets
class Campfire < Sonia::Widget
GRAVATAR_URL = "http://www.gravatar.com/avatar/"

def initialize(config)
super(config)
@room_uri = URI.encode("#{config[:url]}/room/#{config[:room_id]}.json")
user_info()
connect_to_stream()

EventMachine::add_periodic_timer(150) { user_info() }
end

private
def connect_to_stream
puts "Connecting to campfire: #{config.inspect}"
@stream = ::Twitter::JSONStream.connect(
:path => "/room/#{config[:room_id]}/live.json",
:host => 'streaming.campfirenow.com',
:auth => "#{config[:token]}:x"
)

@stream.each_item do |message|
json_message = Yajl::Parser.parse(message)
puts json_message
formatted = format_message(json_message)
push formatted unless formatted.nil?
end
end

def format_message(message)
if message['type']=='TextMessage'
user = @users[message['user_id']]
img = user.nil? ? '' : user_gravatar(user[:email])
name = user.nil? ? 'Unknown' : user[:name]
return {
:body => message['body'],
:user => name,
:avatar => img
}
end
return nil
end

def user_info()
puts "Getting user info"
@users ||= {}
room_info = HTTParty.get(@room_uri, :format => :json, :basic_auth => {:username => config[:token], :password => 'x'})
puts room_info.inspect
room_info['room']['users'].each do |user|
@users[user['id']] = {
:name => user['name'],
:email => user['email_address']
}
end
puts @users.inspect
end

def user_gravatar(email)
email_digest = Digest::MD5.hexdigest(email)
"#{GRAVATAR_URL}#{email_digest}?d=identicon"
end
end
end
end
Binary file added public/images/campfire-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<script type="text/javascript" src="javascripts/widget.js"></script>
<script type="text/javascript" src="javascripts/twitter.js"></script>
<script type="text/javascript" src="javascripts/tfl.js"></script>
<script type="text/javascript" src="javascripts/campfire.js"></script>
<script type="text/javascript">
$(document).observe("dom:loaded", function() {
if ("WebSocket" in window) {
Expand Down
35 changes: 35 additions & 0 deletions public/javascripts/campfire.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var Campfire = Class.create(Widget, {
initialize: function($super, widget_id, config) {
this.messages = [];
return($super(widget_id, config));
},
receive: function(payload) {
console.log("Received some payload: " + payload);
if(this.messages.length >= this.config.nitems) {
this.messages.shift();
}
this.messages.push(payload);
this.update();
},
update: function() {
this.container.childElements().invoke('remove');
this.container.appendChild(this.buildWidgetIcon());
this.container.appendChild(this.buildHeader());
this.messages.reverse(false).each(function(message) {
var cont = new Element('p');
cont.appendChild(new Element('img', { src: message.avatar, width: 48, height: 48 }));
cont.appendChild(new Element('a', { href: '#', className: 'author' }).update(message.user));
cont.appendChild(document.createTextNode(message.body));
cont.appendChild(new Element('hr' ));
this.container.appendChild(cont);
}.bind(this));
},

buildHeader: function() {
return(new Element("h2").update(this.title));
},

buildWidgetIcon: function() {
return(new Element("img", {width: 32, height: 32, className: 'campfire_icon'}));
}
});
6 changes: 6 additions & 0 deletions public/stylesheets/sonia.css
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ div.widget img.twitter_icon {
background-image: url('../images/twitter-32x32.png');
}

div.widget img.campfire_icon {
float: right !important;
margin: 0;
background-image: url('../images/campfire-32x32.png');
}

div.widget img.tfl_icon {
float: right !important;
margin: 0;
Expand Down

2 comments on commit 12fcbb9

@wjlroe
Copy link

@wjlroe wjlroe commented on 12fcbb9 Apr 23, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am so aweome

@pusewicz
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True story

Please sign in to comment.