Skip to content

Commit 88640f6

Browse files
committed
Merge pull request #95 from pusher/94-support-cluster-option
support cluster option
2 parents 7c7c9c3 + 32b2645 commit 88640f6

File tree

4 files changed

+118
-40
lines changed

4 files changed

+118
-40
lines changed

README.md

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,46 @@ gem install pusher
1919

2020
After registering at <http://pusher.com> configure your app with the security credentials.
2121

22-
### Global
22+
### Instantiating a Pusher client
23+
24+
Creating a new Pusher `client` can be done as follows.
25+
26+
``` ruby
27+
pusher_client = Pusher::Client.new(
28+
app_id: 'your-pusher-app-id',
29+
key: 'your-pusher-key',
30+
secret: 'your-pusher-secret'
31+
)
32+
```
33+
34+
If you want to set a custom `host` value for your client then you can do so when instantiating a Pusher client like so:
35+
36+
``` ruby
37+
pusher_client = Pusher::Client.new(
38+
app_id: 'your-pusher-app-id',
39+
key: 'your-pusher-key',
40+
secret: 'your-pusher-secret',
41+
host: 'your-pusher-host'
42+
)
43+
```
44+
45+
If you created your app in a different cluster to the default cluster, you must pass the `cluster` option as follows:
46+
47+
``` ruby
48+
pusher_client = Pusher::Client.new(
49+
app_id: 'your-pusher-app-id',
50+
key: 'your-pusher-key',
51+
secret: 'your-pusher-secret',
52+
cluster: 'your-app-cluster'
53+
)
54+
```
55+
56+
This will set the `host` to `api-<cluster>.pusher.com`. If you pass both `host` and `cluster` options, the `host` will take precendence and `cluster` will be ignored.
57+
58+
### Global (Deprecated)
2359

24-
The most standard way of configuring Pusher is to do it globally on the Pusher class.
60+
Configuring Pusher can also be done globally on the Pusher class.
61+
*NOTE! This is a deprecated feature and will be removed in future versions of this library!*
2562

2663
``` ruby
2764
Pusher.app_id = 'your-pusher-app-id'
@@ -49,33 +86,6 @@ As of version 0.12, SSL certificates are verified when using the synchronous htt
4986
Pusher.default_client.sync_http_client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
5087
```
5188

52-
### Instantiating a Pusher client
53-
54-
Sometimes you may have multiple sets of API keys, or want different configuration in different parts of your application. In these scenarios, a pusher `client` may be configured:
55-
56-
``` ruby
57-
pusher_client = Pusher::Client.new({
58-
app_id: 'your-pusher-app-id',
59-
key: 'your-pusher-key',
60-
secret: 'your-pusher-secret'
61-
})
62-
```
63-
64-
This `client` will have all the functionality listed on the main Pusher class (which proxies to a client internally).
65-
66-
If you want to set the `host` value for your client then you can do so when instantiating a Pusher client like so:
67-
68-
``` ruby
69-
pusher_client = Pusher::Client.new({
70-
app_id: 'your-pusher-app-id',
71-
key: 'your-pusher-key',
72-
secret: 'your-pusher-secret',
73-
host: 'your-pusher-host'
74-
})
75-
```
76-
77-
This is useful if, for example, you've created an app on the EU cluster and wish to set the host to be `api-eu.pusher.com`.
78-
7989
## Interacting with the Pusher service
8090

