Class: Pusher::Client
- Inherits:
- 
      Object
      
        - Object
- Pusher::Client
 
- Defined in:
- lib/pusher/client.rb
Instance Attribute Summary collapse
- 
  
    
      #app_id  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Returns the value of attribute app_id. 
- 
  
    
      #connect_timeout  ⇒ Object 
    
    
  
  
  
  
    
    
      writeonly
    
  
  
  
  
  
  
    Sets the attribute connect_timeout. 
- 
  
    
      #host  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Returns the value of attribute host. 
- 
  
    
      #http_proxy  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Returns the value of attribute http_proxy. 
- 
  
    
      #keep_alive_timeout  ⇒ Object 
    
    
  
  
  
  
    
    
      writeonly
    
  
  
  
  
  
  
    Sets the attribute keep_alive_timeout. 
- 
  
    
      #key  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Returns the value of attribute key. 
- 
  
    
      #port  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Returns the value of attribute port. 
- 
  
    
      #proxy  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute proxy. 
- 
  
    
      #receive_timeout  ⇒ Object 
    
    
  
  
  
  
    
    
      writeonly
    
  
  
  
  
  
  
    Sets the attribute receive_timeout. 
- 
  
    
      #scheme  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Returns the value of attribute scheme. 
- 
  
    
      #secret  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Returns the value of attribute secret. 
- 
  
    
      #send_timeout  ⇒ Object 
    
    
  
  
  
  
    
    
      writeonly
    
  
  
  
  
  
  
    Sets the attribute send_timeout. 
Class Method Summary collapse
- 
  
    
      .from_env(key = 'PUSHER_URL')  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Loads the configuration from an url in the environment. 
Instance Method Summary collapse
- 
  
    
      #authenticate(channel_name, socket_id, custom_data = nil)  ⇒ Hash 
    
    
  
  
  
  
  
  
  
  
  
    Generate the expected response for an authentication endpoint. 
- #authentication_token ⇒ Object
- 
  
    
      #channel(channel_name)  ⇒ Channel 
    
    
      (also: #[])
    
  
  
  
  
  
  
  
  
  
    Return a convenience channel object by name that delegates operations on a channel. 
- 
  
    
      #channel_info(channel_name, params = {})  ⇒ Hash 
    
    
  
  
  
  
  
  
  
  
  
    Request info for a specific channel. 
- 
  
    
      #channel_users(channel_name, params = {})  ⇒ Hash 
    
    
  
  
  
  
  
  
  
  
  
    Request info for users of a presence channel. 
- 
  
    
      #channels(params = {})  ⇒ Hash 
    
    
  
  
  
  
  
  
  
  
  
    Request a list of occupied channels from the API. 
- #cluster=(cluster) ⇒ Object
- #em_http_client(uri) ⇒ Object
- 
  
    
      #encrypted=(boolean)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Configure whether Pusher API calls should be made over SSL (default false). 
- #encrypted? ⇒ Boolean
- 
  
    
      #get(path, params = {})  ⇒ Hash 
    
    
  
  
  
  
  
  
  
  
  
    GET arbitrary REST API resource using a synchronous http client. 
- 
  
    
      #get_async(path, params = {})  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    GET arbitrary REST API resource using an asynchronous http client. 
- 
  
    
      #initialize(options = {})  ⇒ Client 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of Client. 
- 
  
    
      #post(path, params = {})  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    POST arbitrary REST API resource using a synchronous http client. 
- 
  
    
      #post_async(path, params = {})  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    POST arbitrary REST API resource using an asynchronous http client. 
- 
  
    
      #resource(path)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    INTERACE WITH THE API ##. 
- #sync_http_client ⇒ Object
- 
  
    
      #timeout=(value)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Convenience method to set all timeouts to the same value (in seconds). 
- 
  
    
      #trigger(channels, event_name, data, params = {})  ⇒ Hash 
    
    
  
  
  
  
  
  
  
  
  
    Trigger an event on one or more channels. 
- 
  
    
      #trigger_async(channels, event_name, data, params = {})  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Trigger an event on one or more channels asynchronously. 
- 
  
    
      #trigger_batch(*events)  ⇒ Hash 
    
    
  
  
  
  
  
  
  
  
  
    Trigger multiple events at the same time. 
- 
  
    
      #trigger_batch_async(*events)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Trigger multiple events asynchronously. 
- #url(path = nil) ⇒ Object
- 
  
    
      #url=(url)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Configure Pusher connection by providing a url rather than specifying scheme, key, secret, and app_id separately. 
- 
  
    
      #webhook(request)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Convenience method for creating a new WebHook instance for validating and extracting info from a received WebHook. 
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
| 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | # File 'lib/pusher/client.rb', line 20 def initialize( = {}) = { :scheme => 'http', :port => 80, } = .merge() if .has_key?(:host) [:host] = [:host] elsif .has_key?(:cluster) [:host] = "api-#{[:cluster]}.pusher.com" else [:host] = "api.pusherapp.com" end @scheme, @host, @port, @app_id, @key, @secret = .values_at( :scheme, :host, :port, :app_id, :key, :secret ) @http_proxy = nil self.http_proxy = [:http_proxy] if [:http_proxy] # Default timeouts @connect_timeout = 5 @send_timeout = 5 @receive_timeout = 5 @keep_alive_timeout = 30 end | 
Instance Attribute Details
#app_id ⇒ Object
Returns the value of attribute app_id.
| 5 6 7 | # File 'lib/pusher/client.rb', line 5 def app_id @app_id end | 
#connect_timeout=(value) ⇒ Object (writeonly)
Sets the attribute connect_timeout
| 7 8 9 | # File 'lib/pusher/client.rb', line 7 def connect_timeout=(value) @connect_timeout = value end | 
#host ⇒ Object
Returns the value of attribute host.
| 5 6 7 | # File 'lib/pusher/client.rb', line 5 def host @host end | 
#http_proxy ⇒ Object
Returns the value of attribute http_proxy.
| 6 7 8 | # File 'lib/pusher/client.rb', line 6 def http_proxy @http_proxy end | 
#keep_alive_timeout=(value) ⇒ Object (writeonly)
Sets the attribute keep_alive_timeout
| 7 8 9 | # File 'lib/pusher/client.rb', line 7 def keep_alive_timeout=(value) @keep_alive_timeout = value end | 
#key ⇒ Object
Returns the value of attribute key.
| 5 6 7 | # File 'lib/pusher/client.rb', line 5 def key @key end | 
#port ⇒ Object
Returns the value of attribute port.
| 5 6 7 | # File 'lib/pusher/client.rb', line 5 def port @port end | 
#proxy ⇒ Object (readonly)
Returns the value of attribute proxy.
| 6 7 8 | # File 'lib/pusher/client.rb', line 6 def proxy @proxy end | 
#receive_timeout=(value) ⇒ Object (writeonly)
Sets the attribute receive_timeout
| 7 8 9 | # File 'lib/pusher/client.rb', line 7 def receive_timeout=(value) @receive_timeout = value end | 
#scheme ⇒ Object
Returns the value of attribute scheme.
| 5 6 7 | # File 'lib/pusher/client.rb', line 5 def scheme @scheme end | 
#secret ⇒ Object
Returns the value of attribute secret.
| 5 6 7 | # File 'lib/pusher/client.rb', line 5 def secret @secret end | 
#send_timeout=(value) ⇒ Object (writeonly)
Sets the attribute send_timeout
| 7 8 9 | # File 'lib/pusher/client.rb', line 7 def send_timeout=(value) @send_timeout = value end | 
Class Method Details
.from_env(key = 'PUSHER_URL') ⇒ Object
Loads the configuration from an url in the environment
| 13 14 15 16 17 18 | # File 'lib/pusher/client.rb', line 13 def self.from_env(key = 'PUSHER_URL') url = ENV[key] || raise(ConfigurationError, key) client = new client.url = url client end | 
Instance Method Details
#authenticate(channel_name, socket_id, custom_data = nil) ⇒ Hash
Generate the expected response for an authentication endpoint. See pusher.com/docs/authenticating_users for details.
| 323 324 325 326 | # File 'lib/pusher/client.rb', line 323 def authenticate(channel_name, socket_id, custom_data = nil) channel_instance = channel(channel_name) channel_instance.authenticate(socket_id, custom_data) end | 
#authentication_token ⇒ Object
| 50 51 52 53 54 | # File 'lib/pusher/client.rb', line 50 def authentication_token raise ConfigurationError, :key unless @key raise ConfigurationError, :secret unless @secret Pusher::Signature::Token.new(@key, @secret) end | 
#channel(channel_name) ⇒ Channel Also known as: []
Return a convenience channel object by name that delegates operations on a channel. No API request is made.
| 200 201 202 | # File 'lib/pusher/client.rb', line 200 def channel(channel_name) Channel.new(nil, channel_name, self) end | 
#channel_info(channel_name, params = {}) ⇒ Hash
| 233 234 235 | # File 'lib/pusher/client.rb', line 233 def channel_info(channel_name, params = {}) get("/channels/#{channel_name}", params) end | 
#channel_users(channel_name, params = {}) ⇒ Hash
| 249 250 251 | # File 'lib/pusher/client.rb', line 249 def channel_users(channel_name, params = {}) get("/channels/#{channel_name}/users", params) end | 
#channels(params = {}) ⇒ Hash
Request a list of occupied channels from the API
GET /apps//channels
| 217 218 219 | # File 'lib/pusher/client.rb', line 217 def channels(params = {}) get('/channels', params) end | 
#cluster=(cluster) ⇒ Object
| 112 113 114 | # File 'lib/pusher/client.rb', line 112 def cluster=(cluster) @host = "api-#{cluster}.pusher.com" end | 
#em_http_client(uri) ⇒ Object
| 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | # File 'lib/pusher/client.rb', line 343 def em_http_client(uri) begin unless defined?(EventMachine) && EventMachine.reactor_running? raise Error, "In order to use async calling you must be running inside an eventmachine loop" end require 'em-http' unless defined?(EventMachine::HttpRequest) connection_opts = { :connect_timeout => @connect_timeout, :inactivity_timeout => @receive_timeout, } if defined?(@proxy) proxy_opts = { :host => @proxy[:host], :port => @proxy[:port] } if @proxy[:user] proxy_opts[:authorization] = [@proxy[:user], @proxy[:password]] end connection_opts[:proxy] = proxy_opts end EventMachine::HttpRequest.new(uri, connection_opts) end end | 
#encrypted=(boolean) ⇒ Object
Configure whether Pusher API calls should be made over SSL (default false)
| 102 103 104 105 106 | # File 'lib/pusher/client.rb', line 102 def encrypted=(boolean) @scheme = boolean ? 'https' : 'http' # Configure port if it hasn't already been configured @port = boolean ? 443 : 80 end | 
#encrypted? ⇒ Boolean
| 108 109 110 | # File 'lib/pusher/client.rb', line 108 def encrypted? @scheme == 'https' end | 
#get(path, params = {}) ⇒ Hash
GET arbitrary REST API resource using a synchronous http client. All request signing is handled automatically.
| 146 147 148 | # File 'lib/pusher/client.rb', line 146 def get(path, params = {}) resource(path).get(params) end | 
#get_async(path, params = {}) ⇒ Object
GET arbitrary REST API resource using an asynchronous http client. All request signing is handled automatically.
When the eventmachine reactor is running, the em-http-request gem is used; otherwise an async request is made using httpclient. See README for details and examples.
| 162 163 164 | # File 'lib/pusher/client.rb', line 162 def get_async(path, params = {}) resource(path).get_async(params) end | 
#post(path, params = {}) ⇒ Object
POST arbitrary REST API resource using a synchronous http client. Works identially to get method, but posts params as JSON in post body.
| 168 169 170 | # File 'lib/pusher/client.rb', line 168 def post(path, params = {}) resource(path).post(params) end | 
#post_async(path, params = {}) ⇒ Object
POST arbitrary REST API resource using an asynchronous http client. Works identially to get_async method, but posts params as JSON in post body.
| 175 176 177 | # File 'lib/pusher/client.rb', line 175 def post_async(path, params = {}) resource(path).post_async(params) end | 
#resource(path) ⇒ Object
INTERACE WITH THE API ##
| 124 125 126 | # File 'lib/pusher/client.rb', line 124 def resource(path) Resource.new(self, path) end | 
#sync_http_client ⇒ Object
| 329 330 331 332 333 334 335 336 337 338 339 340 | # File 'lib/pusher/client.rb', line 329 def sync_http_client @client ||= begin require 'httpclient' HTTPClient.new(@http_proxy).tap do |c| c.connect_timeout = @connect_timeout c.send_timeout = @send_timeout c.receive_timeout = @receive_timeout c.keep_alive_timeout = @keep_alive_timeout end end end | 
#timeout=(value) ⇒ Object
Convenience method to set all timeouts to the same value (in seconds). For more control, use the individual writers.
| 118 119 120 | # File 'lib/pusher/client.rb', line 118 def timeout=(value) @connect_timeout, @send_timeout, @receive_timeout = value, value, value end | 
#trigger(channels, event_name, data, params = {}) ⇒ Hash
Trigger an event on one or more channels
POST /apps//events
| 268 269 270 | # File 'lib/pusher/client.rb', line 268 def trigger(channels, event_name, data, params = {}) post('/events', trigger_params(channels, event_name, data, params)) end | 
#trigger_async(channels, event_name, data, params = {}) ⇒ Object
Trigger an event on one or more channels asynchronously. For parameters see #trigger
| 290 291 292 | # File 'lib/pusher/client.rb', line 290 def trigger_async(channels, event_name, data, params = {}) post_async('/events', trigger_params(channels, event_name, data, params)) end | 
#trigger_batch(*events) ⇒ Hash
Trigger multiple events at the same time
POST /apps//batch_events
| 283 284 285 | # File 'lib/pusher/client.rb', line 283 def trigger_batch(*events) post('/batch_events', trigger_batch_params(events.flatten)) end | 
#trigger_batch_async(*events) ⇒ Object
Trigger multiple events asynchronously. For parameters see #trigger_batch
| 297 298 299 | # File 'lib/pusher/client.rb', line 297 def trigger_batch_async(*events) post_async('/batch_events', trigger_batch_params(events.flatten)) end | 
#url(path = nil) ⇒ Object
| 57 58 59 60 61 62 63 64 65 | # File 'lib/pusher/client.rb', line 57 def url(path = nil) raise ConfigurationError, :app_id unless @app_id URI::Generic.build({ :scheme => @scheme, :host => @host, :port => @port, :path => "/apps/#{@app_id}#{path}" }) end | 
#url=(url) ⇒ Object
Configure Pusher connection by providing a url rather than specifying scheme, key, secret, and app_id separately.
| 73 74 75 76 77 78 79 80 81 | # File 'lib/pusher/client.rb', line 73 def url=(url) uri = URI.parse(url) @scheme = uri.scheme @app_id = uri.path.split('/').last @key = uri.user @secret = uri.password @host = uri.host @port = uri.port end |