1
1
require 'base64'
2
-
3
2
require 'pusher-signature'
4
3
5
4
module Pusher
@@ -10,6 +9,11 @@ class Client
10
9
:keep_alive_timeout
11
10
12
11
## CONFIGURATION ##
12
+ DEFAULT_CONNECT_TIMEOUT = 5
13
+ DEFAULT_SEND_TIMEOUT = 5
14
+ DEFAULT_RECEIVE_TIMEOUT = 5
15
+ DEFAULT_KEEP_ALIVE_TIMEOUT = 30
16
+ DEFAULT_CLUSTER = "mt1"
13
17
14
18
# Loads the configuration from an url in the environment
15
19
def self . from_env ( key = 'PUSHER_URL' )
@@ -25,43 +29,31 @@ def self.from_url(url)
25
29
end
26
30
27
31
def initialize ( options = { } )
28
- default_options = {
29
- :scheme => 'http' ,
30
- :port => 80 ,
31
- }
32
+ @scheme = "http"
33
+ @port = options [ :port ] || 80
32
34
33
35
if options [ :use_tls ] || options [ :encrypted ]
34
- default_options [ : scheme] = "https"
35
- default_options [ :port ] = 443
36
+ @ scheme = "https"
37
+ @port = options [ :port ] || 443
36
38
end
37
39
38
- merged_options = default_options . merge ( options )
39
-
40
- if options . has_key? ( :host )
41
- merged_options [ :host ] = options [ :host ]
42
- elsif options . has_key? ( :cluster )
43
- merged_options [ :host ] = "api-#{ options [ :cluster ] } .pusher.com"
44
- else
45
- merged_options [ :host ] = "api.pusherapp.com"
46
- end
40
+ @app_id = options [ :app_id ]
41
+ @key = options [ :key ]
42
+ @secret = options [ :secret ]
47
43
48
- @scheme , @host , @port , @app_id , @key , @secret =
49
- merged_options . values_at (
50
- :scheme , :host , :port , :app_id , :key , :secret
51
- )
44
+ @host = options [ :host ]
45
+ @host ||= "api-#{ options [ :cluster ] } .pusher.com" unless options [ :cluster ] . nil? || options [ :cluster ] . empty?
46
+ @host ||= "api-#{ DEFAULT_CLUSTER } .pusher.com"
52
47
53
- if options . has_key? ( :encryption_master_key_base64 )
54
- @encryption_master_key =
55
- Base64 . strict_decode64 ( options [ :encryption_master_key_base64 ] )
56
- end
48
+ @encryption_master_key = Base64 . strict_decode64 ( options [ :encryption_master_key_base64 ] ) if options [ :encryption_master_key_base64 ]
57
49
58
50
@http_proxy = options [ :http_proxy ]
59
51
60
52
# Default timeouts
61
- @connect_timeout = 5
62
- @send_timeout = 5
63
- @receive_timeout = 5
64
- @keep_alive_timeout = 30
53
+ @connect_timeout = DEFAULT_CONNECT_TIMEOUT
54
+ @send_timeout = DEFAULT_SEND_TIMEOUT
55
+ @receive_timeout = DEFAULT_RECEIVE_TIMEOUT
56
+ @keep_alive_timeout = DEFAULT_KEEP_ALIVE_TIMEOUT
65
57
end
66
58
67
59
# @private Returns the authentication token for the client
@@ -75,10 +67,10 @@ def authentication_token
75
67
def url ( path = nil )
76
68
raise ConfigurationError , :app_id unless @app_id
77
69
URI ::Generic . build ( {
78
- : scheme => @scheme ,
79
- : host => @host ,
80
- : port => @port ,
81
- : path => "/apps/#{ @app_id } #{ path } "
70
+ scheme : @scheme ,
71
+ host : @host ,
72
+ port : @port ,
73
+ path : "/apps/#{ @app_id } #{ path } "
82
74
} )
83
75
end
84
76
@@ -102,13 +94,12 @@ def http_proxy=(http_proxy)
102
94
@http_proxy = http_proxy
103
95
uri = URI . parse ( http_proxy )
104
96
@proxy = {
105
- : scheme => uri . scheme ,
106
- : host => uri . host ,
107
- : port => uri . port ,
108
- : user => uri . user ,
109
- : password => uri . password
97
+ scheme : uri . scheme ,
98
+ host : uri . host ,
99
+ port : uri . port ,
100
+ user : uri . user ,
101
+ password : uri . password
110
102
}
111
- @http_proxy
112
103
end
113
104
114
105
# Configure whether Pusher API calls should be made over SSL
@@ -128,6 +119,8 @@ def encrypted?
128
119
end
129
120
130
121
def cluster = ( cluster )
122
+ cluster = DEFAULT_CLUSTER if cluster . nil? || cluster . empty?
123
+
131
124
@host = "api-#{ cluster } .pusher.com"
132
125
end
133
126
@@ -360,9 +353,9 @@ def authenticate(channel_name, socket_id, custom_data = nil)
360
353
361
354
# @private Construct a net/http http client
362
355
def sync_http_client
363
- @client ||= begin
364
- require 'httpclient'
356
+ require 'httpclient'
365
357
358
+ @client ||= begin
366
359
HTTPClient . new ( @http_proxy ) . tap do |c |
367
360
c . connect_timeout = @connect_timeout
368
361
c . send_timeout = @send_timeout
@@ -381,14 +374,14 @@ def em_http_client(uri)
381
374
require 'em-http' unless defined? ( EventMachine ::HttpRequest )
382
375
383
376
connection_opts = {
384
- : connect_timeout => @connect_timeout ,
385
- : inactivity_timeout => @receive_timeout ,
377
+ connect_timeout : @connect_timeout ,
378
+ inactivity_timeout : @receive_timeout ,
386
379
}
387
380
388
381
if defined? ( @proxy )
389
382
proxy_opts = {
390
- : host => @proxy [ :host ] ,
391
- : port => @proxy [ :port ]
383
+ host : @proxy [ :host ] ,
384
+ port : @proxy [ :port ]
392
385
}
393
386
if @proxy [ :user ]
394
387
proxy_opts [ :authorization ] = [ @proxy [ :user ] , @proxy [ :password ] ]
0 commit comments