Class: Ass::Client

Inherits:
Object
  • Object
show all
Includes:
Callback
Defined in:
lib/ass.rb

Instance Method Summary collapse

Methods included from Callback

#build_callback_klass, #callback

Constructor Details

#initialize(server_exchange, client_exchange, queue) ⇒ Client

Returns a new instance of Client.



112
113
114
115
116
# File 'lib/ass.rb', line 112

def initialize(server_exchange,client_exchange,queue)
  @server_exchange = server_exchange
  @client_exchange = client_exchange
  @queue = queue
end

Instance Method Details

#call(method, data = nil, meta = nil, opts = {}) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ass.rb', line 135

def call(method,data=nil,meta=nil,opts={})
  # opts passed to publish
  # if no routing key is given, use receiver's name as the routing key.
  version = @klass.version  if @klass.respond_to? :version
  payload = {
    :method => method,
    :data => data,
    :meta => meta,
    :version => version
  }

  # set it up s.t. server would respond to
  # private queue if key is given, otherwise
  # the server would respond to public queue.
  key = opts.delete(:key)
  @server_exchange.publish Marshal.dump(payload), {
    :key => (key ? key : @server_exchange.name),
    :reply_to => @client_exchange.name
  }.merge(opts)
end

#cast(method, data = nil, meta = nil, opts = {}) ⇒ Object

for casting, just null the reply_to field, so server doesn’t respond.



157
158
159
# File 'lib/ass.rb', line 157

def cast(method,data=nil,meta=nil,opts={})
  self.call(method,data,meta,opts.merge({:reply_to => nil}))
end

#react(callback = nil, opts = nil, &block) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ass.rb', line 118

def react(callback=nil,opts=nil,&block)
  if block
    opts = callback
    callback = block
  end
  opts = {} if opts.nil?
  
  @callback_klass = build_callback_klass(callback)
  @ack = opts[:ack]
  @queue.subscribe(opts) do |info,payload|
    payload = ::Marshal.load(payload)
    callback(info,payload)
    info.ack if @ack
  end
  self
end