Permalink
Browse files

Added Campfire widget

  • Loading branch information...
1 parent 12710d4 commit 12fcbb959a0f9d5c49c3f0208ae9110930e07bb8 William Roe committed Apr 23, 2010
View
@@ -3,3 +3,5 @@ db/*.sqlite3
log/*.log
tmp/**/*
.DS_Store
+*~
+
View
1 .rvmrc
@@ -0,0 +1 @@
+rvm ruby-1.8.7@sonia
View
@@ -10,6 +10,7 @@ group :runtime do
gem "rake"
gem "configliere"
gem "optiflag"
+ gem "httparty"
end
group :test do
View
@@ -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
View
@@ -6,5 +6,6 @@ module Widgets
#end
autoload :Twitter, "sonia/widgets/twitter"
autoload :Tfl, "sonia/widgets/tfl"
+ autoload :Campfire, "sonia/widgets/campfire"
end
end
@@ -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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -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) {
@@ -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'}));
+ }
+});
@@ -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;

0 comments on commit 12fcbb9

Please sign in to comment.