Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 957ad5d55d46b70f83ddd739818cf0c32e1d0b81 @afcapel committed Jul 21, 2011
Showing with 2,288 additions and 0 deletions.
  1. +9 −0 .gitignore
  2. +34 −0 Gemfile
  3. +161 −0 Gemfile.lock
  4. +20 −0 MIT-LICENSE
  5. +3 −0 README.rdoc
  6. +34 −0 Rakefile
  7. +21 −0 lib/pushy_resources.rb
  8. +42 −0 lib/pushy_resources/channel.rb
  9. +29 −0 lib/pushy_resources/command.rb
  10. +39 −0 lib/pushy_resources/event.rb
  11. +22 −0 lib/pushy_resources/message_dispatcher.rb
  12. +28 −0 lib/pushy_resources/pushing.rb
  13. +32 −0 lib/pushy_resources/server.rb
  14. +45 −0 lib/pushy_resources/subscriber.rb
  15. +4 −0 lib/tasks/pushy_resources_tasks.rake
  16. +9 −0 pushy_resources.gemspec
  17. +6 −0 script/rails
  18. +7 −0 test/dummy/Rakefile
  19. +10 −0 test/dummy/app/assets/javascripts/application.js
  20. +31 −0 test/dummy/app/assets/javascripts/pushy-client.js.coffee
  21. +480 −0 test/dummy/app/assets/javascripts/vendor/json2.js
  22. +7 −0 test/dummy/app/assets/stylesheets/application.css
  23. +4 −0 test/dummy/app/controllers/application_controller.rb
  24. +85 −0 test/dummy/app/controllers/chats_controller.rb
  25. +12 −0 test/dummy/app/controllers/messages_controller.rb
  26. +20 −0 test/dummy/app/controllers/sessions_controller.rb
  27. +30 −0 test/dummy/app/controllers/users_controller.rb
  28. +2 −0 test/dummy/app/helpers/application_helper.rb
  29. +6 −0 test/dummy/app/helpers/chats_helper.rb
  30. +23 −0 test/dummy/app/helpers/error_messages_helper.rb
  31. +22 −0 test/dummy/app/helpers/layout_helper.rb
  32. 0 test/dummy/app/mailers/.gitkeep
  33. 0 test/dummy/app/models/.gitkeep
  34. +3 −0 test/dummy/app/models/chat.rb
  35. +5 −0 test/dummy/app/models/message.rb
  36. +34 −0 test/dummy/app/models/user.rb
  37. +21 −0 test/dummy/app/views/chats/_form.html.erb
  38. +6 −0 test/dummy/app/views/chats/edit.html.erb
  39. +23 −0 test/dummy/app/views/chats/index.html.erb
  40. +5 −0 test/dummy/app/views/chats/new.html.erb
  41. +41 −0 test/dummy/app/views/chats/show.html.erb
  42. +19 −0 test/dummy/app/views/layouts/application.html.erb
  43. +15 −0 test/dummy/app/views/sessions/new.html.erb
  44. +20 −0 test/dummy/app/views/users/_form.html.erb
  45. +3 −0 test/dummy/app/views/users/edit.html.erb
  46. +25 −0 test/dummy/app/views/users/index.html.erb
  47. +5 −0 test/dummy/app/views/users/new.html.erb
  48. +15 −0 test/dummy/app/views/users/show.html.erb
  49. +4 −0 test/dummy/config.ru
  50. +42 −0 test/dummy/config/application.rb
  51. +10 −0 test/dummy/config/boot.rb
  52. +31 −0 test/dummy/config/database.yml
  53. +5 −0 test/dummy/config/environment.rb
  54. +27 −0 test/dummy/config/environments/development.rb
  55. +54 −0 test/dummy/config/environments/production.rb
  56. +39 −0 test/dummy/config/environments/test.rb
  57. +7 −0 test/dummy/config/initializers/backtrace_silencers.rb
  58. +10 −0 test/dummy/config/initializers/inflections.rb
  59. +5 −0 test/dummy/config/initializers/mime_types.rb
  60. +7 −0 test/dummy/config/initializers/secret_token.rb
  61. +8 −0 test/dummy/config/initializers/session_store.rb
  62. +12 −0 test/dummy/config/initializers/wrap_parameters.rb
  63. +5 −0 test/dummy/config/locales/en.yml
  64. +19 −0 test/dummy/config/routes.rb
  65. +9 −0 test/dummy/db/migrate/20110719090458_create_chats.rb
  66. +10 −0 test/dummy/db/migrate/20110719090538_create_messages.rb
  67. +16 −0 test/dummy/db/migrate/20110720193249_create_users.rb
  68. +38 −0 test/dummy/db/schema.rb
  69. +48 −0 test/dummy/lib/controller_authentication.rb
  70. 0 test/dummy/log/.gitkeep
  71. +26 −0 test/dummy/public/404.html
  72. +26 −0 test/dummy/public/422.html
  73. +26 −0 test/dummy/public/500.html
  74. 0 test/dummy/public/favicon.ico
  75. +75 −0 test/dummy/public/stylesheets/application.css
  76. +6 −0 test/dummy/script/rails
  77. +13 −0 test/integration/channel_presence_test.rb
  78. +7 −0 test/integration/navigation_test.rb
  79. +63 −0 test/models/channel_test.rb
  80. +28 −0 test/models/command_test.rb
  81. +26 −0 test/models/pushing_test.rb
  82. +7 −0 test/models/pushy_resources_test.rb
  83. +15 −0 test/models/subscriber_test.rb
  84. +19 −0 test/support/factories.rb
  85. +5 −0 test/support/integration_test.rb
  86. +12 −0 test/support/mocks/mock_websocket.rb
  87. +11 −0 test/test_helper.rb
