Complete rails json API example with Rspec tests
Solution 1
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.
Solution 2
RubyGems.org is open source and has a great example of an API
Christian Schlensker
http://github.com/wordofchristian http://dribbble.com/webartisan
Updated on July 09, 2022Comments
-
Christian Schlensker almost 2 years
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.
-
Gazler almost 13 yearsRails 3 in Action is certainly a well written book for TDD, has a very nice API section.
-
PhillipKregg over 12 yearsThanks for the book recommendation - I'm going to check it out.
-
Robin about 12 yearsIt was quite helpful, thanks.
-
Mark Bolusmjak over 11 yearsI can't seem to find an example of a spec that posts JSON to a controller.
-
Leo Cassarani over 11 years@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.
-
Mark Bolusmjak over 11 years@LeoCassarani, I meant the repo, not the book.
-
Leo Cassarani over 11 yearsThis is what I was specifically referring to.
-
Jonathan Lin over 11 yearsThe link to Ticketee helped, thanks!
-
JJD over 11 yearsIn 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 however the singular path/spec/api/v1/
is used. Does this affect any tests? -
Tony over 10 yearsI 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 github.com/toptierlabs/api_explorer Any comments or help improving the api is welcome. :)