Class: SmartfoxJruby::SfsAdapter
- Inherits:
-
Object
- Object
- SmartfoxJruby::SfsAdapter
- Includes:
- IEventListener
- Defined in:
- lib/smartfox_jruby/sfs_adapter.rb
Constant Summary collapse
- DEBUG =
false
Instance Attribute Summary collapse
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#smart_fox ⇒ Object
readonly
Returns the value of attribute smart_fox.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #connect!(opt = {}) ⇒ Object
- #connected? ⇒ Boolean
- #disconnect! ⇒ Object
- #dispatch(event) ⇒ Object
-
#initialize(opts = {}) ⇒ SfsAdapter
constructor
A new instance of SfsAdapter.
- #login_as(username, password, params = {}) ⇒ Object
- #on_connect(&block) ⇒ Object
- #on_login(&block) ⇒ Object
- #process!(wrk = nil, &block) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ SfsAdapter
Returns a new instance of SfsAdapter.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/smartfox_jruby/sfs_adapter.rb', line 13 def initialize(opts = {}) @opts = opts @smart_fox = SmartFox.new(false) @connected = false @worker = opts[:worker] @login_as = opts[:login_as] || {} @opts[:timeout] ||= 20 @opts[:logger] ||= Logger.new(STDOUT) SFSEvent.constants.each do |evt| evt_value = SFSEvent.const_get(evt) debug "Registering event adapter to self for event '#{evt}' --> '#{evt_value}'..." smart_fox.add_event_listener(evt_value, self) end debug "initializing sfs adapter..." end |
Instance Attribute Details
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
7 8 9 |
# File 'lib/smartfox_jruby/sfs_adapter.rb', line 7 def opts @opts end |
#smart_fox ⇒ Object (readonly)
Returns the value of attribute smart_fox.
8 9 10 |
# File 'lib/smartfox_jruby/sfs_adapter.rb', line 8 def smart_fox @smart_fox end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
10 11 12 |
# File 'lib/smartfox_jruby/sfs_adapter.rb', line 10 def username @username end |
Instance Method Details
#connect!(opt = {}) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/smartfox_jruby/sfs_adapter.rb', line 29 def connect!(opt = {}) opts.reverse_merge!(opt) raise "host and port are required to connect SfsAdapter!" if opts[:host].blank? || opts[:port].blank? debug "connecting to smartfox at #{opts[:host]}:#{opts[:port]} ..." smart_fox.connect(opts[:host], opts[:port]) end |
#connected? ⇒ Boolean
62 63 64 |
# File 'lib/smartfox_jruby/sfs_adapter.rb', line 62 def connected? @connected end |
#disconnect! ⇒ Object
46 47 48 |
# File 'lib/smartfox_jruby/sfs_adapter.rb', line 46 def disconnect! smart_fox.disconnect end |
#dispatch(event) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/smartfox_jruby/sfs_adapter.rb', line 36 def dispatch(event) debug "got event #{event.type}: #{event}" callback = "on_#{event.type.try(:underscore)}_event" if respond_to?(callback, true) send(callback, event) else debug "Unknown event caught #{event.type} (No method '#{callback}' in #{self})" end end |
#login_as(username, password, params = {}) ⇒ Object
76 77 78 79 |
# File 'lib/smartfox_jruby/sfs_adapter.rb', line 76 def login_as(username, password, params = {}) params = params.to_sfsobject if params.is_a?(Hash) @login_as = {:username => username, :password => password, :params => params} end |
#on_connect(&block) ⇒ Object
66 67 68 69 |
# File 'lib/smartfox_jruby/sfs_adapter.rb', line 66 def on_connect(&block) @on_connect ||= [] @on_connect << block if block_given? end |
#on_login(&block) ⇒ Object
71 72 73 74 |
# File 'lib/smartfox_jruby/sfs_adapter.rb', line 71 def on_login(&block) @on_login ||= [] @on_login << block if block_given? end |
#process!(wrk = nil, &block) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/smartfox_jruby/sfs_adapter.rb', line 50 def process!(wrk = nil, &block) wait_with_timeout(@opts[:timeout]) { connected? } @worker = wrk || opts[:worker] if block_given? && connected? instance_eval(&block) else raise "Worker is null!" if @worker.blank? raise "Not connected!" unless connected? end worker.perform! end |