Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JSON - Schmason

JSON - Schmason

Using Rails for building Web APIs

Jan Krutisch

October 16, 2012
Tweet

More Decks by Jan Krutisch

Other Decks in Technology

Transcript

  1. JSON - Schmason Using Rails for building Web APIs WebTech

    Conference 2012 Jan Krutisch http://jan.krutisch.de/ Dienstag, 16. Oktober 12
  2. JSON - Schmason Using Rails for building Web APIs WebTech

    Conference 2012 Jan Krutisch http://jan.krutisch.de/ Dienstag, 16. Oktober 12
  3. def index @books = Book.all respond_to do |format| format.html #

    index.html.erb format.json { render json: @books } end end Dienstag, 16. Oktober 12
  4. [ { "author": "Jan Krutisch", "created_at": "2012-10-14T16:29:17Z", "description": "A funny

    story", "id": 1, "isbn": "", "title": "The Website is Down", "updated_at": "2012-10-14T16:29:17Z" } ] Dienstag, 16. Oktober 12
  5. def index @books = Book.all respond_with @books.map{|b| { id: b.id,

    title: b.title, author: b.author, created_at: b.created_at }} end Dienstag, 16. Oktober 12
  6. [ { "book": { "id": 1, "title": "The Website is

    down", "author": "Jan Krutisch", "comments": [ { "text": "A shitty comment", "name": "Jan" }, { "text": "Another shitty comment", "name": "Felix Baumgartner" }, { "text": "I disagree!", "name": "Troll" } ] } } ] Dienstag, 16. Oktober 12
  7. + Kind of the right place + Full view infrastructure

    available + Easy to use Dienstag, 16. Oktober 12
  8. { "books": [ { "id": 1, "author": "Jan Krutisch", "title":

    "The Website is Down" } ] } Dienstag, 16. Oktober 12
  9. [ { "id": 1, "author": "Jan Krutisch", "title": "The Website

    is Down" } ] Dienstag, 16. Oktober 12
  10. + mostly declarative, with fallbacks + no view code to

    mangle data structures - no helpers except URL helpers Dienstag, 16. Oktober 12
  11. + Mosty declarative + Full view infrastructure - Kinda ugly,

    kinda weird - (It get's weirder the more you look) Dienstag, 16. Oktober 12
  12. require 'test_helper' require 'json' class BooksTest < ActionDispatch::IntegrationTest test "books.json

    should return valid json" do get "/books.json" body = JSON.parse(response.body) assert_not_nil body assert_equal 200, status end end Dienstag, 16. Oktober 12
  13. require 'test_helper' require 'json' class BooksRackTest < ActiveSupport::TestCase include Rack::Test::Methods

    def app; Rails.application; end test "should return valid json" do get '/books.json' assert last_response.ok? body = JSON.parse(last_response.body) assert_not_nil body end end Dienstag, 16. Oktober 12
  14. require 'test_helper' require 'json' class BooksRackTest < ActiveSupport::TestCase include Rack::Test::Methods

    def app; Rails.application; end test "should return valid json" do get '/books.json' assert last_response.ok? body = JSON.parse(last_response.body) assert_not_nil body end end Dienstag, 16. Oktober 12
  15. namespace :api_tests do desc "Testing the /books endpoint" task :books

    do require 'rest_client' require 'json' json = JSON.parse( RestClient.get('http://localhost:3000/books.json') ) exit(1) if (json.nil?) end end Dienstag, 16. Oktober 12