@@ -0,0 +1,9 @@
+.DS_Store
+.rvmrc
+.bundle/
+log/*.log
+pkg/
+test/dummy/db/*.sqlite3
+test/dummy/log/*.log
+test/dummy/tmp/
+
34 Gemfile
@@ -0,0 +1,34 @@
+source "http://rubygems.org"
+
+gem 'rails', '3.1.0.rc4'
+gem "mysql2"
+
+# Rails 3.1 - Asset Pipeline
+gem 'json'
+gem 'sass-rails'
+gem 'coffee-script'
+gem 'uglifier'
+
+gem "nifty-generators", :group => :development
+
+
+# Rails 3.1 - JavaScript
+gem 'jquery-rails'
+
+# Pusher server
+gem 'eventmachine', :git => 'git://github.com/eventmachine/eventmachine.git'
+gem 'em-websocket'
+gem 'em-synchrony'
+gem 'em-http-request'
+
+gem 'capybara'
+gem 'launchy'
+gem 'factory_girl'
+
+# Bundle edge Rails instead:
+# gem 'rails', :git => 'git://github.com/rails/rails.git'
+
+
+if RUBY_VERSION < '1.9'
+ gem "ruby-debug", ">= 0.10.3"
+end
@@ -0,0 +1,161 @@
+GIT
+ remote: git://github.com/eventmachine/eventmachine.git
+ revision: 3d47cd2ebd519d1135babc5b53a48aa5724ec7dc
+ specs:
+ eventmachine (1.0.0.beta.4)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ actionmailer (3.1.0.rc4)
+ actionpack (= 3.1.0.rc4)
+ mail (~> 2.3.0)
+ actionpack (3.1.0.rc4)
+ activemodel (= 3.1.0.rc4)
+ activesupport (= 3.1.0.rc4)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ i18n (~> 0.6)
+ rack (~> 1.3.0)
+ rack-cache (~> 1.0.1)
+ rack-mount (~> 0.8.1)
+ rack-test (~> 0.6.0)
+ sprockets (~> 2.0.0.beta.10)
+ tzinfo (~> 0.3.27)
+ activemodel (3.1.0.rc4)
+ activesupport (= 3.1.0.rc4)
+ bcrypt-ruby (~> 2.1.4)
+ builder (~> 3.0.0)
+ i18n (~> 0.6)
+ activerecord (3.1.0.rc4)
+ activemodel (= 3.1.0.rc4)
+ activesupport (= 3.1.0.rc4)
+ arel (~> 2.1.1)
+ tzinfo (~> 0.3.27)
+ activeresource (3.1.0.rc4)
+ activemodel (= 3.1.0.rc4)
+ activesupport (= 3.1.0.rc4)
+ activesupport (3.1.0.rc4)
+ multi_json (~> 1.0)
+ addressable (2.2.6)
+ arel (2.1.3)
+ bcrypt-ruby (2.1.4)
+ builder (3.0.0)
+ capybara (1.0.0)
+ mime-types (>= 1.16)
+ nokogiri (>= 1.3.3)
+ rack (>= 1.0.0)
+ rack-test (>= 0.5.4)
+ selenium-webdriver (~> 0.2.0)
+ xpath (~> 0.1.4)
+ childprocess (0.1.9)
+ ffi (~> 1.0.6)
+ coffee-script (2.2.0)
+ coffee-script-source
+ execjs
+ coffee-script-source (1.1.1)
+ em-http-request (0.3.0)
+ addressable (>= 2.0.0)
+ escape_utils
+ eventmachine (>= 0.12.9)
+ em-synchrony (0.2.0)
+ eventmachine (>= 0.12.9)
+ em-websocket (0.3.0)
+ addressable (>= 2.1.1)
+ eventmachine (>= 0.12.9)
+ erubis (2.7.0)
+ escape_utils (0.2.3)
+ execjs (1.2.0)
+ multi_json (~> 1.0)
+ factory_girl (1.3.3)
+ ffi (1.0.9)
+ hike (1.2.0)
+ i18n (0.6.0)
+ jquery-rails (1.0.12)
+ railties (~> 3.0)
+ thor (~> 0.14)
+ json (1.5.3)
+ json_pure (1.5.3)
+ launchy (2.0.3)
+ mail (2.3.0)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ mime-types (1.16)
+ multi_json (1.0.3)
+ mysql2 (0.3.6)
+ nifty-generators (0.4.6)
+ nokogiri (1.5.0)
+ polyglot (0.3.1)
+ rack (1.3.1)
+ rack-cache (1.0.2)
+ rack (>= 0.4)
+ rack-mount (0.8.1)
+ rack (>= 1.0.0)
+ rack-ssl (1.3.2)
+ rack
+ rack-test (0.6.0)
+ rack (>= 1.0)
+ rails (3.1.0.rc4)
+ actionmailer (= 3.1.0.rc4)
+ actionpack (= 3.1.0.rc4)
+ activerecord (= 3.1.0.rc4)
+ activeresource (= 3.1.0.rc4)
+ activesupport (= 3.1.0.rc4)
+ bundler (~> 1.0)
+ railties (= 3.1.0.rc4)
+ railties (3.1.0.rc4)
+ actionpack (= 3.1.0.rc4)
+ activesupport (= 3.1.0.rc4)
+ rack-ssl (~> 1.3.2)
+ rake (>= 0.8.7)
+ rdoc (~> 3.4)
+ thor (~> 0.14.6)
+ rake (0.9.2)
+ rdoc (3.8)
+ rubyzip (0.9.4)
+ sass (3.1.4)
+ sass-rails (3.1.0.rc.4)
+ actionpack (~> 3.1.0.rc1)
+ railties (~> 3.1.0.rc1)
+ sass (>= 3.1.4)
+ sprockets (>= 2.0.0.beta.9)
+ selenium-webdriver (0.2.2)
+ childprocess (>= 0.1.9)
+ ffi (>= 1.0.7)
+ json_pure
+ rubyzip
+ sprockets (2.0.0.beta.10)
+ hike (~> 1.0)
+ rack (~> 1.0)
+ tilt (!= 1.3.0, ~> 1.1)
+ thor (0.14.6)
+ tilt (1.3.2)
+ treetop (1.4.9)
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.29)
+ uglifier (1.0.0)
+ execjs (>= 0.3.0)
+ multi_json (>= 1.0.2)
+ xpath (0.1.4)
+ nokogiri (~> 1.3)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ capybara
+ coffee-script
+ em-http-request
+ em-synchrony
+ em-websocket
+ eventmachine!
+ factory_girl
+ jquery-rails
+ json
+ launchy
+ mysql2
+ nifty-generators
+ rails (= 3.1.0.rc4)
+ sass-rails
+ uglifier
@@ -0,0 +1,20 @@
+Copyright 2011 YOURNAME
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
+= PushyResources
+
+This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,34 @@
+#!/usr/bin/env rake
+begin
+ require 'bundler/setup'
+rescue LoadError
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
+end
+begin
+ require 'rdoc/task'
+rescue LoadError
+ require 'rdoc/rdoc'
+ require 'rake/rdoctask'
+ RDoc::Task = Rake::RDocTask
+end
+
+RDoc::Task.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'PushyResources'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README.rdoc')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
+
+require 'rake/testtask'
+
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = false
+end
+
+
+task :default => :test
@@ -0,0 +1,21 @@
+Dir[File.dirname(__FILE__) + '/pushy_resources/*.rb'].each {|file| require file }
+
+class ActiveRecord::Base
+ extend PushyResources::Pushing
+end
+
+if EM.reactor_running?
+ PushyResources::Server.run
+else
+ Thread.new do
+ puts "Running EM reactor in new thread"
+ EM.run { PushyResources::Server.run }
+ end
+end
+
+EM.error_handler do |error|
+ puts "Error raised during event loop: #{error.message}"
+ puts error.stacktrace
+end
+
+
@@ -0,0 +1,42 @@
+module PushyResources
+ class Channel
+ attr_reader :name
+ attr_reader :em_channel
+ attr_reader :subscribers
+
+ class << self
+ def list
+ @channel_list ||= {}
+ end
+
+ def [](name)
+ list[name] ||= Channel.new(name)
+ end
+ end
+
+ def initialize(name)
+ @name = name
+ @em_channel = EM::Channel.new
+ @subscribers = []
+ end
+
+ def subscribe(websocket, credentials = nil)
+ subscriber = Subscriber.new(self, websocket, credentials)
+
+ @subscribers << subscriber
+ subscriber
+ end
+
+ def unsubscribe(subscriber)
+ @subscribers.delete(subscriber)
+ end
+
+ def receive(event)
+ @em_channel << event
+ end
+
+ def users
+ @subscribers.collect(&:user).compact.uniq
+ end
+ end
+end
@@ -0,0 +1,29 @@
+module PushyResources
+ class Command
+ attr_reader :name
+
+ def initialize(websocket, command_hash)
+ @websocket = websocket
+
+ @name = command_hash[:command].to_sym
+ @channel_name = command_hash[:channel]
+ @credentials = command_hash[:credentials]
+ end
+
+ def channel
+ @channel ||= Channel[@channel_name]
+ end
+
+ def execute!
+ case name
+ when :subscribe then
+ channel.subscribe @websocket, @credentials
+ when :unsubscribe then
+ subscriptions = Subscriber.subscriptions_for(@websocket)
+ subscriptions = subscriptions.select { |s| s.channel == @channel } if @channel
+
+ subscriptions.each(&:destroy!)
+ end
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 957ad5d

Please sign in to comment.