ActionWebService is back

Posted on July 02, 2008

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:

  1. Add this line to your config/environment.rb file in the initializer section:

    config.gem 'datanoise-actionwebservice', :lib => 'actionwebservice'
    
  2. Add this require statement to test/test_helper.rb file:

    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:

  1. We use scaffolding in the development mode, so we can easily test our API via the browser. Just go to http://localhost:3000/post/invocation and follow the screens to make an API call.

  2. 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"> ...
    
  3. 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!

ruby-debug basics screencast

Posted on May 10, 2007

Brian Donovan shows the basics of using ruby-debug. This is very nice addition to my already outdated tutorial. He also announced that his next screencast will cover debugging of a Rails application.

One the same note, the next version of Rails comes with the native ruby-debug support. The relevant changeset is here.

ruby-debug version 0.9.1 has been released!

Posted on April 03, 2007

Installation

As usual:

    $ sudo gem in ruby-debug

Changes

This is mainly a maintenance release, which includes several important bugfixes related to frame calculations.

In particular,

acts_as_sphinx plugin

Posted on March 23, 2007

We can't imagine a web site without at least a rudimentary search functionality. What a frustration when we shop for a particular product or looking for an answer on a community web site and can't find exactly what we are looking for. The search is the paramount for any online business. If a customer can't find a product she's looking for, she would go somewhere else and you lose a sale.

Unfortunately, most of the databases don't provide full-text search capabilities or provide a minimal support which is not enough in most of the cases.

TextMate and Quick Searches

Posted on March 19, 2007

I've been using TextMate since probably the first public version. At least my license dialog shows October 6th 2004, Serial Number #60. And, oh boy, I've been a happy user. I can't declare that this is the best editor out there, and I don't want to start another editor vs war. I've been using Vi(m)/emacs for such a long time that I can't even remember and I'm not going to give up on them just yet. But I can say that TextMate is definitely a unique piece of software that sets a high mark for other editors to follow.

I think the most innovative feature of this editor is the way it represents the document structure with scopes. Everything else is built around this structure: shortcuts, snippets, commands, code highlighting, etc. This very feature makes it so much easier to customize your editor, than anything I've seen so far. (Whatever I've tried I couldn't make mmm-mode package work for me).

Another thing that I'd love to have from any editor is a desktop integration. Don't get me wrong, I'm an avid terminal and CLI proponent, but I've access to the most powerful desktop out there and it would be sad if I can't enjoy it when I'm writing my documents. And TextMate allows me enjoying both of these worlds.

OK, enough of TextMate praises, there are still some areas where this editor needs some love. One of them is the search capability. The regular and incremental searches are fine, I'm talking about the Find in Project search. First of all, it's quite slow and it used to take a lot of your RAM. Second of all, I've found that in about 95% of all cases, I need to search in a particular directory of my project. So I did what everyone else does, I created my own command, which I am about to share with you.

Quick Search

This command uses grep utility to search in your documents. It's bound to ⌃⇧F keystroke and uses a selected text or prompts you for a query text. What makes it quite useful is that, if you have a project opened, you can select a directory in the project drawer and this command will limit the search only to this directory and all subdirectories. Also you can define the environment variable TM_QS_FILTER in your project settings with a value of a regular expression that selects all files and directories that you don't want to search in. For example, I'm using (tmp|log|rails)$ value for my Rails projects.

Enjoy.

Entering fixtures with TextMate

Posted on March 15, 2007

Probably the most boring task when writing Rails test cases is entering new fixtures. With a little help from TextMate this process can be made more pleasant.

  • Open Bundle Editor and create a new command.
  • Name it, for example, 'rails: new fixture'
  • Use this script as the command body:

ruby-debug version 0.8 has been released!

Posted on March 15, 2007

This is mostly a maintenance release. The major change is that ruby-debug has been split in two parts:

  1. ruby-debug-base gem contains the debugger core API written in C. Use this gem if you want to create your own interface to the debugger and don't want to depend on changes to the ruby-debug CLI. The API is considered somewhat stable.

  2. ruby-debug gem depends on ruby-debug-base and provides CLI interface.