Some code that I wrote

20 August 2010 Some code that I wrote

Between working on various client projects, I've been hacking away at various new open source projects over the last month or two that I want to share.

I've been meaning to write a more detailed blog post about most of these but just haven't found the time. I still plan to cover each of these in more detail but I just wanted to get this out there in the meantime.

LRResty, an Objective-C REST client

Yes, another one. LRResty, is a simple Objective-C client for interacting with REST web services, modelled on the excellent RestClient Ruby gem.

When I started LRResty, I had the following objectives in mind: it should have a clear, succinct API, it should be decoupled from your domain model, it should build upon proven APIs (NSURLConnection, NSOperation) to provide asynchronous behaviour and it should take full advantage of Objective-C blocks.

Here's what it looks like:

NSString *result = nil;

[[LRResty client] get:@"http://www.example.com/some/resource" 
            withBlock:^(LRRestyResponse *response) {
              
  if [response.status == 200] {
    result = [response asString]
  }
}];

Like RestClient, LRResty can be used in a more resource-oriented fashion:

LRRestyResource *resource = [LRResty resource:@"http://www.example.com"];

[[resource at:@"/some/path"] get:
    ^(LRRestyResponse *response, LRRestyResource *resource) {
  // handle response
}];

There is some other cool stuff like request modifiers, small blocks that can be used to modify all requests before they are dispatched which are a really easy way of doing things such as adding authorization and content-type headers to all of your requests, but I'll cover these in more detail in another post.

In the meantime, you can check our LRResty on GitHub. There is no documentation, not even a README right now, but I encourage you to take a look at the project's acceptance tests and examples.

LRMocky, an Objective-C mock object library

LRMocky is a port of the Java mock object library, JMock. I decided to write my own mocking framework after becoming incredibly frustrated with OCMock.

Why JMock? Having recently read this brilliant book, which uses JMock in a significant way, I grew to appreciate its API and felt that it was a better fit for Objective-C then something like Mocha, my Ruby mocking library of choice.

LRMocky tries to mirror the JMock API wherever possible although it is not always possible due to syntax constraints. Here's a brief example:

- (void)testSuccessfulMocking
{
  LRMockery *context = [LRMockery mockeryForTestCase:self];

  id testObject = [context mock:[NSString class] named:@"My Mock String"];

  [context checking:^(that){
    [[oneOf(testObject) receives] uppercaseString];
  }];

  [testObject uppercaseString];
  [context assertSatisfied];
}

Again, I would encourage you read the project README and take a look at the tests.

LRTableModel, the missing link for UITableView

I'll keep this one brief because the README says everything you need to know.

Having written one too many implementations of a UITableViewController and attempted several different ways of abstracting the repetitive nature of UITableViewDataSource, I finally hit upon the table model concept, as used by the Java Swing JTable API.

This project is still in it's early days but my early experiments seem to indicate that this abstraction works. More to come on this one.

Mimic, a Ruby gem to help fake web services

A late entry, Mimic is a small Ruby library that I've been working on in the last couple of days.

It builds on top of Rack and Sinatra and provides a really simple way of starting up a server to act as a stand-in for an external API that can be used when writing integration/end-to-end tests.

Because it actually starts up an HTTP server it is not limited to testing Ruby apps or libraries (ala FakeWeb or WebMock) and I have some interesting ideas on how it can be used when testing iPhone apps (you can see this idea in the acceptance tests for LRResty, which fires up a Sinatra app during it's build phase to perform tests against).

Mimic is available as a gem.

Feedback

As always, I'm interested to hear any feedback people might have on my open-source projects. I'd also love to hear from you if you are using any of my code in one of your projects.

You can drop me an email or send me a message on Twitter. If you'd like to keep informed of the progress of any of these projects, the best way is to simply follow me on Github.