8191
The Pusher gem contains a number of helpers for interacting with the service. As a general rule, the library adheres to a set of conventions that we have aimed to make universal.
@@ -86,7 +96,7 @@ Handle errors by rescuing `Pusher::Error` (all errors are descendants of this er
8696

8797
``` ruby
8898
begin
89-
Pusher.trigger('a_channel', 'an_event', {:some => 'data'})
99+
Pusher.trigger('a_channel', 'an_event', :some => 'data')
90100
rescue Pusher::Error => e
91101
# (Pusher::AuthenticationError, Pusher::HTTPError, or Pusher::Error)
92102
end
@@ -105,8 +115,8 @@ Pusher.logger = Rails.logger
105115
An event can be published to one or more channels (limited to 10) in one API call:
106116

107117
``` ruby
108-
Pusher.trigger('channel', 'event', {foo: 'bar'})
109-
Pusher.trigger(['channel_1', 'channel_2'], 'event_name', {foo: 'bar'})
118+
Pusher.trigger('channel', 'event', foo: 'bar')
119+
Pusher.trigger(['channel_1', 'channel_2'], 'event_name', foo: 'bar')
110120
```
111121

112122
An optional fourth argument may be used to send additional parameters to the API, for example to [exclude a single connection from receiving the event](http://pusher.com/docs/publisher_api_guide/publisher_excluding_recipients).
@@ -120,7 +130,7 @@ Pusher.trigger('channel', 'event', {foo: 'bar'}, {socket_id: '123.456'})
120130
Most examples and documentation will refer to the following syntax for triggering an event:
121131

122132
``` ruby
123-
Pusher['a_channel'].trigger('an_event', {:some => 'data'})
133+
Pusher['a_channel'].trigger('an_event', :some => 'data')
124134
```
125135

126136
This will continue to work, but has been replaced by `Pusher.trigger` which supports one or multiple channels.
@@ -193,10 +203,10 @@ Pusher.authenticate('private-my_channel', params[:socket_id])
193203
These work in a very similar way, but require a unique identifier for the user being authenticated, and optionally some attributes that are provided to clients via presence events:
194204

195205
``` ruby
196-
Pusher.authenticate('presence-my_channel', params[:socket_id], {
206+
Pusher.authenticate('presence-my_channel', params[:socket_id],
197207
user_id: 'user_id',
198208
user_info: {} # optional
199-
})
209+
)
200210
```
201211

202212
## Receiving WebHooks

lib/pusher.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class << self
2727
def_delegators :default_client, :scheme=, :host=, :port=, :app_id=, :key=, :secret=, :http_proxy=
2828

2929
def_delegators :default_client, :authentication_token, :url
30-
def_delegators :default_client, :encrypted=, :url=
30+
def_delegators :default_client, :encrypted=, :url=, :cluster=
3131
def_delegators :default_client, :timeout=, :connect_timeout=, :send_timeout=, :receive_timeout=, :keep_alive_timeout=
3232

3333
def_delegators :default_client, :get, :get_async, :post, :post_async

lib/pusher/client.rb

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,24 @@ class Client
1010
## CONFIGURATION ##
1111

1212
def initialize(options = {})
13-
options = {
13+
default_options = {
1414
:scheme => 'http',
15-
:host => 'api.pusherapp.com',
1615
:port => 80,
17-
}.merge(options)
18-
@scheme, @host, @port, @app_id, @key, @secret = options.values_at(
16+
}
17+
merged_options = default_options.merge(options)
18+
19+
if options.has_key?(:host)
20+
merged_options[:host] = options[:host]
21+
elsif options.has_key?(:cluster)
22+
merged_options[:host] = "api-#{options[:cluster]}.pusher.com"
23+
else
24+
merged_options[:host] = "api.pusherapp.com"
25+
end
26+
27+
@scheme, @host, @port, @app_id, @key, @secret = merged_options.values_at(
1928
:scheme, :host, :port, :app_id, :key, :secret
2029
)
30+
2131
@http_proxy = nil
2232
self.http_proxy = options[:http_proxy] if options[:http_proxy]
2333

@@ -88,6 +98,10 @@ def encrypted?
8898
@scheme == 'https'
8999
end
90100

101+
def cluster=(cluster)
102+
@host = "api-#{cluster}.pusher.com"
103+
end
104+
91105
# Convenience method to set all timeouts to the same value (in seconds).
92106
# For more control, use the individual writers.
93107
def timeout=(value)

spec/client_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,38 @@
6060
end
6161
end
6262

63+
describe 'configuring the cluster' do
64+
it 'should set a new default host' do
65+
@client.cluster = 'eu'
66+
expect(@client.host).to eq('api-eu.pusher.com')
67+
end
68+
69+
it 'should be overridden by host if it comes after' do
70+
@client.cluster = 'eu'
71+
@client.host = 'api.staging.pusher.com'
72+
expect(@client.host).to eq('api.staging.pusher.com')
73+
end
74+
75+
it 'should be overridden by url if it comes after' do
76+
@client.cluster = 'eu'
77+
@client.url = "http://somekey:somesecret@api.staging.pusherapp.com:8080/apps/87"
78+
79+
expect(@client.host).to eq('api.staging.pusherapp.com')
80+
end
81+
82+
it 'should get override the url configuration if it comes after' do
83+
@client.url = "http://somekey:somesecret@api.staging.pusherapp.com:8080/apps/87"
84+
@client.cluster = 'eu'
85+
expect(@client.host).to eq('api-eu.pusher.com')
86+
end
87+
88+
it 'should overrie by the host configuration if it comes after' do
89+
@client.host = 'api.staging.pusher.com'
90+
@client.cluster = 'eu'
91+
expect(@client.host).to eq('api-eu.pusher.com')
92+
end
93+
end
94+
6395
describe 'configuring a http proxy' do
6496
it "should be possible to configure everything by setting the http_proxy" do
6597
@client.http_proxy = 'http://someuser:somepassword@proxy.host.com:8080'
@@ -431,4 +463,26 @@
431463
end
432464
end
433465
end
466+
467+
describe 'configuring cluster' do
468+
it 'should allow clients to specify the cluster only with the default host' do
469+
client = Pusher::Client.new({
470+
:scheme => 'http',
471+
:cluster => 'eu',
472+
:port => 80
473+
})
474+
expect(client.host).to eq('api-eu.pusher.com')
475+
end
476+
477+
it 'should always have host override any supplied cluster value' do
478+
client = Pusher::Client.new({
479+
:scheme => 'http',
480+
:host => 'api.staging.pusherapp.com',
481+
:cluster => 'eu',
482+
:port => 80
483+
})
484+
expect(client.host).to eq('api.staging.pusherapp.com')
485+
end
486+
end
434487
end
488+

0 commit comments

Comments
 (0)