JSON - Schmason

JSON - Schmason

Using Rails for building Web APIs

B5b39c8f21b5bb1ab97852ed32c888ab?s=128

Jan Krutisch

October 16, 2012
Tweet

Transcript

  1. 1.

    JSON - Schmason Using Rails for building Web APIs WebTech

    Conference 2012 Jan Krutisch http://jan.krutisch.de/ Dienstag, 16. Oktober 12
  2. 2.

    JSON - Schmason Using Rails for building Web APIs WebTech

    Conference 2012 Jan Krutisch http://jan.krutisch.de/ Dienstag, 16. Oktober 12
  3. 69.

    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. 71.

    [ { "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. 73.

    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. 86.

    [ { "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. 87.

    + Kind of the right place + Full view infrastructure

    available + Easy to use Dienstag, 16. Oktober 12
  8. 96.

    { "books": [ { "id": 1, "author": "Jan Krutisch", "title":

    "The Website is Down" } ] } Dienstag, 16. Oktober 12
  9. 98.

    [ { "id": 1, "author": "Jan Krutisch", "title": "The Website

    is Down" } ] Dienstag, 16. Oktober 12
  10. 102.

    + mostly declarative, with fallbacks + no view code to

    mangle data structures - no helpers except URL helpers Dienstag, 16. Oktober 12
  11. 107.
  12. 109.

    + Mosty declarative + Full view infrastructure - Kinda ugly,

    kinda weird - (It get's weirder the more you look) Dienstag, 16. Oktober 12
  13. 122.

    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
  14. 124.

    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. 125.

    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
  16. 141.

    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