Class: Pubnub::Client

Inherits:
Object show all
Includes:
Configuration
Defined in:
lib/pubnub/client.rb

Constant Summary collapse

DEFAULT_CONNECT_CALLBACK =
lambda { |msg| $log.info "CONNECTED: #{msg}" }
DEFAULT_ERROR_CALLBACK =
lambda { |msg| $log.error "AN ERROR OCCURRED: #{msg}" }

Constants included from Configuration

Pubnub::Configuration::DEFAULT_AUTO_RECONNECT, Pubnub::Configuration::DEFAULT_CALLBACK, Pubnub::Configuration::DEFAULT_CHANNEL, Pubnub::Configuration::DEFAULT_CONTENT_TYPE, Pubnub::Configuration::DEFAULT_ENCODING, Pubnub::Configuration::DEFAULT_HEADERS, Pubnub::Configuration::DEFAULT_METHOD, Pubnub::Configuration::DEFAULT_ORIGIN, Pubnub::Configuration::DEFAULT_PARAMS, Pubnub::Configuration::DEFAULT_PATH, Pubnub::Configuration::DEFAULT_PORT, Pubnub::Configuration::DEFAULT_PUBLISH_KEY, Pubnub::Configuration::DEFAULT_SECRET_KEY, Pubnub::Configuration::DEFAULT_SSL_SET, Pubnub::Configuration::DEFAULT_SUBSCRIBE_KEY, Pubnub::Configuration::DEFAULT_TIMEOUT, Pubnub::Configuration::DEFAULT_TIMETOKEN, Pubnub::Configuration::DEFAULT_USER_AGENT, Pubnub::Configuration::MAX_RETRIES, Pubnub::Configuration::PERIODIC_TIMER

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/pubnub/client.rb', line 38

def initialize(options = {})
  $log = options[:logger]
  $log = Logger.new('pubnub.log', 0, 100 * 1024 * 1024) unless $log

  @subscriptions = Array.new

  @subscription_request = nil
  @retry            = true
  @retry_count      = 0
  @callback         = options[:callback]
  @error_callback   = options[:error_callback]
  @error_callback   = DEFAULT_ERROR_CALLBACK unless @error_callback
  @connect_callback = options[:connect_callback]
  @connect_callback = DEFAULT_CONNECT_CALLBACK unless @connect_callback
  @cipher_key       = options[:cipher_key]
  @publish_key      = options[:publish_key] || DEFAULT_PUBLISH_KEY
  @subscribe_key    = options[:subscribe_key] || DEFAULT_SUBSCRIBE_KEY
  @channel          = options[:channel] || DEFAULT_CHANNEL
  @message          = options[:message]
  @ssl              = options[:ssl]
  @secret_key       = options[:secret_key]
  @timetoken        = options[:timetoken]
  @session_uuid     = options[:uuid] || options[:session_uuid] || UUID.new.generate

      @history_count    = options[:count]
  @history_start    = options[:start]
  @history_end      = options[:end]
  @history_reverse  = options[:reverse]

  @port             = options[:port]
  @url              = options[:url]
  @origin           = options[:origin]
  @origin           = DEFAULT_ORIGIN unless @origin
  @query            = options[:query]

  @http_sync        = options[:http_sync]

  @max_retries = options[:max_retries]
  @max_retries = MAX_RETRIES unless @max_retries

  @non_subscribe_timeout = options[:non_subscribe_timeout]
  @non_subscribe_timeout = 5 unless @non_subscribe_timeout

  @reconnect_max_attempts = options[:reconnect_max_attempts]
  @reconnect_max_attempts = 60 unless @reconnect_max_attempts

  @reconnect_retry_interval = options[:reconnect_retry_interval]
  @reconnect_retry_interval = 5 unless @reconnect_retry_interval

  @reconnect_response_timeout = options[:reconnect_response_timeout]
  @reconnect_response_timeout = 5 unless @reconnect_response_timeout

  @sync_connection_sub = Pubnub::PubNubHTTParty.new
  @sync_connection     = Pubnub::PubNubHTTParty.new

  @pause_subscribe = false
end

Instance Attribute Details

#callbackObject

Returns the value of attribute callback.



31
32
33
# File 'lib/pubnub/client.rb', line 31

def callback
  @callback
end

#channelObject

Returns the value of attribute channel.



31
32
33
# File 'lib/pubnub/client.rb', line 31

def channel
  @channel
end

#cipher_keyObject

Returns the value of attribute cipher_key.



31
32
33
# File 'lib/pubnub/client.rb', line 31

def cipher_key
  @cipher_key
end

#close_connectionObject

Returns the value of attribute close_connection.



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

def close_connection
  @close_connection
end

#errorObject

Returns the value of attribute error.



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

def error
  @error
end

#history_countObject

Returns the value of attribute history_count.



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

def history_count
  @history_count
end

#history_endObject

Returns the value of attribute history_end.



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

def history_end
  @history_end
end

#history_limitObject

Returns the value of attribute history_limit.



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

def history_limit
  @history_limit
end

#history_reverseObject

Returns the value of attribute history_reverse.



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

def history_reverse
  @history_reverse
end

#history_startObject

Returns the value of attribute history_start.



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

def history_start
  @history_start
end

#hostObject

Returns the value of attribute host.



31
32
33
# File 'lib/pubnub/client.rb', line 31

