Class: RealPush::Client
- Inherits:
-
Object
- Object
- RealPush::Client
- Defined in:
- lib/realpush/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.
-
#hostname ⇒ Object
Returns the value of attribute hostname.
-
#keep_alive_timeout ⇒ Object
writeonly
Sets the attribute keep_alive_timeout.
-
#port ⇒ Object
Returns the value of attribute port.
-
#privatekey ⇒ Object
Returns the value of attribute privatekey.
-
#receive_timeout ⇒ Object
writeonly
Sets the attribute receive_timeout.
-
#scheme ⇒ Object
Returns the value of attribute scheme.
-
#send_timeout ⇒ Object
writeonly
Sets the attribute send_timeout.
Instance Method Summary collapse
- #authenticate(publickey, privatekey) ⇒ Object
-
#authentication_string(custom_string = nil) ⇒ Hash
Generate the expected response for an authentication endpoint.
- #authentication_token ⇒ Object
- #config {|_self| ... } ⇒ Object
- #encrypted=(bool) ⇒ Object
- #encrypted? ⇒ Boolean
- #get(path, params) ⇒ Object
- #get_async(path, params) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #post(path, body) ⇒ Object
- #post_async(path, body) ⇒ Object
- #sync_http_client ⇒ Object
-
#timeout=(value) ⇒ Object
Convenience method to set all timeouts to the same value (in seconds).
-
#trigger(channels, event_name, data) ⇒ Hash
Trigger an event on one or more channels.
-
#trigger_async(channels, event_name, data) ⇒ Object
Trigger an event on one or more channels.
- #url(path = '') ⇒ Object
-
#url=(str) ⇒ Object
Configure Thunderpush connection by providing a url rather than specifying scheme, key, secret, and app_id separately.
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/realpush/client.rb', line 12 def initialize(={}) = { port: 443, scheme: 'http', hostname: '127.0.0.1', app_id: nil, privatekey: nil }.merge @encrypted = false @scheme, @app_id, @privatekey, @hostname, @port = .values_at( :scheme, :app_id, :privatekey, :hostname, :port ) # 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.
8 9 10 |
# File 'lib/realpush/client.rb', line 8 def app_id @app_id end |
#connect_timeout=(value) ⇒ Object (writeonly)
Sets the attribute connect_timeout
9 10 11 |
# File 'lib/realpush/client.rb', line 9 def connect_timeout=(value) @connect_timeout = value end |
#hostname ⇒ Object
Returns the value of attribute hostname.
8 9 10 |
# File 'lib/realpush/client.rb', line 8 def hostname @hostname end |
#keep_alive_timeout=(value) ⇒ Object (writeonly)
Sets the attribute keep_alive_timeout
9 10 11 |
# File 'lib/realpush/client.rb', line 9 def keep_alive_timeout=(value) @keep_alive_timeout = value end |
#port ⇒ Object
Returns the value of attribute port.
8 9 10 |
# File 'lib/realpush/client.rb', line 8 def port @port end |
#privatekey ⇒ Object
Returns the value of attribute privatekey.
8 9 10 |
# File 'lib/realpush/client.rb', line 8 def privatekey @privatekey end |
#receive_timeout=(value) ⇒ Object (writeonly)
Sets the attribute receive_timeout
9 10 11 |
# File 'lib/realpush/client.rb', line 9 def receive_timeout=(value) @receive_timeout = value end |
#scheme ⇒ Object
Returns the value of attribute scheme.
8 9 10 |
# File 'lib/realpush/client.rb', line 8 def scheme @scheme end |
#send_timeout=(value) ⇒ Object (writeonly)
Sets the attribute send_timeout
9 10 11 |
# File 'lib/realpush/client.rb', line 9 def send_timeout=(value) @send_timeout = value end |
Instance Method Details
#authenticate(publickey, privatekey) ⇒ Object
34 35 36 |
# File 'lib/realpush/client.rb', line 34 def authenticate(publickey, privatekey) @app_id, @privatekey = publickey, privatekey end |
#authentication_string(custom_string = nil) ⇒ Hash
Generate the expected response for an authentication endpoint.
46 47 48 49 50 51 52 53 54 55 56 |
# 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 ⇒ Object
59 60 61 |
# File 'lib/realpush/client.rb', line 59 def authentication_token Signature::Token.new(app_id, privatekey) end |
#config {|_self| ... } ⇒ Object
63 64 65 66 |
# File 'lib/realpush/client.rb', line 63 def config(&block) raise ConfigurationError, 'You need a block' unless block_given? yield self end |
#encrypted=(bool) ⇒ Object
68 69 70 71 72 |
# 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? ⇒ Boolean
74 75 76 |
# File 'lib/realpush/client.rb', line 74 def encrypted? scheme == 'https' end |
#get(path, params) ⇒ Object
78 79 80 |
# File 'lib/realpush/client.rb', line 78 def get(path, params) Resource.new(self, "/#{app_id}#{path}").get params end |
#get_async(path, params) ⇒ Object
82 83 84 |
# 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) ⇒ Object
86 87 88 |
# File 'lib/realpush/client.rb', line 86 def post(path, body) Resource.new(self, "/#{app_id}#{path}").post body end |
#post_async(path, body) ⇒ Object
90 91 92 |
# 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 ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 |
# 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) ⇒ Object
Convenience method to set all timeouts to the same value (in seconds). For more control, use the individual writers.
110 111 112 |
# 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) ⇒ Hash
128 129 130 |
# 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) ⇒ Object
144 145 146 |
# 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 = '') ⇒ Object
166 167 168 169 170 171 172 173 174 |
# 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) ⇒ Object
Configure Thunderpush connection by providing a url rather than specifying scheme, key, secret, and app_id separately.
154 155 156 157 158 159 160 161 162 163 |
# 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 |