pusher 1.3.2 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,170 +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)
75
- .with("/channels/mychannel", anything)
76
- .and_return({:occupied => true, :subscription_count => 12})
77
- @channel = @client['mychannel']
78
- @channel.info
79
- end
80
-
81
- it "should assemble the requested attributes into the info option" do
82
- expect(@client).to receive(:get)
83
- .with(anything, {:info => "user_count,connection_count"})
84
- .and_return({:occupied => true, :subscription_count => 12, :user_count => 12})
85
- @channel = @client['presence-foo']
86
- @channel.info(%w{user_count connection_count})
87
- end
88
- end
89
-
90
- describe '#users' do
91
- it "should call the Client#channel_users" do
92
- expect(@client).to receive(:get).with("/channels/presence-mychannel/users", {}).and_return({:users => {'id' => '4'}})
93
- @channel = @client['presence-mychannel']
94
- @channel.users
95
- end
96
- end
97
-
98
- describe "#authentication_string" do
99
- def authentication_string(*data)
100
- lambda { @channel.authentication_string(*data) }
101
- end
102
-
103
- it "should return an authentication string given a socket id" do
104
- auth = @channel.authentication_string('1.1')
105
-
106
- expect(auth).to eq('12345678900000001:02259dff9a2a3f71ea8ab29ac0c0c0ef7996c8f3fd3702be5533f30da7d7fed4')
107
- end
108
-
109
- it "should raise error if authentication is invalid" do
110
- [nil, ''].each do |invalid|
111
- expect(authentication_string(invalid)).to raise_error Pusher::Error
112
- end
113
- end
114
-
115
- describe 'with extra string argument' do
116
- it 'should be a string or nil' do
117
- expect(authentication_string('1.1', 123)).to raise_error Pusher::Error
118
- expect(authentication_string('1.1', {})).to raise_error Pusher::Error
119
-
120
- expect(authentication_string('1.1', 'boom')).not_to raise_error
121
- expect(authentication_string('1.1', nil)).not_to raise_error
122
- end
123
-
124
- it "should return an authentication string given a socket id and custom args" do
125
- auth = @channel.authentication_string('1.1', 'foobar')
126
-
127
- expect(auth).to eq("12345678900000001:#{hmac(@client.secret, "1.1:test_channel:foobar")}")
128
- end
129
- end
130
- end
131
-
132
- describe '#authenticate' do
133
- before :each do
134
- @custom_data = {:uid => 123, :info => {:name => 'Foo'}}
135
- end
136
-
137
- it 'should return a hash with signature including custom data and data as json string' do
138
- allow(MultiJson).to receive(:encode).with(@custom_data).and_return 'a json string'
139
-
140
- response = @channel.authenticate('1.1', @custom_data)
141
-
142
- expect(response).to eq({
143
- :auth => "12345678900000001:#{hmac(@client.secret, "1.1:test_channel:a json string")}",
144
- :channel_data => 'a json string'
145
- })
146
- end
147
-
148
- it 'should fail on invalid socket_ids' do
149
- expect {
150
- @channel.authenticate('1.1:')
151
- }.to raise_error Pusher::Error
152
-
153
- expect {
154
- @channel.authenticate('1.1foo', 'channel')
155
- }.to raise_error Pusher::Error
156
-
157
- expect {
158
- @channel.authenticate(':1.1')
159
- }.to raise_error Pusher::Error
160
-
161
- expect {
162
- @channel.authenticate('foo1.1', 'channel')
163
- }.to raise_error Pusher::Error
164
-
165
- expect {
166
- @channel.authenticate('foo', 'channel')
167
- }.to raise_error Pusher::Error
168
- end
169
- end
170
- end