def host
  @host
end

#jsonpObject

Returns the value of attribute jsonp.



31
32
33
# File 'lib/pubnub/client.rb', line 31

def jsonp
  @jsonp
end

#last_timetokenObject

Returns the value of attribute last_timetoken.



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

def last_timetoken
  @last_timetoken
end

#messageObject

Returns the value of attribute message.



31
32
33
# File 'lib/pubnub/client.rb', line 31

def message
  @message
end

#operationObject

Returns the value of attribute operation.



31
32
33
# File 'lib/pubnub/client.rb', line 31

def operation
  @operation
end

#originObject

Returns the value of attribute origin.



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

def origin
  @origin
end

#portObject

Returns the value of attribute port.



31
32
33
# File 'lib/pubnub/client.rb', line 31

def port
  @port
end

#publish_keyObject

Returns the value of attribute publish_key.



31
32
33
# File 'lib/pubnub/client.rb', line 31

def publish_key
  @publish_key
end

#queryObject

Returns the value of attribute query.



31
32
33
# File 'lib/pubnub/client.rb', line 31

def query
  @query
end

#responseObject

Returns the value of attribute response.



31
32
33
# File 'lib/pubnub/client.rb', line 31

def response
  @response
end

#secret_keyObject

Returns the value of attribute secret_key.



31
32
33
# File 'lib/pubnub/client.rb', line 31

def secret_key
  @secret_key
end

#session_uuidObject

Returns the value of attribute session_uuid.



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

def session_uuid
  @session_uuid
end

#sslObject

Returns the value of attribute ssl.



31
32
33
# File 'lib/pubnub/client.rb', line 31

def ssl
  @ssl
end

#subscribe_keyObject

Returns the value of attribute subscribe_key.



31
32
33
# File 'lib/pubnub/client.rb', line 31

def subscribe_key
  @subscribe_key
end

#timetokenObject

Returns the value of attribute timetoken.



31
32
33
# File 'lib/pubnub/client.rb', line 31

def timetoken
  @timetoken
end

#urlObject

Returns the value of attribute url.



31
32
33
# File 'lib/pubnub/client.rb', line 31

def url
  @url
end

#uuidObject

Returns the value of attribute uuid.



31
32
33
# File 'lib/pubnub/client.rb', line 31

def uuid
  @uuid
end

Instance Method Details

#active_subscriptionsObject



167
168
169
# File 'lib/pubnub/client.rb', line 167

def active_subscriptions
  @subscription_request
end

#here_now(options = {}, &block) ⇒ Object



149
150
151
152
153
154
# File 'lib/pubnub/client.rb', line 149

def here_now(options = {}, &block)
  options[:callback] = block if block_given?
  options = merge_options(options, 'here_now')
  verify_operation('here_now', options)
  start_request options
end

#history(options = {}, &block) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/pubnub/client.rb', line 120

def history(options = {}, &block)
  options[:callback] = block if block_given?
  options = merge_options(options, 'history')
  verify_operation('history', options)
  options[:params].merge!({:count => options[:count]})
  options[:params].merge!({:start => options[:start]}) unless options[:start].nil?
  options[:params].merge!({:end => options[:end]}) unless options[:end].nil?
  options[:params].merge!({:reverse => 'true'}) if options[:reverse]
  start_request options
end

#leave(options = {}, &block) ⇒ Object Also known as: unsubscribe



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/pubnub/client.rb', line 131

def leave(options = {}, &block)
  options[:callback] = block if block_given?
  options = merge_options(options, 'leave')
  verify_operation('leave', options)
  return false unless get_channels_for_subscription.include? options[:channel]
  remove_from_subscription options[:channel]
  if @subscriptions.empty?
    @timetoken = 0
    @subscription_request.timetoken = 0
    @subscribe_connection.close
    @wait_for_response = false
  end
  start_request options

end

#presence(options = {}, &block) ⇒ Object



113
114
115
116
117
118
# File 'lib/pubnub/client.rb', line 113

def presence(options = {}, &block)
  options[:callback] = block if block_given?
  options = merge_options(options, 'presence')
  verify_operation('presence', options)
  start_request options
end

#publish(options = {}, &block) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/pubnub/client.rb', line 96

def publish(options = {}, &block)
  options[:callback] = block if block_given?
  options = merge_options(options, 'publish')
  verify_operation('publish', options)
  start_request options

end

#subscribe(options = {}, &block) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/pubnub/client.rb', line 104

def subscribe(options = {}, &block)
  options[:callback] = block if block_given?
  options = merge_options(options, 'subscribe')
  verify_operation('subscribe', options)
  @error_callback.call 'YOU ARE ALREADY SUBSCRIBED TO THAT CHANNEL' if get_channels_for_subscription.include? options[:channel]
  $log.error 'YOU ARE ALREADY SUBSCRIBED TO THAT CHANNEL' if get_channels_for_subscription.include? options[:channel]
  start_request options
end

#subscription_running?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/pubnub/client.rb', line 163

def subscription_running?
  @subscription_running
end

#time(options = {}, &block) ⇒ Object



156
157
158
159
160
161
# File 'lib/pubnub/client.rb', line 156

def time(options = {}, &block)
  options[:callback] = block if block_given?
  options = merge_options(options, 'time')
  verify_operation('time', options)
  start_request options
end