This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Public gist |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| r = require('rethinkdb'); | |
| var connection = null; | |
| r.connect( {host: 'localhost', port: 28015}, function(err, conn) { | |
| if (err) throw err; | |
| connection = conn; | |
| r.db('test').tableCreate('authors').run(connection, function(err, result) { | |
| if (err) throw err; | |
| console.log(JSON.stringify(result, null, 2)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ApplicationController < ActionController::API | |
| include Response | |
| include ExceptionHandler | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #app/controller/concerns/response.rb | |
| module Response | |
| def json_response(object, status = :ok) | |
| render json: object, status: status | |
| end | |
| end | |
| #app/controller/concerns/exception_handler.rb | |
| module ExceptionHandler | |
| # provides the more graceful `included` method |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #app/controller/concerns/response.rb | |
| module Response | |
| def json_response(object, status = :ok) | |
| render json: object, status: status | |
| end | |
| end | |
| #app/controller/concerns/exception_handler.rb | |
| module ExceptionHandler | |
| # provides the more graceful `included` method |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ArticlesController < ApplicationController | |
| skip_before_action :verify_authenticity_token | |
| before_action :set_article, only: [:show, :update, :destroy] | |
| # GET /articles | |
| def index | |
| @articles = Article.all | |
| json_response(@articles) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Rails.application.routes.draw do | |
| # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html | |
| resources :articles | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # spec/rails_helper.rb | |
| # [...] | |
| Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } | |
| # [...] | |
| RSpec.configuration do |config| | |
| # [...] | |
| config.include RequestSpecHelper, type: :request | |
| # [...] | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module RequestSpecHelper | |
| # Parse JSON response to ruby hash | |
| def json | |
| JSON.parse(response.body) | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'rails_helper' | |
| RSpec.describe 'articles API', type: :request do | |
| # initialize test data | |
| let!(:articles) { create_list(:article, 10) } | |
| let(:article_id) { articles.first.id } | |
| # Test suite for GET /articles | |
| describe 'GET /articles' do | |
| # make HTTP get request before each example |
NewerOlder