class RealPush::Client

Attributes

app_id[RW]
connect_timeout[W]
hostname[RW]
keep_alive_timeout[W]
port[RW]
privatekey[RW]
receive_timeout[W]
scheme[RW]
send_timeout[W]

Public Class Methods

new(options={}) click to toggle source
# File lib/realpush/client.rb, line 12
def initialize(options={})
  options = {
    port: 443,
    scheme: 'http',
    hostname: '127.0.0.1',
    app_id: nil,
    privatekey: nil
  }.merge options

  @encrypted = false

  @scheme, @app_id, @privatekey, @hostname, @port = options.values_at(
    :scheme, :app_id, :privatekey, :hostname, :port
  )

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

Public Instance Methods

authenticate(publickey, privatekey) click to toggle source
# File lib/realpush/client.rb, line 34
def authenticate(publickey, privatekey)
  @app_id, @privatekey = publickey, privatekey
end
authentication_string(custom_string=nil) click to toggle source

Generate the expected response for an authentication endpoint.

@example Private channels

render :json => RealPush.default_client.authenticate(params[:realpush])

@param custom_string [String | Hash]

@return [Hash]

# File lib/realpush/client.rb, line 46
def authentication_string(custom_string=nil)
  custom_string = MultiJson.encode(custom_string) if custom_string.kind_of?(Hash)
  unless custom_string.nil? || custom_string.kind_of?(String)
    raise Error, 'Custom argument must be a string'
  end

  string_to_sign = [app_id, custom_string].compact.map(&:to_s).join(':')
  digest = OpenSSL::Digest::SHA256.new
  signature = OpenSSL::HMAC.hexdigest(digest, privatekey, string_to_sign)
  {auth:signature}
end
authentication_token() click to toggle source

@private Returns the authentication token for the client

# File lib/realpush/client.rb, line 59
def authentication_token
  Signature::Token.new(app_id, privatekey)
end
config() { |self| ... } click to toggle source
# File lib/realpush/client.rb, line 63
def config(&block)
  raise ConfigurationError, 'You need a block' unless block_given?
  yield self
end
encrypted=(bool) click to toggle source
# File lib/realpush/client.rb, line 68
def encrypted=(bool)
  @scheme = bool ? 'https' : 'http'
  # Configure port if it hasn't already been configured
  @port = bool ? 443 : 80
end
encrypted?() click to toggle source
# File lib/realpush/client.rb, line 74
def encrypted?
  scheme == 'https'
end
get(path, params) click to toggle source
# File lib/realpush/client.rb, line 78
def get(path, params)
  Resource.new(self, "/#{app_id}#{path}").get params
end
get_async(path, params) click to toggle source
# File lib/realpush/client.rb, line 82
def get_async(path, params)
  Resource.new(self, "/#{app_id}#{path}").get_async params
end
post(path, body) click to toggle source
# File lib/realpush/client.rb, line 86
def post(path, body)
  Resource.new(self, "/#{app_id}#{path}").post body
end
post_async(path, body) click to toggle source
# File lib/realpush/client.rb, line 90
def post_async(path, body)
  Resource.new(self, "/#{app_id}#{path}").post_async body
end
sync_http_client() click to toggle source

@private Construct a net/http http client

# File lib/realpush/client.rb, line 95
def sync_http_client
  @client ||= begin
    require 'httpclient'

    HTTPClient.new(default_header: {'X-RealPush-Secret-Key' => @privatekey}).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) click to toggle source

Convenience method to set all timeouts to the same value (in seconds). For more control, use the individual writers.

# File lib/realpush/client.rb, line 110
def timeout=(value)
  @connect_timeout, @send_timeout, @receive_timeout = value, value, value
end
trigger(channels, event_name, data) click to toggle source

Trigger an event on one or more channels

POST /api//events//

@param channels [String or Array] 1-10 channel names @param event_name [String] @param data [Object] Event data to be triggered in javascript.

Objects other than strings will be converted to JSON

@return [Hash] See Thunderpush API docs

@raise [ThunderPush::Error] Unsuccessful response - see the error message @raise [ThunderPush::HTTPError] Error raised inside http client. The original error is wrapped in error.original_error

# File lib/realpush/client.rb, line 128
def trigger(channels, event_name, data)
  post("/events/#{event_name}/", trigger_params(channels, data))
end
trigger_async(channels, event_name, data) click to toggle source

Trigger an event on one or more channels

POST /apps//events//

@param channels [String or Array] 1-10 channel names @param event_name [String] @param data [Object] Event data to be triggered in javascript.

Objects other than strings will be converted to JSON

@raise [ThunderPush::Error] Unsuccessful response - see the error message @raise [ThunderPush::HTTPError] Error raised inside http client. The original error is wrapped in error.original_error

# File lib/realpush/client.rb, line 144
def trigger_async(channels, event_name, data)
  post_async("/events/#{event_name}/", trigger_params(channels, data))
end
url(path = '') click to toggle source

@private Builds a url for this app, optionally appending a path

# File lib/realpush/client.rb, line 166
def url(path = '')
  path = "/#{path}" unless path.start_with? '/'
  URI::Generic.build({
    :scheme => @scheme,
    :host => @hostname,
    :port => @port,
    :path => "/#{RealPush::API_VERSION_APP}#{path}"
  })
end
url=(str) click to toggle source

Configure Thunderpush connection by providing a url rather than specifying scheme, key, secret, and app_id separately.

@example

ThunderPush.default_client.url = http://key:secret@127.0.0.1:5678
# File lib/realpush/client.rb, line 154
def url=(str)
  regex = /^(?<scheme>http|https):\/\/((?<app_id>[\d-]+)(:(?<privatekey>[\w-]+){1})?@)?(?<hostname>[\w\.-]+)(:(?<port>[\d]+))?/
  match = str.match regex
  @scheme     = match[:scheme]     unless match[:scheme].nil?
  self.encrypted= true if scheme == 'https'
  @port       = match[:port].to_i  unless match[:port].nil?
  @hostname   = match[:hostname]   unless match[:hostname].nil?
  @app_id     = match[:app_id]     unless match[:app_id].nil?
  @privatekey = match[:privatekey] unless match[:privatekey].nil?
end

Protected Instance Methods

trigger_params(channels, data) click to toggle source
# File lib/realpush/client.rb, line 178
def trigger_params(channels, data)
  data.merge! :channels => channels
  begin
    MultiJson.encode(data)
  rescue MultiJson::DecodeError => e
    ThunderPush.logger.error("Could not convert #{data.inspect} into JSON")
    raise e
  end
end