Complete rails json API example with Rspec tests

22,693

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

Share:
22,693
Christian Schlensker
Author by

Christian Schlensker

http://github.com/wordofchristian http://dribbble.com/webartisan

Updated on July 09, 2022

Comments

  • Christian Schlensker
    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
    Gazler almost 13 years
    Rails 3 in Action is certainly a well written book for TDD, has a very nice API section.
  • PhillipKregg
    PhillipKregg over 12 years
    Thanks for the book recommendation - I'm going to check it out.
  • Robin
    Robin about 12 years
    It was quite helpful, thanks.
  • Mark Bolusmjak
    Mark Bolusmjak over 11 years
    I can't seem to find an example of a spec that posts JSON to a controller.
  • Leo Cassarani
    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
    Mark Bolusmjak over 11 years
    @LeoCassarani, I meant the repo, not the book.
  • Leo Cassarani
    Leo Cassarani over 11 years
    This is what I was specifically referring to.
  • Jonathan Lin
    Jonathan Lin over 11 years
    The link to Ticketee helped, thanks!
  • JJD
    JJD over 11 years
    In 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
    Tony over 10 years
    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 github.com/toptierlabs/api_explorer Any comments or help improving the api is welcome. :)