pusher 0.3.2 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +5 -3
- data/Rakefile +0 -1
- data/VERSION +1 -1
- data/lib/pusher.rb +20 -50
- data/lib/pusher/channel.rb +38 -0
- data/pusher.gemspec +3 -5
- data/spec/pusher_spec.rb +49 -53
- data/spec/spec_helper.rb +7 -2
- metadata +7 -18
data/README.md
CHANGED
@@ -4,17 +4,19 @@ Pusher gem
|
|
4
4
|
Getting started
|
5
5
|
---------------
|
6
6
|
|
7
|
-
|
7
|
+
After registering at <http://pusherapp.com> configure your app with the security credentials
|
8
8
|
|
9
9
|
Pusher.key = 'your-pusher-key'
|
10
|
-
Pusher.secret = 'your-pusher-
|
10
|
+
Pusher.secret = 'your-pusher-secret'
|
11
11
|
|
12
12
|
Trigger an event
|
13
13
|
|
14
14
|
Pusher['arbitrary-channel-name'].trigger({:some => 'data'})
|
15
15
|
|
16
16
|
Logging
|
17
|
-
|
17
|
+
-------
|
18
|
+
|
19
|
+
Errors are logged to `Pusher.logger`. It will by default use `Logger` from stdlib, however you can assign any logger:
|
18
20
|
|
19
21
|
Pusher.logger = Rails.logger
|
20
22
|
|
data/Rakefile
CHANGED
@@ -11,7 +11,6 @@ begin
|
|
11
11
|
gem.homepage = "http://github.com/newbamboo/pusher-gem"
|
12
12
|
gem.authors = ["New Bamboo"]
|
13
13
|
# gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
-
gem.add_dependency "rest-client"
|
15
14
|
gem.add_dependency "json"
|
16
15
|
gem.add_development_dependency "rspec", ">= 1.2.9"
|
17
16
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.4
|
data/lib/pusher.rb
CHANGED
@@ -1,68 +1,38 @@
|
|
1
1
|
require 'json'
|
2
2
|
require 'uri'
|
3
3
|
require 'net/http'
|
4
|
-
require 'logger'
|
5
4
|
|
6
|
-
|
7
|
-
|
5
|
+
autoload 'Logger', 'logger'
|
6
|
+
|
7
|
+
module Pusher
|
8
8
|
class ArgumentError < ::ArgumentError
|
9
9
|
def message
|
10
|
-
'You must configure both Pusher.key
|
10
|
+
'You must configure both Pusher.key in order to authenticate your Pusher app'
|
11
11
|
end
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
class << self
|
15
|
-
attr_accessor :host, :port
|
16
|
-
attr_writer :
|
15
|
+
attr_accessor :host, :port
|
16
|
+
attr_writer :logger
|
17
|
+
attr_accessor :key, :secret
|
18
|
+
|
19
|
+
def logger
|
20
|
+
@logger ||= begin
|
21
|
+
log = Logger.new(STDOUT)
|
22
|
+
log.level = Logger::INFO
|
23
|
+
log
|
24
|
+
end
|
25
|
+
end
|
17
26
|
end
|
18
27
|
|
19
|
-
self.host
|
20
|
-
self.port
|
21
|
-
self.logger = Logger.new($STDOUT)
|
28
|
+
self.host = 'api.pusherapp.com'
|
29
|
+
self.port = 80
|
22
30
|
|
23
31
|
def self.[](channel_id)
|
24
32
|
raise ArgumentError unless @key
|
25
33
|
@channels ||= {}
|
26
34
|
@channels[channel_id.to_s] = Channel.new(@key, channel_id)
|
27
35
|
end
|
28
|
-
|
29
|
-
class Channel
|
30
|
-
def initialize(key, id)
|
31
|
-
@uri = URI.parse("http://#{Pusher.host}:#{Pusher.port}/app/#{key}/channel/#{id}")
|
32
|
-
@http = Net::HTTP.new(@uri.host, @uri.port)
|
33
|
-
end
|
34
|
-
|
35
|
-
def trigger(event_name, data, socket_id = nil)
|
36
|
-
begin
|
37
|
-
@http.post(
|
38
|
-
@uri.path,
|
39
|
-
self.class.turn_into_json({
|
40
|
-
:event => event_name,
|
41
|
-
:data => data,
|
42
|
-
:socket_id => socket_id
|
43
|
-
}),
|
44
|
-
{'Content-Type'=> 'application/json'}
|
45
|
-
)
|
46
|
-
rescue StandardError => e
|
47
|
-
handle_error e
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def self.turn_into_json(data)
|
52
|
-
j = if Object.const_defined?('ActiveSupport')
|
53
|
-
data.to_json
|
54
|
-
else
|
55
|
-
JSON.generate(data)
|
56
|
-
end
|
57
|
-
j
|
58
|
-
end
|
59
|
-
|
60
|
-
private
|
61
|
-
|
62
|
-
def handle_error(e)
|
63
|
-
self.logger.error(e.backtrace.join("\n"))
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
67
|
-
|
68
36
|
end
|
37
|
+
|
38
|
+
require 'pusher/channel'
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Pusher
|
2
|
+
class Channel
|
3
|
+
def initialize(key, id)
|
4
|
+
@uri = URI.parse("http://#{Pusher.host}:#{Pusher.port}/app/#{key}/channel/#{id}")
|
5
|
+
@http = Net::HTTP.new(@uri.host, @uri.port)
|
6
|
+
end
|
7
|
+
|
8
|
+
def trigger(event_name, data, socket_id = nil)
|
9
|
+
begin
|
10
|
+
@http.post(
|
11
|
+
@uri.path,
|
12
|
+
self.class.turn_into_json({
|
13
|
+
:event => event_name,
|
14
|
+
:data => data,
|
15
|
+
:socket_id => socket_id
|
16
|
+
}),
|
17
|
+
{'Content-Type'=> 'application/json'}
|
18
|
+
)
|
19
|
+
rescue StandardError => e
|
20
|
+
handle_error e
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.turn_into_json(data)
|
25
|
+
if Object.const_defined?('ActiveSupport')
|
26
|
+
data.to_json
|
27
|
+
else
|
28
|
+
JSON.generate(data)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def handle_error(e)
|
35
|
+
self.logger.error(e.backtrace.join("\n"))
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/pusher.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{pusher}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["New Bamboo"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-04-14}
|
13
13
|
s.description = %q{Wrapper for pusherapp.com REST api}
|
14
14
|
s.email = %q{support@pusherapp.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
|
|
24
24
|
"Rakefile",
|
25
25
|
"VERSION",
|
26
26
|
"lib/pusher.rb",
|
27
|
+
"lib/pusher/channel.rb",
|
27
28
|
"pusher.gemspec",
|
28
29
|
"spec/pusher_spec.rb",
|
29
30
|
"spec/spec.opts",
|
@@ -44,16 +45,13 @@ Gem::Specification.new do |s|
|
|
44
45
|
s.specification_version = 3
|
45
46
|
|
46
47
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
47
|
-
s.add_runtime_dependency(%q<rest-client>, [">= 0"])
|
48
48
|
s.add_runtime_dependency(%q<json>, [">= 0"])
|
49
49
|
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
50
50
|
else
|
51
|
-
s.add_dependency(%q<rest-client>, [">= 0"])
|
52
51
|
s.add_dependency(%q<json>, [">= 0"])
|
53
52
|
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
54
53
|
end
|
55
54
|
else
|
56
|
-
s.add_dependency(%q<rest-client>, [">= 0"])
|
57
55
|
s.add_dependency(%q<json>, [">= 0"])
|
58
56
|
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
59
57
|
end
|
data/spec/pusher_spec.rb
CHANGED
@@ -1,92 +1,88 @@
|
|
1
|
-
require File.expand_path(
|
1
|
+
require File.expand_path('../spec_helper', __FILE__)
|
2
2
|
|
3
3
|
describe Pusher do
|
4
|
-
|
5
4
|
describe 'configuration' do
|
6
5
|
it 'should be preconfigured for api host' do
|
7
6
|
Pusher.host.should == 'api.pusherapp.com'
|
8
7
|
end
|
9
|
-
|
8
|
+
|
10
9
|
it 'should be preconfigured for pi port 80' do
|
11
10
|
Pusher.port.should == 80
|
12
11
|
end
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
it 'should raise exception' do
|
18
|
-
lambda {
|
19
|
-
Pusher['test-channel']
|
20
|
-
}.should raise_error(Pusher::ArgumentError)
|
21
|
-
end
|
12
|
+
|
13
|
+
it 'should use standard logger if no other logger if defined' do
|
14
|
+
Pusher.logger.debug('foo')
|
15
|
+
Pusher.logger.should be_kind_of(Logger)
|
22
16
|
end
|
23
17
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
18
|
+
it "can be configured to use any logger" do
|
19
|
+
logger = mock("ALogger")
|
20
|
+
logger.should_receive(:debug).with('foo')
|
21
|
+
Pusher.logger = logger
|
22
|
+
Pusher.logger.debug('foo')
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should raise exception if key and secret are missing' do
|
26
|
+
lambda {
|
27
|
+
Pusher['test-channel']
|
28
|
+
}.should raise_error(Pusher::ArgumentError)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should raise exception if key is missing' do
|
32
|
+
lambda {
|
33
|
+
Pusher['test-channel']
|
34
|
+
}.should raise_error(Pusher::ArgumentError)
|
30
35
|
end
|
31
36
|
end
|
32
|
-
|
33
|
-
|
37
|
+
|
34
38
|
describe 'configured' do
|
35
39
|
before do
|
36
40
|
Pusher.key = '12345678900000001'
|
37
41
|
Pusher.secret = '12345678900000001'
|
38
42
|
end
|
39
|
-
|
43
|
+
|
44
|
+
after do
|
45
|
+
Pusher.key = nil
|
46
|
+
Pusher.secret = nil
|
47
|
+
end
|
48
|
+
|
40
49
|
describe '.[]' do
|
41
50
|
before do
|
42
51
|
@channel = Pusher['test_channel']
|
43
52
|
end
|
44
|
-
|
53
|
+
|
45
54
|
it 'should return a new channel' do
|
46
55
|
@channel.should be_kind_of(Pusher::Channel)
|
47
56
|
end
|
48
57
|
end
|
49
|
-
|
50
|
-
describe 'calling Channel#trigger' do
|
51
58
|
|
59
|
+
describe 'Channel#trigger' do
|
52
60
|
before do
|
53
61
|
@http = mock('HTTP', :post => 'posting')
|
54
62
|
Net::HTTP.stub!(:new).and_return @http
|
55
63
|
end
|
56
64
|
|
57
65
|
it 'should configure HTTP library to talk to pusher API' do
|
58
|
-
Net::HTTP.should_receive(:new).
|
59
|
-
|
60
|
-
|
61
|
-
'Some data'
|
62
|
-
)
|
66
|
+
Net::HTTP.should_receive(:new).
|
67
|
+
with('api.pusherapp.com', 80).and_return @http
|
68
|
+
Pusher['test_channel'].trigger('new_event', 'Some data')
|
63
69
|
end
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
parsed['socket_id'].should == nil
|
75
|
-
args[2].should == {'Content-Type'=> 'application/json'}
|
76
|
-
end
|
77
|
-
Pusher['test_channel'].trigger(
|
78
|
-
'new_event',
|
79
|
-
:name => 'Pusher',
|
80
|
-
:last_name => 'App'
|
81
|
-
)
|
70
|
+
|
71
|
+
it 'should POST JSON to pusher API' do
|
72
|
+
@http.should_receive(:post) do |path, data, headers|
|
73
|
+
path.should == '/app/12345678900000001/channel/test_channel'
|
74
|
+
parsed = JSON.parse(data)
|
75
|
+
parsed['event'].should == 'new_event'
|
76
|
+
parsed['data']['name'].should == 'Pusher'
|
77
|
+
parsed['data']['last_name'].should == 'App'
|
78
|
+
parsed['socket_id'].should == nil
|
79
|
+
headers.should == {'Content-Type'=> 'application/json'}
|
82
80
|
end
|
81
|
+
Pusher['test_channel'].trigger('new_event', {
|
82
|
+
:name => 'Pusher',
|
83
|
+
:last_name => 'App'
|
84
|
+
})
|
83
85
|
end
|
84
86
|
end
|
85
|
-
|
86
|
-
end
|
87
|
-
|
88
|
-
after do
|
89
|
-
Pusher.key = nil
|
90
|
-
Pusher.secret = nil
|
91
87
|
end
|
92
88
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,8 +1,13 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
require 'spec'
|
4
|
+
require 'spec/autorun'
|
5
|
+
|
1
6
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
7
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
8
|
+
|
3
9
|
require 'pusher'
|
4
|
-
|
5
|
-
require 'spec/autorun'
|
10
|
+
|
6
11
|
Spec::Runner.configure do |config|
|
7
12
|
|
8
13
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 4
|
9
|
+
version: 0.3.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- New Bamboo
|
@@ -14,11 +14,11 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-04-14 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
|
-
name:
|
21
|
+
name: json
|
22
22
|
prerelease: false
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
@@ -29,22 +29,10 @@ dependencies:
|
|
29
29
|
version: "0"
|
30
30
|
type: :runtime
|
31
31
|
version_requirements: *id001
|
32
|
-
- !ruby/object:Gem::Dependency
|
33
|
-
name: json
|
34
|
-
prerelease: false
|
35
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
-
requirements:
|
37
|
-
- - ">="
|
38
|
-
- !ruby/object:Gem::Version
|
39
|
-
segments:
|
40
|
-
- 0
|
41
|
-
version: "0"
|
42
|
-
type: :runtime
|
43
|
-
version_requirements: *id002
|
44
32
|
- !ruby/object:Gem::Dependency
|
45
33
|
name: rspec
|
46
34
|
prerelease: false
|
47
|
-
requirement: &
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
48
36
|
requirements:
|
49
37
|
- - ">="
|
50
38
|
- !ruby/object:Gem::Version
|
@@ -54,7 +42,7 @@ dependencies:
|
|
54
42
|
- 9
|
55
43
|
version: 1.2.9
|
56
44
|
type: :development
|
57
|
-
version_requirements: *
|
45
|
+
version_requirements: *id002
|
58
46
|
description: Wrapper for pusherapp.com REST api
|
59
47
|
email: support@pusherapp.com
|
60
48
|
executables: []
|
@@ -72,6 +60,7 @@ files:
|
|
72
60
|
- Rakefile
|
73
61
|
- VERSION
|
74
62
|
- lib/pusher.rb
|
63
|
+
- lib/pusher/channel.rb
|
75
64
|
- pusher.gemspec
|
76
65
|
- spec/pusher_spec.rb
|
77
66
|
- spec/spec.opts
|