Class: Nasreddin::RemoteTorqueboxAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/nasreddin/remote_torquebox_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, klass) ⇒ RemoteTorqueboxAdapter

Returns a new instance of RemoteTorqueboxAdapter.



32
33
34
35
# File 'lib/nasreddin/remote_torquebox_adapter.rb', line 32

def initialize(resource, klass)
  @resource = resource
  @klass = klass
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



6
7
8
# File 'lib/nasreddin/remote_torquebox_adapter.rb', line 6

def klass
  @klass
end

#resourceObject

Returns the value of attribute resource.



6
7
8
# File 'lib/nasreddin/remote_torquebox_adapter.rb', line 6

def resource
  @resource
end

Instance Method Details

#call(params, as_new_objects = false) ⇒ Object



26
27
28
29
30
# File 'lib/nasreddin/remote_torquebox_adapter.rb', line 26

def call(params, as_new_objects=false)
  status, _, data = *(queue.publish_and_receive(params, persistant: false))
  values = load_data(data,@resource,as_new_objects) if data && !data.empty?
  [ succeded?(status), values ]
end

#load_data(data, resource, as_objects = true) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/nasreddin/remote_torquebox_adapter.rb', line 8

def load_data(data,resource, as_objects = true)
  resp = MultiJson.load(data)
  resp = resp[@resource] if resp.respond_to?(:keys) && resp.keys.include?(@resource)
  if resp.kind_of? Array
    as_objects ? resp.map { |r| @klass.new(r) } : resp
  else
    as_objects ? @klass.new(resp) : resp
  end
end

#queueObject



22
23
24
# File 'lib/nasreddin/remote_torquebox_adapter.rb', line 22

def queue
  @queue ||= TorqueBox::Messaging::Queue.start("/queues/#{@resource}", durable: false)
end

#succeded?(status) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/nasreddin/remote_torquebox_adapter.rb', line 18

def succeded?(status)
  status != nil && status > 199 && status < 300
end