Class: Pusher::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pusher/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

CONFIGURATION ##


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pusher/client.rb', line 12

def initialize(options = {})
  options = {
    :scheme => 'http',
    :host => 'api.pusherapp.com',
    :port => 80,
  }.merge(options)
  @scheme, @host, @port, @app_id, @key, @secret = options.values_at(
    :scheme, :host, :port, :app_id, :key, :secret
  )
  @http_proxy = nil
  self.http_proxy = options[:http_proxy] if options[:http_proxy]

  # Default timeouts
  @connect_timeout = 5
  @send_timeout = 5
  @receive_timeout = 5
  @keep_alive_timeout = 30
end

Instance Attribute Details

#app_idObject

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

Parameters:

  • value

    the value to set the attribute connect_timeout to.


7
8
9
# File 'lib/pusher/client.rb', line 7

def connect_timeout=(value)
  @connect_timeout = value
end

#hostObject

Returns the value of attribute host


5
6
7
# File 'lib/pusher/client.rb', line 5

def host
  @host
end

#http_proxyObject

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

Parameters:

  • value

    the value to set the attribute keep_alive_timeout to.


7
8
9
# File 'lib/pusher/client.rb', line 7

def keep_alive_timeout=(value)
  @keep_alive_timeout = value
end

#keyObject

Returns the value of attribute key


5
6
7
# File 'lib/pusher/client.rb', line 5

def key
  @key
end

#portObject

Returns the value of attribute port


5
6
7
# File 'lib/pusher/client.rb', line 5

def port
  @port
end

#proxyObject (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

Parameters:

  • value

    the value to set the attribute receive_timeout to.


7
8
9
# File 'lib/pusher/client.rb', line 7

def receive_timeout=(value)
  @receive_timeout = value
end

#schemeObject

Returns the value of attribute scheme


5
6
7
# File 'lib/pusher/client.rb', line 5

def scheme
  @scheme
end

#secretObject

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

Parameters:

  • value

    the value to set the attribute send_timeout to.


7
8
9
# File 'lib/pusher/client.rb', line 7

def send_timeout=(value)
  @send_timeout = value
end

Instance Method Details

#authentication_tokenObject


32
33
34
# File 'lib/pusher/client.rb', line 32

def authentication_token
  Signature::Token.new(@key, @secret)
end

#channel(channel_name) ⇒ Channel Also known as: []

Return a convenience channel object by name. No API request is made.

Examples:

Pusher['my-channel']

Returns:

Raises:

  • (ConfigurationError)

    unless key, secret and app_id have been configured. Channel names should be less than 200 characters, and should not contain anything other than letters, numbers, or the characters “[email protected],.;”


174
175
176
177
# File 'lib/pusher/client.rb', line 174

def channel(channel_name)
  raise ConfigurationError, 'Missing client configuration: please check that key, secret and app_id are configured.' unless configured?
  Channel.new(url, channel_name, self)
end

#channel_info(channel_name, params = {}) ⇒ Hash

Request info for a specific channel

GET /apps//channels/

Parameters:

  • channel_name (String)

    Channel name (max 200 characters)

  • params (Hash) (defaults to: {})

    Hash of parameters for the API - see REST API docs

Returns:

  • (Hash)

    See Pusher API docs

Raises:

  • (Pusher::Error)

    Unsuccessful response - see the error message

  • (Pusher::HTTPError)

    Error raised inside http client. The original error is wrapped in error.original_error


208
209
210
# File 'lib/pusher/client.rb', line 208

def channel_info(channel_name, params = {})
  get("/channels/#{channel_name}", params)
end

#channels(params = {}) ⇒ Hash

Request a list of occupied channels from the API

GET /apps//channels

Parameters:

  • params (Hash) (defaults to: {})

    Hash of parameters for the API - see REST API docs

Returns:

  • (Hash)

    See Pusher API docs

