Class: Zack::Client

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

Overview

Client part of Zack RPC

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tube_name, opts = {}) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/zack/client.rb', line 9

def initialize(tube_name, opts={})
  server = opts[:server] || 'beanstalk:11300'
  # Only respond to these messages
  @only        = opts[:only] || proc { true }
  # These have answers (wait for the server to answer)
  @with_answer = opts[:with_answer] || []

  @outgoing = Cod.beanstalk(server, tube_name)
  unless @with_answer.empty?
    @incoming = Cod.beanstalk(server, 
      unique_tube_name(tube_name))
  end
  
  @service = Cod::Client.new(@outgoing, @incoming, 1)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/zack/client.rb', line 28

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)
    return service.call([sym, args])
  else
    service.notify [sym, args]
    return nil
  end
rescue Cod::Channel::TimeoutError
  raise Zack::ServiceTimeout, "No response from server in the allowed time."
end

Instance Attribute Details

#serviceObject (readonly)

Returns the value of attribute service.



7
8
9
# File 'lib/zack/client.rb', line 7

def service
  @service
end

Instance Method Details

#has_answer?(sym) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/zack/client.rb', line 43

def has_answer?(sym)
  @with_answer.include?(sym.to_sym)
end

#respond_to?(msg) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/zack/client.rb', line 25

def respond_to?(msg)
  !! @only[msg]
end