If you want to mount an XML-RPC webservice under Camping (with anything you might need in-between) or just steal Camping's raw POST request, you can do something like this:
class RPC < R('/megacorp-api.xml')
# Bypass Camping's own request parsing
def initialize(r, e, m)
# r is an IO-ish something, always. What is NOT always the same
# is whether it's IO-ish enough for XML-RPC, so to be on the safe
# side we tempfile it. It bites with FCGI otherwise, trust me.
@raw = Tempfile.new("camprpc")
@raw.write(r.read) until r.eof?; @raw.rewind
super(StringIO.new(''),e,m)
end
def post
begin
# Specifically because Ruby's XML RPC is stooped
@headers['Content-Type'] = 'text/xml'
s = XMLRPC::BasicServer.new(self)
# add any handlers you need
s.add_handler MyRPCService.new
s.process(@raw)
rescue Exception => e
$stderr.puts e.message, e.backtrace.join("\n")
b = Builder::XmlMarkup.new
b.methodResponse do; b.fault do; b.value do; b.struct do
b.member { b.name("faultCode"); b.value { b.int 1 } }
b.member { b.name("faultString"); b.value { b.string([e.class.to_s, e.message].join(' : ')) }}
end;end;end;end.to_s
end
end
end
You can accept requests as YAML or XML in a similar fashion. The exception handling is for the case when your XMLRPC service or the server raise during initialize.
