Skip to content

Instantly share code, notes, and snippets.

@simon-engledew
Created June 23, 2011 08:53
Show Gist options
  • Save simon-engledew/1042176 to your computer and use it in GitHub Desktop.
Save simon-engledew/1042176 to your computer and use it in GitHub Desktop.
XMLRPC
# config/environment.rb => require 'xmlrpc/client'
#XMLRPC::Config.const_set(:ENABLE_NIL_CREATE, true)
XMLRPC::Config.const_set(:ENABLE_NIL_PARSER, true) unless XMLRPC::Config.const_defined?(:ENABLE_NIL_PARSER)
module XmlRpc
class Api
class << self
def method_missing(method, *args)
call(method, *args)
end
def call(method, *args)
client = XMLRPC::Client.new_from_uri(@host, nil, 90)
begin
return client.call(@prefix ? "#{@prefix}.#{method}" : method, *args)
rescue Errno::ECONNREFUSED => e
raise BadGateway.new
rescue XMLRPC::FaultException => e
# e.faultCode
raise
ensure
begin
client.instance_variable_get(:@http).finish
rescue
end
end
end
def site(host, prefix = nil)
@host = host
@prefix = prefix != :default ? prefix : self.to_s.underscore.gsub(/_api$/, '')
end
end
end
end
# app/apis/backend_api.rb
"""
class BackendApi < XmlRpc::Api
site("127.0.0.1:8080")
class << self
# override the remote method ping to shout louder
def ping
super.upcase
end
# no need to define the remote method echo
end
end
BackendApi.echo => "echo"
BackendApi.ping => "PONG"
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment