class RealPush::Client
Attributes
Public Class Methods
# 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
# File lib/realpush/client.rb, line 34 def authenticate(publickey, privatekey) @app_id, @privatekey = publickey, privatekey end
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
@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
# File lib/realpush/client.rb, line 63 def config(&block) raise ConfigurationError, 'You need a block' unless block_given? yield self end
# 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
# File lib/realpush/client.rb, line 74 def encrypted? scheme == 'https' end
# File lib/realpush/client.rb, line 78 def get(path, params) Resource.new(self, "/#{app_id}#{path}").get params end
# File lib/realpush/client.rb, line 82 def get_async(path, params) Resource.new(self, "/#{app_id}#{path}").get_async params end
# File lib/realpush/client.rb, line 86 def post(path, body) Resource.new(self, "/#{app_id}#{path}").post body end
# File lib/realpush/client.rb, line 90 def post_async(path, body) Resource.new(self, "/#{app_id}#{path}").post_async body end
@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
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 an event on one or more channels
@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 an event on one or more channels
@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
@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
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
# 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