Skip to content

Instantly share code, notes, and snippets.

View pascalesdedy's full-sized avatar

Pascales Kurniawan pascalesdedy

  • Not All Who Wander Are Lost - JRR Tolkien
  • Yogyakarta
View GitHub Profile
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));
@pascalesdedy
pascalesdedy / application_controller.rb
Created October 21, 2018 19:27
app/controller/application_controller.rb - Rails api server using TDD
class ApplicationController < ActionController::API
include Response
include ExceptionHandler
end
@pascalesdedy
pascalesdedy / response and exception_handler .rb
Created October 21, 2018 19:25
app/controller/concerns files - Simple Rails API server - TDD
#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
@pascalesdedy
pascalesdedy / response and exception_handler .rb
Created October 21, 2018 19:25
app/controller/concerns files - Simple Rails API server - TDD
#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
@pascalesdedy
pascalesdedy / articles_controller.rb
Created October 21, 2018 19:18
app/controller/articles_controller.rb - Simple rails api server TDD
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
@pascalesdedy
pascalesdedy / routes.rb
Created October 21, 2018 19:15
app/config/routes.rb - simple rails API TDD
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
resources :articles
end
@pascalesdedy
pascalesdedy / rails_helper.rb
Last active October 21, 2018 20:18
spec/rails_helper.rb - used for creating simple CRUD TDD
# 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
@pascalesdedy
pascalesdedy / request_spec_helper.rb
Last active October 21, 2018 20:20
spec/support/request_spec_helper.rb - used for creating simple CRUD TDD
module RequestSpecHelper
# Parse JSON response to ruby hash
def json
JSON.parse(response.body)
end
end
@pascalesdedy
pascalesdedy / article_spec.rb
Last active October 21, 2018 19:11
/spec/request/articles_spec.rb - used for creating simple CRUD TDD
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
@pascalesdedy
pascalesdedy / article.rb
Last active October 20, 2018 15:28
spec/factories/article.rb - used for creating CRUD using simple TDD
FactoryBot.define do
factory :article do
title { Faker::Lorem.word }
content { Faker::Lorem.paragraph(2) }
end
end