Class: RealPush::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Instance Attribute Details

#app_idObject

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

Parameters:

  • value

    the value to set the attribute connect_timeout to.



9
10
11
# File 'lib/realpush/client.rb', line 9

def connect_timeout=(value)
  @connect_timeout = value
end

#hostnameObject

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

Parameters:

  • value

    the value to set the attribute keep_alive_timeout to.



9
10
11
# File 'lib/realpush/client.rb', line 9

def keep_alive_timeout=(value)
  @keep_alive_timeout = value
end

#portObject

Returns the value of attribute port.



8
9
10
# File 'lib/realpush/client.rb', line 8

def port
  @port
end

#privatekeyObject

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

Parameters:

  • value

    the value to set the attribute receive_timeout to.



9
10
11
# File 'lib/realpush/client.rb', line 9

def receive_timeout=(value)
  @receive_timeout = value
end

#schemeObject

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

Parameters:

  • value

    the value to set the attribute send_timeout to.



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.

Examples:

Private channels

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

Parameters:

  • custom_string (String | Hash) (defaults to: nil)

Returns:

  • (Hash)


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_tokenObject



59
60
61
# File 'lib/realpush/client.rb', line 59

def authentication_token
  Signature::Token.new(app_id, privatekey)
end

#config {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

Raises:



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

Returns:

  • (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_clientObject



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

Trigger an event on one or more channels

POST /api//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

Returns:

  • (Hash)

    See Thunderpush API docs

Raises:

  • (ThunderPush::Error)

    Unsuccessful response - see the error message

  • (ThunderPush::HTTPError)

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



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

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

Raises:

  • (ThunderPush::Error)

    Unsuccessful response - see the error message

  • (ThunderPush::HTTPError)

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



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.

Examples:

ThunderPush.default_client.url = http://key:[email protected]:5678


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