11require 'base64'
2-
32require 'pusher-signature'
43
54module Pusher
@@ -10,6 +9,11 @@ class Client
109 :keep_alive_timeout
1110
1211 ## 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"
1317
1418 # Loads the configuration from an url in the environment
1519 def self . from_env ( key = 'PUSHER_URL' )
@@ -25,43 +29,31 @@ def self.from_url(url)
2529 end
2630
2731 def initialize ( options = { } )
28- default_options = {
29- :scheme => 'http' ,
30- :port => 80 ,
31- }
32+ @scheme = "http"
33+ @port = options [ :port ] || 80
3234
3335 if options [ :use_tls ] || options [ :encrypted ]
34- default_options [ : scheme] = "https"
35- default_options [ :port ] = 443
36+ @ scheme = "https"
37+ @port = options [ :port ] || 443
3638 end
3739
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 ]
4743
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"
5247
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 ]
5749
5850 @http_proxy = options [ :http_proxy ]
5951
6052 # 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
6557 end
6658
6759 # @private Returns the authentication token for the client
@@ -75,10 +67,10 @@ def authentication_token
7567 def url ( path = nil )
7668 raise ConfigurationError , :app_id unless @app_id
7769 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 } "
8274 } )
8375 end
8476
@@ -102,13 +94,12 @@ def http_proxy=(http_proxy)
10294 @http_proxy = http_proxy
10395 uri = URI . parse ( http_proxy )
10496 @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
110102 }
111- @http_proxy
112103 end
113104
114105 # Configure whether Pusher API calls should be made over SSL
@@ -128,6 +119,8 @@ def encrypted?
128119 end
129120
130121 def cluster = ( cluster )
122+ cluster = DEFAULT_CLUSTER if cluster . nil? || cluster . empty?
123+
131124 @host = "api-#{ cluster } .pusher.com"
132125 end
133126
@@ -360,9 +353,9 @@ def authenticate(channel_name, socket_id, custom_data = nil)
360353
361354 # @private Construct a net/http http client
362355 def sync_http_client
363- @client ||= begin
364- require 'httpclient'
356+ require 'httpclient'
365357
358+ @client ||= begin
366359 HTTPClient . new ( @http_proxy ) . tap do |c |
367360 c . connect_timeout = @connect_timeout
368361 c . send_timeout = @send_timeout
@@ -381,14 +374,14 @@ def em_http_client(uri)
381374 require 'em-http' unless defined? ( EventMachine ::HttpRequest )
382375
383376 connection_opts = {
384- : connect_timeout => @connect_timeout ,
385- : inactivity_timeout => @receive_timeout ,
377+ connect_timeout : @connect_timeout ,
378+ inactivity_timeout : @receive_timeout ,
386379 }
387380
388381 if defined? ( @proxy )
389382 proxy_opts = {
390- : host => @proxy [ :host ] ,
391- : port => @proxy [ :port ]
383+ host : @proxy [ :host ] ,
384+ port : @proxy [ :port ]
392385 }
393386 if @proxy [ :user ]
394387 proxy_opts [ :authorization ] = [ @proxy [ :user ] , @proxy [ :password ] ]
0 commit comments