Ruby HOWTO

From Enso Wiki

Jump to: navigation, search

Following is a ruby implementation of the Python_HOWTO.

require "xmlrpc/client"
require "xmlrpc/server"
 
class MyEnsoExtension
  
  def initialize
    @enso = XMLRPC::Client.new("127.0.0.1", "/", 11374).proxy
  end
  
  def callCommand( commandName, postfix )
    selectedText = @enso.getUnicodeSelection
    returnText = selectedText.upcase
    @enso.setUnicodeSelection returnText, "ooper case"
    return true
  end
 
end
 
class EnsoExtensionServer
  
  def initialize()
    @enso = XMLRPC::Client.new("127.0.0.1", "/", 11374).proxy
    register_commands
    server = XMLRPC::Server.new(11375, "127.0.0.1", 4, $stdout, true, true, '')
    server.add_handler("", MyEnsoExtension.new)
    begin
      server.serve
    rescue
    ensure
      unregister_commands
    end
  end
  
  def register_commands
    @enso.registerCommand( 
      # The URL of your XML-RPC server
      "http://127.0.0.1:11375",
      # The name of the command.
      "ooper case",
      # One-line description of the command, which will be shown
      # in the command quasimode.
      "Ooper-cases your currently selected text (Ruby version)",
      # XHTML fragment containing help text for the command,
      # which will be shown if the user executes the 'help'
      # command on this command.
      "<p>This command transforms the current
      text selection the ooper-cased version of
      that text.</p>",
 
      # The postfix or argument type of the command.
      # Oopercase takes a selection, not an argument, so
      # it takes 'none' as its postfix.
      "none"
    )
  end
  
  def unregister_commands
    @enso.unregisterCommand "http://127.0.0.1:11375", "ooper case"
  end
end
 
EnsoExtensionServer.new