XMLRPC call with Ruby, example

In this example we will use XMLRPC call to Ruby. In other words we use RPC protocol to make
a request ‘POST’ to a server (in ruby). And get the response back to AppleScript.
The code in AppleScript is the same as other examples from me.

tell methodCall to its methodName:"add" params:{2, 3}

script methodCall
	on methodName:methodName params:params
		tell application "http://localhost:9070"
			call xmlrpc {method name:methodName, parameters:params}
		end tell
	end methodName:params:
end script

Here is the ruby code that you save to a file ex. xmlrpc_server.rb.
You need 2 modules xmlrpc and xmlparser. At least that is what I installed to get it to work.
It could be that you do not need xmlparser. If you have Xcode installed it could be that
you need to install a new version of Ruby.

gem install xmlrpc will install the module. And you maybe need also xmlparser.

If it doesn’t work and you use Apple’s version of Ruby.
You need to install a new one from ex. Brew. brew install ruby

I have test the XMLRPC server in Ruby with this version:
ruby 3.2.1 (2023-02-08 revision 31819e82c8) [arm64-darwin21]

require “xmlrpc/server”

s = XMLRPC::Server.new(9070)

s.add_handler(“add”) do |a,b|
a + b

s.add_handler(“div”) do |a,b|
if b == 0
raise XMLRPC::FaultException.new(1, “division by zero”)
a / b

s.set_default_handler do |name, *args|
raise XMLRPC::FaultException.new(-99, “Method #{name} missing” +
" or wrong number of parameters!")


Benchmark between Python vs Ruby…
The first script is Python and second is Ruby.