Raises:

  • (Pusher::Error)

    Unsuccessful response - see the error message

  • (Pusher::HTTPError)

    Error raised inside http client. The original error is wrapped in error.original_error


192
193
194
# File 'lib/pusher/client.rb', line 192

def channels(params = {})
  get('/channels', params)
end

#em_http_client(uri) ⇒ Object


253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/pusher/client.rb', line 253

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)

Examples:

Pusher.encrypted = true

81
82
83
84
85
# File 'lib/pusher/client.rb', line 81

def encrypted=(boolean)
  @scheme = boolean ? 'https' : 'http'
  # Configure port if it hasn't already been configured
  @port = boolean ? 443 : 80
end

#encrypted?Boolean

Returns:

  • (Boolean)

87
88
89
# File 'lib/pusher/client.rb', line 87

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.

Examples:

begin
  Pusher.get('/channels', filter_by_prefix: 'private-')
rescue Pusher::Error => e
  # Handle error
end

Parameters:

  • path (String)

    Path excluding /apps/APP_ID

  • params (Hash) (defaults to: {})

    API params (see pusher.com/docs/rest_api)

Returns:

  • (Hash)

    See Pusher API docs

Raises:

  • (Pusher::Error)

    Unsuccessful response - see the error message

  • (Pusher::HTTPError)

    Error raised inside http client. The original error is wrapped in error.original_error


121
122
123
# File 'lib/pusher/client.rb', line 121

def get(path, params = {})
  Resource.new(self, 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.

Parameters:

  • path (String)

    Path excluding /apps/APP_ID

  • params (Hash) (defaults to: {})

    API params (see pusher.com/docs/rest_api)

Returns:

  • Either an EM::DefaultDeferrable or a HTTPClient::Connection


137
138
139
# File 'lib/pusher/client.rb', line 137

def get_async(path, params = {})
  Resource.new(self, 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.


143
144
145
# File 'lib/pusher/client.rb', line 143

def post(path, params = {})
  Resource.new(self, 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.


150
151
152
# File 'lib/pusher/client.rb', line 150

def post_async(path, params = {})
  Resource.new(self, path).post_async(params)
end

#resource(path) ⇒ Object

INTERACE WITH THE API ##


99
100
101
# File 'lib/pusher/client.rb', line 99

def resource(path)
  Resource.new(self, path)
end

#sync_http_clientObject


239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/pusher/client.rb', line 239

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.


93
94
95
# File 'lib/pusher/client.rb', line 93

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

Parameters:

  • channels (String or Array)

    1-10 channel names

  • event_name (String)
  • data (Object)

    Event data to be triggered in javascript. Objects other than strings will be converted to JSON

  • params (Hash) (defaults to: {})

    Additional parameters to send to api, e.g socket_id

Returns:

  • (Hash)

    See Pusher API docs

Raises:

  • (Pusher::Error)

    Unsuccessful response - see the error message

  • (Pusher::HTTPError)

    Error raised inside http client. The original error is wrapped in error.original_error


227
228
229
# File 'lib/pusher/client.rb', line 227

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


234
235
236
# File 'lib/pusher/client.rb', line 234

def trigger_async(channels, event_name, data, params = {})
  post_async('/events', trigger_params(channels, event_name, data, params))
end

#url(path = nil) ⇒ Object


37
38
39
40
41
42
43
44
# File 'lib/pusher/client.rb', line 37

def url(path = nil)
  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.

Examples:

Pusher.url = http://KEY:[email protected]/apps/APP_ID

52
53
54
55
56
57
58
59
60
# File 'lib/pusher/client.rb', line 52

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

#webhook(request) ⇒ Object

Convenience method for creating a new WebHook instance for validating and extracting info from a received WebHook

Parameters:

  • request (Rack::Request)

    Either a Rack::Request or a Hash containing :key, :signature, :body, and optionally :content_type.


161
162
163
# File 'lib/pusher/client.rb', line 161

def webhook(request)
  WebHook.new(request, self)
end