Class: Zack::Client
- Inherits:
-
Object
- Object
- Zack::Client
- Defined in:
- lib/zack/client.rb
Overview
Client for a simple client-server RPC connection.
Instance Attribute Summary collapse
-
#service ⇒ Object
readonly
Returns the value of attribute service.
Instance Method Summary collapse
- #close ⇒ Object
- #has_answer?(sym) ⇒ Boolean
-
#initialize(tube_name, opts = {}) ⇒ Client
constructor
Constructs a client for the service given by tube_name.
- #method_missing(sym, *args, &block) ⇒ Object
- #respond_to?(msg) ⇒ Boolean
Constructor Details
#initialize(tube_name, opts = {}) ⇒ Client
Constructs a client for the service given by tube_name. Optional arguments are:
- :server
-
beanstalkd server location url
- :only
-
ignores all messages not in this hash
- :with_answer
-
these messages wait for an answer from the service
- :timeout
-
How long to wait for an answer
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/zack/client.rb', line 17 def initialize(tube_name, opts={}) # Only respond to these messages @only = opts[:only] || proc { true } # These have answers (wait for the server to answer) @with_answer = opts[:with_answer] || [] @timeout = opts[:timeout] @tube_name = tube_name @server = opts[:server] || 'localhost:11300' connect end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/zack/client.rb', line 38 def method_missing(sym, *args, &block) super unless respond_to?(sym) raise ArgumentError, "Can't call methods remotely with a block" if block if has_answer?(sym) with_timeout do return service.call([sym, args]) end else service.notify [sym, args] return nil end rescue Timeout::Error => ex raise Zack::ServiceTimeout, "The service took too long to answer (>#{@timeout || 1}s)." end |
Instance Attribute Details
#service ⇒ Object (readonly)
Returns the value of attribute service.
7 8 9 |
# File 'lib/zack/client.rb', line 7 def service @service end |
Instance Method Details
#close ⇒ Object
55 56 57 |
# File 'lib/zack/client.rb', line 55 def close @service.close end |
#has_answer?(sym) ⇒ Boolean
34 35 36 |
# File 'lib/zack/client.rb', line 34 def has_answer?(sym) @with_answer.include?(sym.to_sym) end |
#respond_to?(msg) ⇒ Boolean
30 31 32 |
# File 'lib/zack/client.rb', line 30 def respond_to?(msg) !! @only[msg] end |