Permalink
Browse files

Setup pusher

  • Loading branch information...
1 parent 302241e commit a25f95bca94cfee21087742e8e6e5a6e9f4d4856 @aubreyrhodes committed Sep 30, 2012
View
@@ -32,3 +32,4 @@ gem 'slim-rails'
gem 'thin'
gem 'omniauth-facebook'
gem 'dotenv', :groups => [:development, :test]
+gem 'pusher'
View
@@ -99,6 +99,9 @@ GEM
coderay (~> 1.0.5)
method_source (~> 0.8)
slop (~> 3.3.1)
+ pusher (0.10.0)
+ multi_json (~> 1.0)
+ signature (~> 0.1.4)
rack (1.4.1)
rack-cache (1.2)
rack (>= 0.4)
@@ -139,6 +142,7 @@ GEM
tilt (~> 1.3)
sassy-math (1.2)
compass (~> 0.11)
+ signature (0.1.4)
slim (1.3.2)
temple (~> 0.5.0)
tilt (~> 1.3.3)
@@ -184,6 +188,7 @@ DEPENDENCIES
json
omniauth-facebook
pry
+ pusher
rails (= 3.2.8)
rspec
sass-rails (~> 3.2.3)
View
@@ -7,7 +7,7 @@ def initialize(items, checked_items = [])
end
def add_item(item)
- Checklist.new(@items << item, @checked_items)
+ Checklist.new([item] + @items, @checked_items)
end
def check_off_item(item)
@@ -1,21 +1,42 @@
+add_item = (val) ->
+ $('.items-list').prepend('<div class="outer-item"><div class="item"><span class="text">'+val+'</span><a href="#" class="check">did it</a></div><br style="clear: both"/></div>')
+
+check_item = (val) ->
+ item = $('.item span.text:contains("' + val + '"):first').parent()
+ item.addClass('checked')
+ $('.check', item).remove()
+ item.parent().remove()
+ $('.items-list').append(item.parent())
+
+
+
$ ->
$('input, textarea').placeholder()
$('#new-item textarea').on('keyup', (event) ->
if event.which == 13
val = $.trim($(this).val())
return if val == ''
$(this).val('')
- $('.items-list').append('<div class="outer-item"><div class="item"><span class="text">'+val+'</span><a href="#" class="check">did it</a></div><br style="clear: both"/></div>')
$.post('/items', { item: val})
)
$('.item a.check').on('click', ->
item = $(this).parents('.item')
- item.addClass('checked')
val = $('.text', item).text()
- $(this).remove()
- item.parent().remove()
- $('.items-list').append(item.parent())
$.post('/items/check', { item: val, _method: 'put' })
false
)
+
+ pusher_id = $('body').data('pusher-id')
+ channel_name = $('body').data('channel')
+
+ pusher = new Pusher(pusher_id)
+ channel = pusher.subscribe(channel_name)
+
+ channel.bind('new', (data) ->
+ add_item(data.item)
+ )
+
+ channel.bind('check', (data) ->
+ check_item(data.item)
+ )
@@ -1,6 +1,8 @@
class ApplicationController < ActionController::Base
attr_reader :current_email
+ helper_method :current_email
+
protect_from_forgery
before_filter :authenticate!
@@ -13,11 +13,13 @@ def new
def create
@@checklists[current_email] = @@checklists[current_email].add_item(params[:item])
+ Pusher["private-#{current_email}"].trigger('new', {item: params[:item] } )
redirect_to items_path
end
def check
@@checklists[current_email] = @@checklists[current_email].check_off_item(params[:item])
+ Pusher["private-#{current_email}"].trigger('check', {item: params[:item] } )
redirect_to items_path
end
end
@@ -0,0 +1,12 @@
+class PusherController < ApplicationController
+ protect_from_forgery :except => :auth # stop rails CSRF protection for this action
+
+ def auth
+ if current_email
+ response = Pusher[params[:channel_name]].authenticate(params[:socket_id])
+ render :json => response
+ else
+ render :text => "Forbidden", :status => '403'
+ end
+ end
+end
@@ -22,9 +22,10 @@ head
/ Included JS Files
= javascript_include_tag "application"
+ script src="http://js.pusher.com/1.12/pusher.min.js"
= csrf_meta_tag
-body
+body *{ 'data-pusher-id' => Pusher.key, 'data-channel' => "private-#{current_email}" }
.container
- == yield
+ == yield
@@ -0,0 +1,7 @@
+require 'pusher'
+
+if Rails.env == 'development'
+ Pusher.app_id = ENV['PUSHER_APP_ID']
+ Pusher.key = ENV['PUSHER_KEY']
+ Pusher.secret = ENV['PUSHER_SECRET']
+end
@@ -5,6 +5,7 @@
match "/auth/:provider/callback" => "sessions#create"
match "/auth/failure" => "sessions#failure"
+ match "pusher/auth" => "pusher#auth"
resources :sessions, only: :new

0 comments on commit a25f95b

Please sign in to comment.