Skip to content

Commit 8f3c4fa

Browse files
committed
Introduce Pusher::Client.from_env
This allows to have a counterpart to the Pusher singleton when using an environment variable for config.
1 parent 8886b4f commit 8f3c4fa

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ pusher_client = Pusher::Client.new(
5555

5656
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.
5757

58+
Finally, if you have the configuration set in an `PUSHER_URL` environment
59+
variable, you can use:
60+
61+
``` ruby
62+
pusher_client = Pusher::Client.from_env
63+
```
64+
5865
### Global
5966

6067
Configuring Pusher can also be done globally on the Pusher class.

lib/pusher/client.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ class Client
99

1010
## CONFIGURATION ##
1111

12+
# Loads the configuration from an url in the environment
13+
def self.from_env(key = 'PUSHER_URL')
14+
url = ENV[key] || raise(ConfigurationError, key)
15+
client = new
16+
client.url = url
17+
client
18+
end
19+
1220
def initialize(options = {})
1321
default_options = {
1422
:scheme => 'http',

spec/client_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
@client.url
6666
}.to raise_error(Pusher::ConfigurationError)
6767
end
68+
6869
end
6970

7071
describe 'configuring the cluster' do
@@ -107,6 +108,20 @@
107108
end
108109
end
109110

111+
describe 'configuring from env' do
112+
it "works" do
113+
url = "http://somekey:somesecret@api.staging.pusherapp.com:8080/apps/87"
114+
ENV['PUSHER_URL'] = url
115+
116+
client = Pusher::Client.from_env
117+
expect(client.key).to eq("somekey")
118+
expect(client.secret).to eq("somesecret")
119+
expect(client.app_id).to eq("87")
120+
expect(client.url.to_s).to eq("http://api.staging.pusherapp.com:8080/apps/87")
121+
ENV['PUSHER_URL'] = nil
122+
end
123+
end
124+
110125
describe 'when configured' do
111126
before :each do
112127
@client.app_id = '20'

0 commit comments

Comments
 (0)