rack_push 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Oliver Nightingale
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ = rack_push
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Oliver Nightingale. See LICENSE for details.
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rack_push"
8
+ gem.summary = %Q{rack interface for with pusher}
9
+ gem.description = %Q{rack interface for with pusher}
10
+ gem.email = "oliver.n@new-bamboo.co.uk"
11
+ gem.homepage = "http://github.com/olivernn/rack_push"
12
+ gem.authors = ["Oliver Nightingale"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/test_*.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "rack_push #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,13 @@
1
+ require 'rack'
2
+ require 'pusher'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/jem')
5
+
6
+ require 'rack_push/request'
7
+ require 'rack_push/app'
8
+
9
+ module Rack
10
+ module Push
11
+
12
+ end
13
+ end
@@ -0,0 +1,38 @@
1
+ module Rack
2
+ module Push
3
+ class App
4
+ def initialize app, options
5
+ @app = app
6
+ @path = options[:path] || '/pusher'
7
+ @method = options[:method] || :post
8
+ Pusher.key = options[:key]
9
+ Pusher.app_id = options[:app_id]
10
+ Pusher.secret = options[:secret]
11
+ end
12
+
13
+ def call(env)
14
+ request = Push::Request.new(env)
15
+ if push_this request
16
+ if request.well_formed?
17
+ Pusher[request.channel].trigger(
18
+ request.event,
19
+ request.event_data,
20
+ request.socket_id
21
+ )
22
+ [200, {"Content-Type" => "text/plain", "Content-Length" => "0"}, [""]]
23
+ else
24
+ [412, {"Content-Type" => "text/plain", "Content-Length" => "0"}, [""]]
25
+ end
26
+ else
27
+ @app.call(env)
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def push_this request
34
+ (request.request_method.downcase.to_sym == @method) && (!!request.path_info.match(%r{^#{@path}/[\w-]*$}))
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,43 @@
1
+ module Rack
2
+ module Push
3
+ class Request < Rack::Request
4
+ include Rack::Utils
5
+
6
+ attr_reader :env
7
+
8
+ def initialize(env)
9
+ @env = env
10
+ super(env)
11
+ end
12
+
13
+ def channel
14
+ path_info.split('/').last
15
+ end
16
+
17
+ [:event, :socket_id].each do |param|
18
+ define_method param do
19
+ str = ""
20
+ query_string.split(query_string_seperator).each do |pair|
21
+ str = pair.gsub("#{param}=", "") if pair.match("#{param}=")
22
+ end
23
+ str
24
+ end
25
+ end
26
+
27
+ def event_data
28
+ self.body.rewind
29
+ self.body.read
30
+ end
31
+
32
+ def well_formed?
33
+ !event.empty? && !socket_id.empty? && !channel.empty? && !event_data.empty?
34
+ end
35
+
36
+ private
37
+
38
+ def query_string_seperator
39
+ query_string.match(";") ? ";" : "&"
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,25 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'rack/test'
4
+ require 'mocha'
5
+
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+
9
+ require 'rack_push'
10
+
11
+
12
+ class Test::Unit::TestCase
13
+ def self.test(name, &block)
14
+ test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
15
+ defined = instance_method(test_name) rescue false
16
+ raise "#{test_name} is already defined in #{self}" if defined
17
+ if block_given?
18
+ define_method(test_name, &block)
19
+ else
20
+ define_method(test_name) do
21
+ flunk "No implementation provided for #{name}"
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,80 @@
1
+ require 'helper'
2
+
3
+ class TestRackPush < Test::Unit::TestCase
4
+ include Rack::Test::Methods
5
+
6
+ def setup
7
+ TestRackPush.class_eval { def app; using_default_params; end }
8
+ Pusher.key = 'pusher_key'
9
+ Pusher.app_id = 'app_id'
10
+ Pusher.secret = 'secret'
11
+ end
12
+
13
+ def using_default_params
14
+ # default url for Rack::Push is '/pusher'
15
+ Rack::Push::App.new lambda { |env|
16
+ [200, {'Content-Length' => '10', 'Content-Type' => 'text/plain'}, ["NOT PUSHER"]]
17
+ }, :key => 'pusher_key', :app_id => 'app_id', :secret => 'secret'
18
+ end
19
+
20
+ def using_custom_params
21
+ Rack::Push::App.new lambda { |env|
22
+ [200, {'Content-Length' => '10', 'Content-Type' => 'text/plain'}, ["NOT PUSHER"]]
23
+ }, :key => 'pusher_key', :app_id => 'app_id', :secret => 'secret', :path => '/customized/path', :method => :put
24
+ end
25
+
26
+ test "bypassing Rack::Push" do
27
+ Pusher['my_channel'].expects(:trigger).never
28
+ get '/not_pusher'
29
+ assert last_response.ok?
30
+ assert_equal "NOT PUSHER", last_response.body
31
+ end
32
+
33
+ test "sending a request to pusher" do
34
+ mock_channel = mock('channel')
35
+ mock_channel.stubs(:trigger)
36
+ Pusher.stubs(:[]).returns(mock_channel)
37
+ Pusher['my_channel'].expects(:trigger).with('my_event', '{"title":"pushing"}', '123')
38
+ post '/pusher/my_channel?event=my_event&socket_id=123', '{"title":"pushing"}'
39
+ assert last_response.ok?
40
+ assert_not_equal "NOT PUSHER", last_response.body
41
+ end
42
+
43
+ test "ignoring wrong request methods" do
44
+ Pusher['my_channel'].expects(:trigger).never
45
+ put '/pusher/my_channel?event=my_event&socket_id=123', '{"title":"pushing"}'
46
+ assert last_response.ok?
47
+ assert_equal "NOT PUSHER", last_response.body
48
+ end
49
+
50
+ test "handling missing parameters" do
51
+ Pusher['my_channel'].expects(:trigger).never
52
+ post '/pusher/my_channel?event=my_event', '{"title":"pushing"}'
53
+ assert_equal 412, last_response.status
54
+ end
55
+
56
+ test "trying to push no data" do
57
+ Pusher['my_channel'].expects(:trigger).never
58
+ post '/pusher/my_channel?event=my_event&socket_id=123'
59
+ assert_equal 412, last_response.status
60
+ end
61
+
62
+ test "bypassing when using custom params" do
63
+ TestRackPush.class_eval { def app; using_custom_params; end }
64
+ Pusher['my_channel'].expects(:trigger).never
65
+ get '/not_pusher'
66
+ assert last_response.ok?
67
+ assert_equal "NOT PUSHER", last_response.body
68
+ end
69
+
70
+ test "sending a request to pusher when using custom params" do
71
+ TestRackPush.class_eval { def app; using_custom_params; end }
72
+ mock_channel = mock('channel')
73
+ mock_channel.stubs(:trigger)
74
+ Pusher.stubs(:[]).returns(mock_channel)
75
+ Pusher['my_channel'].expects(:trigger).with('my_event', '{"title":"pushing"}', '123')
76
+ put '/customized/path/my_channel?event=my_event&socket_id=123', '{"title":"pushing"}'
77
+ assert last_response.ok?
78
+ assert_not_equal "NOT PUSHER", last_response.body
79
+ end
80
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack_push
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Oliver Nightingale
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-04 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: rack interface for with pusher
22
+ email: oliver.n@new-bamboo.co.uk
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - LICENSE
29
+ - README.rdoc
30
+ files:
31
+ - .document
32
+ - .gitignore
33
+ - LICENSE
34
+ - README.rdoc
35
+ - Rakefile
36
+ - VERSION
37
+ - lib/rack_push.rb
38
+ - lib/rack_push/app.rb
39
+ - lib/rack_push/request.rb
40
+ - test/helper.rb
41
+ - test/test_rack_push.rb
42
+ has_rdoc: true
43
+ homepage: http://github.com/olivernn/rack_push
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --charset=UTF-8
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.3.6
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: rack interface for with pusher
72
+ test_files:
73
+ - test/helper.rb
74
+ - test/test_rack_push.rb