Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

first commit

  • Loading branch information...
commit 522649af6e76fb70d29417bd3160d7e99f2bca25 0 parents
@alexkv authored
7 .gitignore
@@ -0,0 +1,7 @@
+config
+Capfile
+
+coverage
+doc
+pkg
+
67 README.rdoc
@@ -0,0 +1,67 @@
+= rails-xmlrpc
+
+Rails has native support for xmlrpc. Most people are familiar with the 'xmlrpc/client' library. The 'xmlrpc/server' library examples mostly make an assumption that you will run a standalone server.
+
+xmlrpc-endpoint allows you to instead expose normal Rails controller methods via XMLRPC, tied to an xmlrpc endpoint route in your app.
+
+Install
+
+Run in terminal:
+ gem install rails-xmlrpc
+
+add row inside Gemfile:
+ gem "rails-xmlrpc"
+
+Setup your environment
+
+ # set up a route to the controller action "xe_index" in your routes.rb file
+ # (this "xe_index" action will be created for you by the xmlrpc-endpoint, the route must point at 'xe_index' action to work)
+ # In a future release, xmlrpc-endpoint may support auto-generation of this route, and customization
+
+ match 'api/xmlrpc' => 'my_controller#xe_index'
+
+Add this code to your controller:
+
+ class MyController < ApplicationController
+ exposes_xmlrpc_methods
+
+ add_method 'Container.method_name' do
+ return 'Hello World'
+ end
+
+ end
+
+Then, pointing an XMLRPC client at the defined route, your normal controller actions will handle the requests.
+
+ require 'xmlrpc/client'
+ server = XMLRPC::Client.new2("http://localhost:3000/api/xmlrpc")
+ server.call("Container.method_name")
+
+To use a custom namespace prefix on all exposed methods (for example, if using someone else's specified protocol like MetaWeblog[http://www.xmlrpc.com/metaWeblogApi]), declare a method_prefix:
+
+ class MyApiController < ApplicationController
+ exposes_xmlrpc_methods :method_prefix => "metaWeblog."
+
+ # This method will be exposed externally as "metaWeblog.newPost()"
+ def newPost(blogid, username, password, struct, publish)
+ ...
+ end
+ end
+
+Thanks to Nathan Crause for saving me some time on the details of avoiding the standalone server.
+http://nathan.crause.name/entries/programming/xlm-rpc-under-ruby-on-rails
+
+
+== Note on Patches/Pull Requests
+
+* Fork the project.
+* Make your feature addition or bug fix.
+* Add tests for it. This is important so I don't break it in a
+ future version unintentionally.
+* Commit, do not mess with rakefile, version, or history.
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
+* Send me a pull request. Bonus points for topic branches.
+
+== Copyright
+
+Copyright (c) 2011 Aleksei Kvitinskii, released under the MIT license.
53 Rakefile
@@ -0,0 +1,53 @@
+require 'rubygems'
+require 'rake'
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gem|
+ gem.name = "rails-xmlrpc"
+ gem.summary = %Q{Expose Rails controller actions as XMLRPC method calls.}
+ gem.description = %Q{Expose Rails controller actions as XMLRPC method calls.}
+ gem.email = "wkoffel@alum.mit.edu"
+ gem.homepage = "http://github.com/Axt/rails-xmlrpc"
+ gem.authors = ["Will Koffel"]
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
+ end
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
+end
+
+require 'rake/testtask'
+Rake::TestTask.new(:test) do |test|
+ test.libs << 'lib' << 'test'
+ test.pattern = 'test/**/test_*.rb'
+ test.verbose = true
+end
+
+begin
+ require 'rcov/rcovtask'
+ Rcov::RcovTask.new do |test|
+ test.libs << 'test'
+ test.pattern = 'test/**/test_*.rb'
+ test.verbose = true
+ end
+rescue LoadError
+ task :rcov do
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
+ end
+end
+
+task :test => :check_dependencies
+
+task :default => :test
+
+require 'rake/rdoctask'
+Rake::RDocTask.new do |rdoc|
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
+
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = "rails-xmlrpc #{version}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
1  init.rb
@@ -0,0 +1 @@
+require 'rails-xmlrpc'
75 lib/action_controller/acts/rails_xmlrpc.rb
@@ -0,0 +1,75 @@
+module ActionController
+ module Acts
+ module RailsXmlrpc
+ def self.included(controller)
+ controller.extend(ClassMethods)
+ end
+
+ module ClassMethods
+ def exposes_xmlrpc_methods(options = {})
+ configuration = { :method_prefix => nil }
+ configuration.update(options) if options.is_a?(Hash)
+
+ before_filter(:add_method_handlers, :only => [:xe_index])
+ class_eval <<-EOV
+ require 'xmlrpc/server'
+ include ActionController::Acts::RailsXmlrpc::InstanceMethods
+
+ def xe_method_prefix
+ '#{configuration[:method_prefix]}'
+ end
+
+ def self.add_method name, &block
+ @method_list ||= []
+ @method_list << { :name => name, :block => block}
+ end
+
+ def self.xe_method_list
+ @method_list ||= []
+ @method_list
+ end
+
+ def self.method_size
+ @method_list
+ end
+ EOV
+ end
+ end
+
+ module InstanceMethods
+ # TODO: add route automatically for this?
+ def xe_index
+ result = @xmlrpc_server.process(request.body)
+ puts "\n\n----- BEGIN RESULT -----\n#{result}----- END RESULT -----\n\n"
+ render :text => result, :content_type => 'text/xml'
+ end
+
+ private
+
+ def add_method_handlers
+ @xmlrpc_server = XMLRPC::BasicServer.new
+ # loop through all the methods, adding them as handlers
+ self.class.xe_method_list.each do |method|
+ puts "Adding XMLRPC method for #{method[:name].to_s}"
+ @xmlrpc_server.add_handler method[:name], &method[:block]
+ # method[:block].call
+ # end
+ end
+
+ @xmlrpc_server.add_handler 'demo3' do
+ self.class.method_size[0][:name]
+ end
+
+ self.class.instance_methods(false).each do |method|
+ unless ['xe_index', 'xe_method_prefix', 'xe_method_list'].member?(method)
+ puts "Adding XMLRPC method for #{method.to_s}"
+ @xmlrpc_server.add_handler(xe_method_prefix + method) do |*args|
+ self.send(method.to_sym, *args)
+ end
+ end
+ end
+ end
+ end
+ end
+ end
+end
8 lib/rails-xmlrpc.rb
@@ -0,0 +1,8 @@
+# This file loads when the gem is included, and does plugin-style initialization
+
+module ActionController::Acts
+ autoload :RailsXmlrpc, "#{File.dirname(__FILE__)}/action_controller/acts/rails_xmlrpc.rb"
+end
+
+ActionController::Base.send(:include, ActionController::Acts::RailsXmlrpc )
+
61 rails-xmlrpc.gemspec
@@ -0,0 +1,61 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{rails-xmlrpc}
+ s.version = "0.3.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Aleksei Kvitinskii","Will Koffel"]
+ s.date = %q{2011-04-10}
+ s.description = %q{Expose Rails controller actions as XMLRPC method calls.}
+ s.email = %q{aleksei.wm@gmail.com}
+ s.extra_rdoc_files = [
+ "README.rdoc"
+ ]
+ s.files = [
+ ".document",
+ ".gitignore",
+ "README.rdoc",
+ "Rakefile",
+ "VERSION",
+ "doc/created.rid",
+ "doc/files/README_rdoc.html",
+ "doc/fr_class_index.html",
+ "doc/fr_file_index.html",
+ "doc/fr_method_index.html",
+ "doc/index.html",
+ "doc/rdoc-style.css",
+ "init.rb",
+ "lib/action_controller/acts/rails_xmlrpc.rb",
+ "lib/rails_xmlrpc.rb",
+ "test/helper.rb",
+ "test/test_rails-xmlrpc.rb",
+ "rails-xmlrpc.gemspec"
+ ]
+ s.homepage = %q{http://github.com/Axy/rails-xmlrpc}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.5}
+ s.summary = %q{Expose Rails controller actions as XMLRPC method calls.}
+ s.test_files = [
+ "test/helper.rb",
+ "test/test_rails-xmlrpc.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
+ else
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
+ end
+ else
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
+ end
+end
+
10 test/helper.rb
@@ -0,0 +1,10 @@
+require 'rubygems'
+require 'test/unit'
+require 'shoulda'
+
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+require 'rails-xmlrpc'
+
+class Test::Unit::TestCase
+end
7 test/test_rails-xmlrpc.rb
@@ -0,0 +1,7 @@
+require 'helper'
+
+class TestRailsXmlrpc < Test::Unit::TestCase
+ should "probably rename this file and start testing for real" do
+ flunk "hey buddy, you should probably rename this file and start testing for real"
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.