-
You sometimes need to create an API
- You want frontend and backend separate
- You need REST API alongside the web app (GitHub, DNSimple, Travis)
-
JSON API specifications
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 "benchmark/ips" | |
| File.write "minitest_test.rb", <<-EOS | |
| require "minitest/autorun" | |
| require "minitest/pride" | |
| class MintestTest < Minitest::Test | |
| def test_foo | |
| assert true | |
| 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
| gem "active_model_serializers", "0.10.0.rc1" | |
| require "yaks" | |
| require "active_record" | |
| require "active_model_serializers" | |
| require "benchmark/ips" | |
| ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "testing") | |
| ActiveRecord::Schema.define do | |
| create_table :users, force: true do |t| |
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
| # 1. Models (Entities) | |
| # | |
| # * The nouns of your business logic | |
| # * Usually persisted in the database | |
| # * Usually contain validations | |
| # * Usually expose associations with other models | |
| # * Usually have scopes (to encapsulate the query logic) | |
| ################ | |
| # ActiveRecord # |
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
| ################# | |
| # CLASSIC STYLE # | |
| ################# | |
| class SessionsController < ApplicationController | |
| # ... | |
| def create | |
| if user = AuthenticateUser.call(params[:username], params[:password]) | |
| sign_in!(user) |
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 SocialPresenter | |
| # ... | |
| class Twitter < Struct.new(:object) | |
| def message | |
| case object | |
| when String |
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 "active_record" | |
| ActiveRecord::Base.establish_connection('postgres:///testing') | |
| ActiveRecord::Migration.verbose = false | |
| ActiveRecord::Migration.class_eval do | |
| create_table :played_quizzes, force: true do |t| | |
| t.integer :player_ids, array: true | |
| t.json :quiz_snapshot | |
| 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 "mail" | |
| Mail.defaults do | |
| delivery_method :smtp, | |
| address: "smtp.gmail.com", | |
| port: 587, | |
| user_name: ENV["GMAIL_EMAIL"], | |
| password: ENV["GMAIL_PASSWORD"], | |
| authentication: "plain", | |
| end |