I've been porting several of my old applications to the latest version of Rails. It turned out not that a difficult thing to do. But unfortunately some of them rely on ActionWebService gem that appears to be left behind of the latest Rails development. Because of the lack of time, I stepped away from being a maintainer of this library some time ago and it seems that no one has taken this position. Not surprisingly, building REST API is so much easier and kosher nowadays.
For those of you who still need to provide SOAP/XML-RPC API, I've uploaded my port of ActionWebService to GitHub. Note that it depends on Rails version 2.1.0.
This is how you install it:
$ sudo gem install datanoise-actionwebservice --source http://gems.github.com
Below is a small refresher of how to use this thing:
Configuration
Assuming that you installed the gem:
Add this line to your
config/environment.rbfile in the initializer section:config.gem 'datanoise-actionwebservice', :lib => 'actionwebservice'Add this require statement to
test/test_helper.rbfile:require 'action_web_service/test_invoke'
Generating API controller
ActionWebService gem includes web_service generator that you can use like this:
$ ./script/generate web_service post
exists app/services/
exists app/controllers/
exists test/functional/
create app/services/post_api.rb
create app/controllers/post_controller.rb
create test/functional/post_api_test.rb
Note that your Apis are placed into app/services directory which Rails automatically includes in the search path, instead of the old app/apis directory.
Define your API
Open app/services/post_api.rb file and add methods that your service exposes:
class PostApi < ActionWebService::API::Base
api_method :get_posts, :returns => [[:string]]
end
Here, we defined get_posts method that accepts no parameters and returns an array of strings.
API implementation
We are going to use direct dispatching mode, so all methods that implement our API go directly to PostController itself:
class PostController < ApplicationController
wsdl_service_name 'Post'
web_service_api PostApi
web_service_scaffold :invocation if Rails.env == 'development'
def get_posts
["Post 1", "Post 2"]
end
end
Several things need to mention here:
We use scaffolding in the development mode, so we can easily test our API via the browser. Just go to
http://localhost:3000/post/invocationand follow the screens to make an API call.You can get WSDL of our Post API from
http://localhost:3000/post/wsdl:$ curl http://localhost:3000/post/wsdl <?xml version="1.0" encoding="UTF-8"?> <definitions name="Post" xmlns:typens="urn:ActionWebService"> ...The URL that you actually use to make API calls is
http://localhost:3000/post/api
Unit Testing
As you probably noticed, our web_service generator created a unit test file test/functional/post_api_test.rb. Lets test our get_posts method:
require File.dirname(__FILE__) + '/../test_helper'
require 'post_controller'
class PostController; def rescue_action(e) raise e end; end
class PostControllerApiTest < Test::Unit::TestCase
def setup
@controller = PostController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
def test_get_posts
result = invoke :get_posts
assert_equal(["Post 1", "Post 2"], result)
end
end
Lets see if it works:
$ ruby test/functional/post_api_test.rb
Loaded suite test/functional/post_api_test
Started
.
Finished in 0.182469 seconds.
1 tests, 1 assertions, 0 failures, 0 errors
OK, we are good to go!
What changes if any were made in the port? I run AWS with Rails 2.1 fine in a few of may apps. Glad to see the code on github :)
Zack,
I made numerous changes, but most important ones are:
Fixed casting of :time type parameters.
Renamed template files according to new .html.erb naming schema.
Made all unit tests pass again.
Recovered generator classes from rails-1.2.6 gem
Basically, I just wanted to have an easy way to install this library for my old applications. :)
Great!
Glad to hear that a maintainable branch is up and easy to fork... because unfortunately this is a much needed library for many of my apps too...
Thanks very much for this, I was struggling to get AWS working with 2.1.
Your instructions worked fine but in case anyone else hits the same problem as I did - namely a cryptic error loading the environment, you might need to run
gem update --systemif you have not used 'config.gem' before. More details of the problem are here:http://rails.lighthouseapp.com/projects/8994/tickets/462-loading-environment-fails-with-an-outdated-version-of-rubygems-while-using-config-gem-some_gem
I ought to mention I also had to cp actionwebservice.rb datanoise-actionwebservice.rb in lib/ before config.gem would find the gem.
steve
Steve was dead right on both those counts. Please update that package with the copied file...I'm not git-enabled yet myself.
The solution is to add
:lib => 'actionwebservice'to the config.gemHi, if i try to access a datetime column of an activerecord object (ie createdat or modifiedat) , i get the following error. The error does not appear, if the column contains NULL. Anybody knows a a workaround or fix for this problem?
I am having a similar problem. I believe it happened after we upgraded to rails v2.1.
Any help would be greatly appreciated.
We're leaning towards the changes to the date object in Rails v2.1.
Appears that there is a problem with running AWS against JRuby which is a pity ...
http://jira.codehaus.org/browse/JRUBY-2446
I certainly run into this error when using JRuby 1.1.2, Rails 2.1.0, and datanoise-actionwebservice.
Geoff.
For some reason Exeption Notifier does not send me emails if an exception occurs in code being called from the AWS XML-RPC interface. Does anyone know how to get that working with AWS?