pusher 1.3.1 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,28 +0,0 @@
1
- require 'rubygems'
2
- require 'pusher'
3
- require 'eventmachine'
4
- require 'em-http-request'
5
-
6
- # To get these values:
7
- # - Go to https://app.pusherapp.com/
8
- # - Click on Choose App.
9
- # - Click on one of your apps
10
- # - Click API Access
11
- Pusher.app_id = 'your_app_id'
12
- Pusher.key = 'your_key'
13
- Pusher.secret = 'your_secret'
14
-
15
-
16
- EM.run {
17
- deferrable = Pusher['test_channel'].trigger_async('my_event', 'hi')
18
-
19
- deferrable.callback { # called on success
20
- puts "Message sent successfully."
21
- EM.stop
22
- }
23
- deferrable.errback { |error| # called on error
24
- puts "Message could not be sent."
25
- puts error
26
- EM.stop
27
- }
28
- }
@@ -1,69 +0,0 @@
1
- module Pusher
2
- module NativeNotification
3
- class Client
4
- attr_reader :app_id, :host
5
-
6
- API_PREFIX = "server_api"
7
- API_VERSION = "v1"
8
-
9
- def initialize(app_id, host, scheme, pusher_client)
10
- @app_id = app_id
11
- @host = host
12
- @scheme = scheme
13
- @pusher_client = pusher_client
14
- end
15
-
16
- # Send a notification via the native notifications API
17
- def notify(interests, data = {})
18
- Request.new(
19
- @pusher_client,
20
- :post,
21
- url("/notifications"),
22
- {},
23
- payload(interests, data)
24
- ).send_sync
25
- end
26
-
27
- private
28
-
29
- # {
30
- # interests: [Array of interests],
31
- # apns: {
32
- # See https://pusher.com/docs/push_notifications/ios/server
33
- # },
34
- # gcm: {
35
- # See https://pusher.com/docs/push_notifications/android/server
36
- # }
37
- # }
38
- #
39
- # @raise [Pusher::Error] if the interests array is empty
40
- # @return [String]
41
- def payload(interests, data)
42
- interests = Array(interests).map(&:to_s)
43
-
44
- raise Pusher::Error, "Interests array must not be empty" if interests.length == 0
45
-
46
- data = deep_symbolize_keys!(data)
47
-
48
- data.merge!(interests: interests)
49
-
50
- MultiJson.encode(data)
51
- end
52
-
53
- def url(path = nil)
54
- URI.parse("#{@scheme}://#{@host}/#{API_PREFIX}/#{API_VERSION}/apps/#{@app_id}#{path}")
55
- end
56
-
57
- # Symbolize all keys in the hash recursively
58
- def deep_symbolize_keys!(hash)
59
- hash.keys.each do |k|
60
- ks = k.respond_to?(:to_sym) ? k.to_sym : k
61
- hash[ks] = hash.delete(k)
62
- deep_symbolize_keys!(hash[ks]) if hash[ks].kind_of?(Hash)
63
- end
64
-
65
- hash
66
- end
67
- end
68
- end
69
- end
data/pusher.gemspec DELETED
@@ -1,33 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- require File.expand_path('../lib/pusher/version', __FILE__)
4
-
5
- Gem::Specification.new do |s|
6
- s.name = "pusher"
7
- s.version = Pusher::VERSION
8
- s.platform = Gem::Platform::RUBY
9
- s.authors = ["Pusher"]
10
- s.email = ["support@pusher.com"]
11
- s.homepage = "http://github.com/pusher/pusher-http-ruby"
12
- s.summary = %q{Pusher API client}
13
- s.description = %q{Wrapper for pusher.com REST api}
14
- s.license = "MIT"
15
-
16
- s.add_dependency "multi_json", "~> 1.0"
17
- s.add_dependency 'pusher-signature', "~> 0.1.8"
18
- s.add_dependency "httpclient", "~> 2.7"
19
- s.add_dependency "jruby-openssl" if defined?(JRUBY_VERSION)
20
-
21
- s.add_development_dependency "rspec", "~> 3.0"
22
- s.add_development_dependency "webmock"
23
- s.add_development_dependency "em-http-request", "~> 1.1.0"
24
- s.add_development_dependency "addressable", "=2.4.0"
25
- s.add_development_dependency "rake", "~> 10.4.2"
26
- s.add_development_dependency "rack", "~> 1.6.4"
27
- s.add_development_dependency "json", "~> 1.8.3"
28
-
29
- s.files = `git ls-files`.split("\n")
30
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
31
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
32
- s.require_paths = ["lib"]
33
- end
data/spec/channel_spec.rb DELETED
@@ -1,168 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require 'spec_helper'
3
-
4
- describe Pusher::Channel do
5
- before do
6
- @client = Pusher::Client.new({
7
- :app_id => '20',
8
- :key => '12345678900000001',
9
- :secret => '12345678900000001',
10
- :host => 'api.pusherapp.com',
11
- :port => 80,
12
- })
13
- @channel = @client['test_channel']
14
- end
15
-
16
- let(:pusher_url_regexp) { %r{/apps/20/events} }
17
-
18
- def stub_post(status, body = nil)
19
- options = {:status => status}
20
- options.merge!({:body => body}) if body
21
-
22
- stub_request(:post, pusher_url_regexp).to_return(options)
23
- end
24
-
25
- def stub_post_to_raise(e)
26
- stub_request(:post, pusher_url_regexp).to_raise(e)
27
- end
28
-
29
- describe '#trigger!' do
30
- it "should use @client.trigger internally" do
31
- expect(@client).to receive(:trigger)
32
- @channel.trigger('new_event', 'Some data')
33
- end
34
- end
35
-
36
- describe '#trigger' do
37
- it "should log failure if error raised in http call" do
38
- stub_post_to_raise(HTTPClient::BadResponseError)
39
-
40
- expect(Pusher.logger).to receive(:error).with("Exception from WebMock (HTTPClient::BadResponseError) (Pusher::HTTPError)")
41
- expect(Pusher.logger).to receive(:debug) #backtrace
42
- channel = Pusher::Channel.new(@client.url, 'test_channel', @client)
43
- channel.trigger('new_event', 'Some data')
44
- end
45
-
46
- it "should log failure if Pusher returns an error response" do
47
- stub_post 401, "some signature info"
48
- expect(Pusher.logger).to receive(:error).with("some signature info (Pusher::AuthenticationError)")
49
- expect(Pusher.logger).to receive(:debug) #backtrace
50
- channel = Pusher::Channel.new(@client.url, 'test_channel', @client)
51
- channel.trigger('new_event', 'Some data')
52
- end
53
- end
54
-
55
- describe "#initialization" do
56
- it "should not be too long" do
57
- expect { @client['b'*201] }.to raise_error(Pusher::Error)
58
- end
59
-
60
- it "should not use bad characters" do
61
- expect { @client['*^!±`/""'] }.to raise_error(Pusher::Error)
62
- end
63
- end
64
-
65
- describe "#trigger_async" do
66
- it "should use @client.trigger_async internally" do
67
- expect(@client).to receive(:trigger_async)
68
- @channel.trigger_async('new_event', 'Some data')
69
- end
70
- end
71
-
72
- describe '#info' do
73
- it "should call the Client#channel_info" do
74
- expect(@client).to receive(:get).with("/channels/mychannel", anything)
75
- @channel = @client['mychannel']
76
- @channel.info
77
- end
78
-
79
- it "should assemble the requested attributes into the info option" do
80
- expect(@client).to receive(:get).with(anything, {
81
- :info => "user_count,connection_count"
82
- })
83
- @channel = @client['presence-foo']
84
- @channel.info(%w{user_count connection_count})
85
- end
86
- end
87
-
88
- describe '#users' do
89
- it "should call the Client#channel_users" do
90
- expect(@client).to receive(:get).with("/channels/presence-mychannel/users", {}).and_return({:users => {'id' => '4'}})
91
- @channel = @client['presence-mychannel']
92
- @channel.users
93
- end
94
- end
95
-
96
- describe "#authentication_string" do
97
- def authentication_string(*data)
98
- lambda { @channel.authentication_string(*data) }
99
- end
100
-
101
- it "should return an authentication string given a socket id" do
102
- auth = @channel.authentication_string('1.1')
103
-
104
- expect(auth).to eq('12345678900000001:02259dff9a2a3f71ea8ab29ac0c0c0ef7996c8f3fd3702be5533f30da7d7fed4')
105
- end
106
-
107
- it "should raise error if authentication is invalid" do
108
- [nil, ''].each do |invalid|
109
- expect(authentication_string(invalid)).to raise_error Pusher::Error
110
- end
111
- end
112
-
113
- describe 'with extra string argument' do
114
- it 'should be a string or nil' do
115
- expect(authentication_string('1.1', 123)).to raise_error Pusher::Error
116
- expect(authentication_string('1.1', {})).to raise_error Pusher::Error
117
-
118
- expect(authentication_string('1.1', 'boom')).not_to raise_error
119
- expect(authentication_string('1.1', nil)).not_to raise_error
120
- end
121
-
122
- it "should return an authentication string given a socket id and custom args" do
123
- auth = @channel.authentication_string('1.1', 'foobar')
124
-
125
- expect(auth).to eq("12345678900000001:#{hmac(@client.secret, "1.1:test_channel:foobar")}")
126
- end
127
- end
128
- end
129
-
130
- describe '#authenticate' do
131
- before :each do
132
- @custom_data = {:uid => 123, :info => {:name => 'Foo'}}
133
- end
134
-
135
- it 'should return a hash with signature including custom data and data as json string' do
136
- allow(MultiJson).to receive(:encode).with(@custom_data).and_return 'a json string'
137
-
138
- response = @channel.authenticate('1.1', @custom_data)
139
-
140
- expect(response).to eq({
141
- :auth => "12345678900000001:#{hmac(@client.secret, "1.1:test_channel:a json string")}",
142
- :channel_data => 'a json string'
143
- })
144
- end
145
-
146
- it 'should fail on invalid socket_ids' do
147
- expect {
148
- @channel.authenticate('1.1:')
149
- }.to raise_error Pusher::Error
150
-
151
- expect {
152
- @channel.authenticate('1.1foo', 'channel')
153
- }.to raise_error Pusher::Error
154
-
155
- expect {
156
- @channel.authenticate(':1.1')
157
- }.to raise_error Pusher::Error
158
-
159
- expect {
160
- @channel.authenticate('foo1.1', 'channel')
161
- }.to raise_error Pusher::Error
162
-
163
- expect {
164
- @channel.authenticate('foo', 'channel')
165
- }.to raise_error Pusher::Error
166
- end
167
- end
168
- end