iPhone on Rails
Posted by Giuseppe Arici in
Ruby on Rails -
1 comment
Introduction to ObjectiveResource, the open source framework that connects the iPhone to the Ruby on Rails application
ObjectiveResource is a library that is written in Objective-C by YFactorial the purpose is to facilitate the integration between Ruby on Rails applications and programs for the iPhone.
As declared from the developers, the project starts from the idea to carry the potential of the module ActiveResource of Rails onto the iPhone .
The library supplies a system that is able to serialize the objects from and to web services RESTful standard of Rails (through XML o JSON) and makes the complexity requested transparent to invoke such as services from any programming language used on the iPhone.
Even if the library was recently released and is still under continues development, it is evident that the effort done by the developers in search of covering the functionality of ActiveResource and in the supply of an interface utilized along side fully conforms to the subsystem of Cocoa used as the management of the persistence of data, better known as Core Data as complete as possible in the world.
On the website dedicated to ObjectiveResource an introductive guide is available, quite useful just to give you an idea. It is also possible to download a project example that explains how simple and quick it is to use.
ObjectiveResource adds some methods to the class NSObject through the category NSObject(ObjectiveResource), in such a way that each instance of the Objective-C class can act from remote resource.
The name of the class and the name of the property must coincide with those from the remote resources of the Rails application. Given that the id is a reserved word in Objective-C, the property id of a Rails resource is mapped in Objective-C on a property, which name is composed from name of the class followed by suffix Id (like this for the class Dog such a property will be dogId)
The following code shows a Dog class that maps the correspondent Rails resource:
@interface Dog : NSObject {
NSString *name;
NSString *dogId;
}
@property (non-atomic , retain) NSString *name;
// map the property "id" of Rails
@property (non-atomic , retain) NSString *dogId;
@end
To use the library it is necessary for us to put certain parameters in the ObjectiveResourceConfig class:
//Set the Rails website. The last slash is mandatory.
[ObjectiveResourceConfig setSite:@"http://localhost:3000/"];
//Set the username and password requested to connect to the Rails site
[ObjectiveResourceConfig setUser:@"remoteResourceUserName"];
[ObjectiveResourceConfig setPassword:@"remoteResourcePassword"];
//Set the protocol to use for remote communication(XML o JSON)
[ObjectiveResourceConfig setResponseType:XmlResponse];//il default
[ObjectiveResourceConfig setResponseType:JSONResponse];
All the CRUD operations are supported.
//Create
Dog *dog = [[[Dog alloc] init] autorelease];
dog.name = @"Pluto";
[dog saveRemote];
//Read
NSArray *dogs = [Dog findAllRemote];
dog = [Dog findRemote:dog.dogId];
//Update
dog.name = @"Bobby";
[dog updateRemote];
//Delete
[dog destroyRemote];
Other than ObjectiveResource, connected resources are also supported.
For example to find all the dogs that belongs to one person, you only need to add the following method for the Dog class:
+ (NSArray *)findAllForPersonWithId:(NSString *)personId {
//using the generation method of the URL of ObjectiveResource to
//construct the path nested.
//http://localhost:3000/persons/:person_id/dogs.xml
NSString *dogPersonPath = [NSString stringWithFormat:@"%@%@/%@/%@%@",
[self getRemoteSite],
[Person getRemoteCollectionName],
personId,
[self getRemoteCollectionName],
[self getRemoteProtocolExtension]];
Response *res = [Connection get:dogPersonPath withUser:[[self class] getUser]
andPassword:[[self class] getPassword]];
return [self allFromXMLData:res.body];
}
The non CRUD custom actions are also supported. This is how you implement a method pet in the class Dog, that points to the URL http://localhost:3000/dogs/1/pet.xml:
- (void) pet {
//Construct the path to the custom action pet
NSString *petPath = [NSString stringWithFormat:@"%@%@/%@/pet%@",
[Dog getRemoteSite],
[Dog getRemoteCollectionName],
dogId,
[Dog getRemoteProtocolExtension]];
//Send the request
Response *res = [Connection get:petPath withUser:[[self class] getUser]
andPassword:[[self class] getPassword]];
//Modify the property with the values restored from the server
[self setProperties:[[Dog fromXMLData:res.body] properties];
}
@interface Dog : NSObject {
NSString *name;
NSString *dogId;
}
@property (nonatomic , retain) NSString *name;
@property (nonatomic , retain) NSString *dogId;
@end
#import "ObjectiveResourceConfig.h"
[ObjectiveResourceConfig setSite:@"http://localhost:3000"];
NSArray *dogs = [Dog findAllRemote];
To complete this example, the developers have made a short screencast available on vimeo and have announced that the next tutorial published is on Pragmatic Programmers:.
Getting Started with Objective Resource from Josh Vickery on Vimeo.
Although some applications already exist that uses ObjectiveResource, as they indicate on the project website, the library still has enough margins for improvement. An idea could be to implement an automatic system generation of class files, that maps the resources of the Rails application.
You can create a script in Ruby that is able to reduce the property such as classes navigating the website, or better, using the Rails API directly.
The opinion of ObjectiveResource can only be positive: good idea, good projecting and careful implementation. Welcoming instruments that facilitate integration between the world of web and mobile devices such as iPhone. The opportunity that this library offers are interesting and many. Think of a possible example to simply develop functional applications for the iPhone, that makes it possible directly in real-time on the terminal of the user an altogether well-defined and limited to updated data (for example earnings of the quotations, on the variation of prices, on the unsold goods, ...) without having to develop the mobile version of the whole application.
At the end of this library it allows you to quickly develop with Ruby on Rails to the flexibility of the utilization of the iPhone: agility to the nth power!


Comments
Max
Posted on February 27