Created
November 27, 2008 00:06
-
-
Save jmettraux/29639 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ActiveResourceParticipant | |
include OpenWFE::LocalParticipant | |
def initialize (options, &block) | |
@options = options | |
# some defaults | |
@options[:site] ||= 'http://127.0.0.1:3000' | |
@options[:method] ||= :get | |
@block = block | |
#[ :resource_name ].each do |n| | |
# raise "missing option :#{optname}" unless @options[n] | |
#end | |
end | |
# | |
# This method is called each time the participant receives a workitem. It | |
# calls the requested ActiveResource method, calls the response handling code | |
# if present and then immediatly replies to the engine. | |
# | |
def consume (workitem) | |
# use block to determine the method's arguments if given | |
args = if @block | |
call_block(@block, workitem) | |
elsif a = param[:args] || param[:arguments] | |
Array(a) | |
else | |
[] # no arguments | |
end | |
# create new subclass of ActiveResource::Base | |
active_resource_class = Class.new(ActiveResource::Base) | |
active_resource_class.site = param[:site] | |
active_resource_class.element_name = param[:resource_name] | |
# Do we work on a single or on a set of resources? If resource_id is nil or | |
# negative, it's a set of resources. | |
resource_id = param[:resource_id] | |
active_resource = if (!resource_id) || (resource_id.to_i < 0) | |
# set of resources | |
active_resource_class | |
else | |
# specific resource | |
active_resource_class.new(:id => resource_id) | |
end | |
response = active_resource.send(param[:method], *args) | |
# we got our response, but what to do with it? | |
if h = param[:response_handling] | |
h.call(response, workitem) | |
end | |
# reply to the engine | |
reply_to_engine(workitem) | |
end | |
protected | |
def param (key) | |
workitem.params[key] || @options[key] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment