I'm looking for a good example repository of a Rails 3 project that implements a JSON API and has included RSpec tests. A full tutorial would be great but I'd settle for just a well commented example.
-
Not to promote myself too much, but I wrote a tutorial on creating an oauth2 based API, however some general concepts will still apply, and there are a couple specs in there you can take a look at. https://github.com/Gazler/Oauth2-Tutorial – Gazler Jul 30 '11 at 19:41
-
3are there any up to date tutorials with rails 3.2 available? Because the repo is over 2 years old, maybe there are better solutions now, or some concepts has changed – 23tux Feb 17 '13 at 15:41
3 Answers
Yehuda Katz and Ryan Bigg's excellent upcoming book, Rails 3 in Action, has a chapter on building a test-driven JSON API on top of the example application that runs through the entire book.
While I strongly recommend buying a copy of the beta edition of the book, the code for the sample application, Ticketee, is open source and can be found on GitHub. In particular, everything API-related is namespaced under /app/controllers/api
and you can find plenty of RSpec tests in /spec/api
.
I was in your same position a couple of months ago when I was having to write a JSON API for an iPhone app so I'm well aware of the lack of good, test-driven, relevant tutorials online. Lucky for me I found out about the book and it turned out to be an absolute life saver.

- 1,289
- 10
- 14
-
Rails 3 in Action is certainly a well written book for TDD, has a very nice API section. – Gazler Jul 31 '11 at 15:29
-
-
-
I can't seem to find an example of a spec that posts JSON to a controller. – Mark Bolusmjak Aug 27 '12 at 21:44
-
@z5h: Really? Section 13.1.6 ("Creating projects") of Rails 3 in Action shows an example of POSTing to an API endpoint and passing some JSON parameters to create a new database record. – Leo Cassarani Sep 02 '12 at 20:18
-
-
[This](https://github.com/rails3book/ticketee/blob/master/spec/api/v1/projects_spec.rb#L44) is what I was specifically referring to. – Leo Cassarani Sep 07 '12 at 20:06
-
-
2In the book the author recommends to create the path `/spec/apis/v1/` in its **plural** version as mentioned in chapter 13.1.1 on page 351. In the [Ticketee example](https://github.com/rails3book/ticketee/tree/master/spec/api/v1) however the **singular path** `/spec/api/v1/` is used. Does this affect any tests? – JJD Dec 20 '12 at 19:17
-
I deployed a Rails engine (packed as a gem) that is really useful to debug APIs on rails. You just have to mount the engine and go to the url that you specified, i.e. “localhost:3000/api_explorer” to see it. It’s a way of documenting an API also, reading the webservices specification from a file. The gem is named ‘api_explorer’ and the repo is http://www.github.com/toptierlabs/api_explorer Any comments or help improving the api is welcome. :) – Tony Nov 21 '13 at 19:13
Can't necessarily answer the api example, but I can definitely vouch for the API testing of a rails 3 app in processes.
Here you go: whitneytaylorimura.wordpress.com
It has a full example using Cucumber, Rspec, & FactoryGirl to create your objects (with DatabaseCleaner to clean them up).

- 810
- 1
- 6
- 15