SOAP Rails + Flex and typed objects

Ruby on Rails gives the opportunity to build SOAP webservices in few lines. But as Ruby is a dynamic language, we have to define manually the types for inputs and outputs. From the point of view of Flex there is no problem to recover simple types as String, Integer, Datetime… etc. But how can we recover a more complex object ?
In the previous post we have introduced SchemaTypeRegistry which allows recovering personalized objects in Flex; no problems for services written in Java or C#, objects created with theses languages will be correctly defined in the wsdl and Flex will understand them without problems. However, we can imagine that we have a Ruby model called User, which point to the table users. Recovering this object in the SOAP, Flex will not be capable to understand it because none of its properties is explicitly defined.
A solution to this problem exists yet: ActionWebService::Struct. In defining a model inheriting from this class we could build a “package” of our based model and define these properties into it.

Example for a User model :
We define the UserStruct < ActionWebService::Struct > model and we add the properties :

class UserStruct
member :name, :string
member :pass, :string
end

and in our API class we specify the return as :

:returns =&gt; [{:answer =&gt;UserStruct}]

As Ruby is totally dynamic we can directly resend a User object in the controller, which one will be automatically transformed into a UserStruct. Example of a method in the controller :

def getUser(id)
User.find(id)
end

The received object will then be typed as userstruct (lowercase in the XML) and will contain all the values of User if they have been correctly defined.
Finally for a better readability we can name our object “User” in Flex too and specify the changed of the name in SchemaTypeRegistry (see previous post).