├── .gitignore ├── Gemfile ├── Gemfile.lock ├── README.md ├── Rakefile ├── app ├── controllers │ ├── application_controller.rb │ ├── books_controller.rb │ └── concerns │ │ └── .keep ├── jobs │ └── application_job.rb ├── mailers │ └── .keep ├── models │ ├── .keep │ ├── book.rb │ └── concerns │ │ └── .keep └── serializers │ └── book_serializer.rb ├── bin ├── bundle ├── rails ├── rake ├── setup └── update ├── config.ru ├── config ├── application.rb ├── boot.rb ├── database.yml ├── environment.rb ├── environments │ ├── development.rb │ ├── production.rb │ └── test.rb ├── initializers │ ├── active_model_serializer.rb │ ├── active_record_belongs_to_required_by_default.rb │ ├── application_controller_renderer.rb │ ├── backtrace_silencers.rb │ ├── callback_terminator.rb │ ├── cors.rb │ ├── filter_parameter_logging.rb │ ├── inflections.rb │ ├── mime_types.rb │ ├── request_forgery_protection.rb │ ├── swagger.rb │ └── wrap_parameters.rb ├── locales │ └── en.yml ├── routes.rb └── secrets.yml ├── db ├── migrate │ └── 20151127182546_create_books.rb ├── schema.rb └── seeds.rb ├── lib └── tasks │ └── .keep ├── public ├── 404.html ├── 422.html ├── 500.html ├── api-docs.json ├── books.json ├── favicon.ico └── robots.txt ├── spec ├── controllers │ └── books_controller_spec.rb ├── models │ └── book_spec.rb ├── rails_helper.rb ├── requests │ └── books_spec.rb ├── routing │ └── books_routing_spec.rb └── spec_helper.rb └── test ├── controllers ├── .keep └── books_controller_test.rb ├── fixtures ├── .keep ├── books.yml └── files │ └── .keep ├── integration └── .keep ├── mailers └── .keep ├── models ├── .keep └── book_test.rb └── test_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files for more about ignoring files. 2 | # 3 | # If you find yourself ignoring temporary files generated by your text editor 4 | # or operating system, you probably want to add a global ignore instead: 5 | # git config --global core.excludesfile '~/.gitignore_global' 6 | 7 | # Ignore bundler config. 8 | /.bundle 9 | 10 | # Ignore the default SQLite database. 11 | /db/*.sqlite3 12 | /db/*.sqlite3-journal 13 | 14 | # Ignore all logfiles and tempfiles. 15 | /log 16 | /tmp 17 | !/log/.keep 18 | !/tmp/.keep 19 | 20 | /.idea 21 | .rspec -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gem 'rails', path: "/Users/gwalchmai/Dev/webinar/rails" 4 | gem 'sprockets-rails', github: "rails/sprockets-rails" 5 | gem 'sprockets', github: "rails/sprockets" 6 | gem 'sass-rails', github: "rails/sass-rails" 7 | gem 'arel', github: "rails/arel" 8 | gem 'rack', github: "rack/rack" 9 | 10 | # Use sqlite3 as the database for Active Record 11 | gem 'sqlite3' 12 | # Use ActiveModel has_secure_password 13 | # gem 'bcrypt', '~> 3.1.7' 14 | 15 | # Use Unicorn as the app server 16 | # gem 'unicorn' 17 | 18 | # Use Capistrano for deployment 19 | # gem 'capistrano-rails', group: :development 20 | 21 | # Use ActiveModelSerializers to serialize JSON responses 22 | gem 'active_model_serializers', '~> 0.10.0.rc2' 23 | 24 | # Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible 25 | # gem 'rack-cors' 26 | 27 | gem 'swagger-docs' 28 | 29 | group :development, :test do 30 | # Call 'byebug' anywhere in the code to stop execution and get a debugger console 31 | gem 'byebug' 32 | gem 'rspec-rails' 33 | gem 'rails-controller-testing' 34 | end 35 | 36 | group :development do 37 | 38 | # Loading the listen gem enables an evented file system monitor. Check 39 | # https://github.com/guard/listen#listen-adapters if on Windows or *BSD. 40 | # gem 'listen', '~> 3.0.5' 41 | end 42 | 43 | # Windows does not include zoneinfo files, so bundle the tzinfo-data gem 44 | gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 45 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GIT 2 | remote: git://github.com/rack/rack.git 3 | revision: c393176b0edf3e5d06cabbb6eb9d9c7a07b2afa7 4 | specs: 5 | rack (2.0.0.alpha) 6 | json 7 | 8 | GIT 9 | remote: git://github.com/rails/arel.git 10 | revision: 3c429c5d86e9e2201c2a35d934ca6a8911c18e69 11 | specs: 12 | arel (7.0.0.alpha) 13 | 14 | GIT 15 | remote: git://github.com/rails/sass-rails.git 16 | revision: 6e4eee736bcbfa5b2962467673c7a51abf434c67 17 | specs: 18 | sass-rails (6.0.0) 19 | railties (>= 4.0.0, < 5.0) 20 | sass (~> 3.4) 21 | sprockets (>= 4.0) 22 | sprockets-rails (< 4.0) 23 | 24 | GIT 25 | remote: git://github.com/rails/sprockets-rails.git 26 | revision: 06d84e952490d7d391592124f6e3b79f9c596674 27 | specs: 28 | sprockets-rails (3.0.0.beta3) 29 | actionpack (>= 4.0) 30 | activesupport (>= 4.0) 31 | sprockets (>= 3.0.0) 32 | 33 | GIT 34 | remote: git://github.com/rails/sprockets.git 35 | revision: 98d0ccc42949815d959cf95fbffd52a3df650358 36 | specs: 37 | sprockets (4.0.0) 38 | rack (>= 1, < 3) 39 | 40 | PATH 41 | remote: /Users/gwalchmai/Dev/webinar/rails 42 | specs: 43 | actionmailer (5.0.0.alpha) 44 | actionpack (= 5.0.0.alpha) 45 | actionview (= 5.0.0.alpha) 46 | activejob (= 5.0.0.alpha) 47 | mail (~> 2.5, >= 2.5.4) 48 | rails-dom-testing (~> 1.0, >= 1.0.5) 49 | actionpack (5.0.0.alpha) 50 | actionview (= 5.0.0.alpha) 51 | activesupport (= 5.0.0.alpha) 52 | rack (~> 2.x) 53 | rack-test (~> 0.6.3) 54 | rails-dom-testing (~> 1.0, >= 1.0.5) 55 | rails-html-sanitizer (~> 1.0, >= 1.0.2) 56 | actionview (5.0.0.alpha) 57 | activesupport (= 5.0.0.alpha) 58 | builder (~> 3.1) 59 | erubis (~> 2.7.0) 60 | rails-dom-testing (~> 1.0, >= 1.0.5) 61 | rails-html-sanitizer (~> 1.0, >= 1.0.2) 62 | activejob (5.0.0.alpha) 63 | activesupport (= 5.0.0.alpha) 64 | globalid (>= 0.3.0) 65 | activemodel (5.0.0.alpha) 66 | activesupport (= 5.0.0.alpha) 67 | builder (~> 3.1) 68 | activerecord (5.0.0.alpha) 69 | activemodel (= 5.0.0.alpha) 70 | activesupport (= 5.0.0.alpha) 71 | arel (= 7.0.0.alpha) 72 | activesupport (5.0.0.alpha) 73 | concurrent-ruby (~> 1.0) 74 | i18n (~> 0.7) 75 | json (~> 1.7, >= 1.7.7) 76 | method_source 77 | minitest (~> 5.1) 78 | tzinfo (~> 1.1) 79 | rails (5.0.0.alpha) 80 | actionmailer (= 5.0.0.alpha) 81 | actionpack (= 5.0.0.alpha) 82 | actionview (= 5.0.0.alpha) 83 | activejob (= 5.0.0.alpha) 84 | activemodel (= 5.0.0.alpha) 85 | activerecord (= 5.0.0.alpha) 86 | activesupport (= 5.0.0.alpha) 87 | bundler (>= 1.3.0, < 2.0) 88 | railties (= 5.0.0.alpha) 89 | sprockets-rails (>= 2.0.0) 90 | railties (5.0.0.alpha) 91 | actionpack (= 5.0.0.alpha) 92 | activesupport (= 5.0.0.alpha) 93 | method_source 94 | rake (>= 0.8.7) 95 | thor (>= 0.18.1, < 2.0) 96 | 97 | GEM 98 | remote: https://rubygems.org/ 99 | specs: 100 | active_model_serializers (0.10.0.rc3) 101 | actionpack (>= 4.0) 102 | activemodel (>= 4.0) 103 | railties (>= 4.0) 104 | builder (3.2.2) 105 | byebug (8.2.1) 106 | concurrent-ruby (1.0.0) 107 | diff-lcs (1.2.5) 108 | erubis (2.7.0) 109 | globalid (0.3.6) 110 | activesupport (>= 4.1.0) 111 | i18n (0.7.0) 112 | json (1.8.3) 113 | loofah (2.0.3) 114 | nokogiri (>= 1.5.9) 115 | mail (2.6.3) 116 | mime-types (>= 1.16, < 3) 117 | method_source (0.8.2) 118 | mime-types (2.99) 119 | mini_portile (0.6.2) 120 | minitest (5.8.3) 121 | nokogiri (1.6.6.4) 122 | mini_portile (~> 0.6.0) 123 | rack-test (0.6.3) 124 | rack (>= 1.0) 125 | rails-controller-testing (0.0.3) 126 | rails (>= 4.2) 127 | rails-deprecated_sanitizer (1.0.3) 128 | activesupport (>= 4.2.0.alpha) 129 | rails-dom-testing (1.0.7) 130 | activesupport (>= 4.2.0.beta, < 5.0) 131 | nokogiri (~> 1.6.0) 132 | rails-deprecated_sanitizer (>= 1.0.1) 133 | rails-html-sanitizer (1.0.2) 134 | loofah (~> 2.0) 135 | rake (10.4.2) 136 | rspec-core (3.1.7) 137 | rspec-support (~> 3.1.0) 138 | rspec-expectations (3.1.2) 139 | diff-lcs (>= 1.2.0, < 2.0) 140 | rspec-support (~> 3.1.0) 141 | rspec-mocks (3.1.3) 142 | rspec-support (~> 3.1.0) 143 | rspec-rails (3.1.0) 144 | actionpack (>= 3.0) 145 | activesupport (>= 3.0) 146 | railties (>= 3.0) 147 | rspec-core (~> 3.1.0) 148 | rspec-expectations (~> 3.1.0) 149 | rspec-mocks (~> 3.1.0) 150 | rspec-support (~> 3.1.0) 151 | rspec-support (3.1.2) 152 | sass (3.4.19) 153 | sqlite3 (1.3.11) 154 | swagger-docs (0.1.9) 155 | activesupport (>= 3, < 5) 156 | rails (>= 3, < 5) 157 | thor (0.19.1) 158 | thread_safe (0.3.5) 159 | tzinfo (1.2.2) 160 | thread_safe (~> 0.1) 161 | 162 | PLATFORMS 163 | ruby 164 | 165 | DEPENDENCIES 166 | active_model_serializers (~> 0.10.0.rc2) 167 | arel! 168 | byebug 169 | rack! 170 | rails! 171 | rails-controller-testing 172 | rspec-rails 173 | sass-rails! 174 | sprockets! 175 | sprockets-rails! 176 | sqlite3 177 | swagger-docs 178 | tzinfo-data 179 | 180 | BUNDLED WITH 181 | 1.10.6 182 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rails 5 API tutorial 2 | 3 | This demo shows a Rails 5 API with Swagger documentation and Test::Unit and Rspec testing. It uses ActiveModel::Serializers to comply with the JSON+API hypermedia spec. 4 | 5 | 6 | ## Getting started with Rails 5 and creating your own books API 7 | 8 | Install Rails 5: 9 | 10 | git clone https://github.com/rails/rails.git 11 | cd rails/ 12 | bundle install 13 | 14 | Generate API: 15 | 16 | bundle exec railties/exe/rails new ../books --edge --dev --api 17 | 18 | Create Scaffold: 19 | 20 | bundle exec rails generate scaffold books title description:text page_count:integer 21 | 22 | Migrate the database: 23 | 24 | bundle exec rake db:migrate 25 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # Add your own tasks in files placed in lib/tasks ending in .rake, 2 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. 3 | 4 | require File.expand_path('../config/application', __FILE__) 5 | 6 | Rails.application.load_tasks 7 | -------------------------------------------------------------------------------- /app/controllers/application_controller.rb: -------------------------------------------------------------------------------- 1 | class ApplicationController < ActionController::API 2 | Swagger::Docs::Generator::set_real_methods 3 | end 4 | -------------------------------------------------------------------------------- /app/controllers/books_controller.rb: -------------------------------------------------------------------------------- 1 | class BooksController < ApplicationController 2 | before_action :set_book, only: [:show, :update, :destroy] 3 | 4 | swagger_controller :books, "Books" 5 | 6 | swagger_api :index do 7 | summary "Fetches all Book items" 8 | notes "This lists all books" 9 | param :query, :page, :integer, :optional, "Page number" 10 | response :ok, "Success", :Book 11 | response :unauthorized 12 | response :not_acceptable, "The request you made is not acceptable" 13 | response :requested_range_not_satisfiable 14 | end 15 | # GET /books 16 | def index 17 | @books = Book.all 18 | 19 | render json: @books 20 | end 21 | 22 | swagger_api :show do 23 | summary "Fetches a single User item" 24 | param :path, :id, :integer, :optional, "User Id" 25 | response :ok, "Success", :Book 26 | response :unauthorized 27 | response :not_acceptable 28 | response :not_found 29 | end 30 | # GET /books/1 31 | def show 32 | render json: @book 33 | end 34 | 35 | swagger_api :create do 36 | summary "Creates a new Book" 37 | param :form, :title, :string, :required, "Title" 38 | param :form, :description, :string, :required, "Description" 39 | param :form, :page_count, :integer, :required, "Page count" 40 | response :created, "Success", :Book 41 | response :unauthorized 42 | response :not_acceptable 43 | end 44 | # POST /books 45 | def create 46 | @book = Book.new(book_params) 47 | 48 | if @book.save 49 | render json: @book, status: :created, location: @book 50 | else 51 | render json: @book.errors, status: :unprocessable_entity 52 | end 53 | end 54 | 55 | swagger_api :update do 56 | summary "Creates a new Book" 57 | param :path, :id, :integer, :required, "Book Id" 58 | param :form, :title, :string, :required, "Title" 59 | param :form, :description, :string, :required, "Description" 60 | param :form, :page_count, :integer, :required, "Page count" 61 | response :created, "Success", :Book 62 | response :unauthorized 63 | response :not_acceptable 64 | end 65 | # PATCH/PUT /books/1 66 | def update 67 | if @book.update(book_params) 68 | render json: @book 69 | else 70 | render json: @book.errors, status: :unprocessable_entity 71 | end 72 | end 73 | 74 | swagger_api :destroy do 75 | summary "Deletes an existing Book item" 76 | param :path, :id, :integer, :optional, "Book Id" 77 | response :unauthorized 78 | response :not_found 79 | end 80 | # DELETE /books/1 81 | def destroy 82 | @book.destroy 83 | end 84 | 85 | swagger_model :Book do 86 | description "A Tag object." 87 | property :id, :integer, :required, "Book Id" 88 | property :title, :string, :required, "Title" 89 | property :description, :string, :optional, "Description" 90 | property :page_count, :integer, :optional, "Page count" 91 | end 92 | 93 | private 94 | # Use callbacks to share common setup or constraints between actions. 95 | def set_book 96 | @book = Book.find(params[:id]) 97 | end 98 | 99 | # Only allow a trusted parameter "white list" through. 100 | def book_params 101 | params.require(:book).permit(:title, :page_count, :description) 102 | end 103 | end 104 | -------------------------------------------------------------------------------- /app/controllers/concerns/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/switzersc/rails-5-api-tutorial/7eb8ac74f7c8142c1804b7fe04e53c2caac667fe/app/controllers/concerns/.keep -------------------------------------------------------------------------------- /app/jobs/application_job.rb: -------------------------------------------------------------------------------- 1 | class ApplicationJob < ActiveJob::Base 2 | end 3 | -------------------------------------------------------------------------------- /app/mailers/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/switzersc/rails-5-api-tutorial/7eb8ac74f7c8142c1804b7fe04e53c2caac667fe/app/mailers/.keep -------------------------------------------------------------------------------- /app/models/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/switzersc/rails-5-api-tutorial/7eb8ac74f7c8142c1804b7fe04e53c2caac667fe/app/models/.keep -------------------------------------------------------------------------------- /app/models/book.rb: -------------------------------------------------------------------------------- 1 | class Book < ActiveRecord::Base 2 | validates_presence_of :title 3 | end 4 | -------------------------------------------------------------------------------- /app/models/concerns/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/switzersc/rails-5-api-tutorial/7eb8ac74f7c8142c1804b7fe04e53c2caac667fe/app/models/concerns/.keep -------------------------------------------------------------------------------- /app/serializers/book_serializer.rb: -------------------------------------------------------------------------------- 1 | class BookSerializer < ActiveModel::Serializer 2 | attributes :id, :title, :page_count, :description, :created_at 3 | end 4 | -------------------------------------------------------------------------------- /bin/bundle: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) 3 | load Gem.bin_path('bundler', 'bundle') 4 | -------------------------------------------------------------------------------- /bin/rails: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | APP_PATH = File.expand_path('../../config/application', __FILE__) 3 | require_relative '../config/boot' 4 | require 'rails/commands' 5 | -------------------------------------------------------------------------------- /bin/rake: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require_relative '../config/boot' 3 | require 'rake' 4 | Rake.application.run 5 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require 'pathname' 3 | require 'fileutils' 4 | include FileUtils 5 | 6 | # path to your application root. 7 | APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) 8 | 9 | def system!(*args) 10 | system(*args) || abort("\n== Command #{args} failed ==") 11 | end 12 | 13 | chdir APP_ROOT do 14 | # This script is a starting point to setup your application. 15 | # Add necessary setup steps to this file. 16 | 17 | puts '== Installing dependencies ==' 18 | system! 'gem install bundler --conservative' 19 | system('bundle check') or system!('bundle install') 20 | 21 | # puts "\n== Copying sample files ==" 22 | # unless File.exist?('config/database.yml') 23 | # cp 'config/database.yml.sample', 'config/database.yml' 24 | # end 25 | 26 | puts "\n== Preparing database ==" 27 | system! 'ruby bin/rake db:setup' 28 | 29 | puts "\n== Removing old logs and tempfiles ==" 30 | system! 'ruby bin/rake log:clear tmp:clear' 31 | 32 | puts "\n== Restarting application server ==" 33 | system! 'ruby bin/rake restart' 34 | end 35 | -------------------------------------------------------------------------------- /bin/update: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require 'pathname' 3 | require 'fileutils' 4 | include FileUtils 5 | 6 | # path to your application root. 7 | APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) 8 | 9 | def system!(*args) 10 | system(*args) || abort("\n== Command #{args} failed ==") 11 | end 12 | 13 | chdir APP_ROOT do 14 | # This script is a way to update your development environment automatically. 15 | # Add necessary update steps to this file. 16 | 17 | puts '== Installing dependencies ==' 18 | system! 'gem install bundler --conservative' 19 | system 'bundle check' or system! 'bundle install' 20 | 21 | puts "\n== Updating database ==" 22 | system! 'bin/rake db:migrate' 23 | 24 | puts "\n== Removing old logs and tempfiles ==" 25 | system! 'bin/rake log:clear tmp:clear' 26 | 27 | puts "\n== Restarting application server ==" 28 | system! 'bin/rake restart' 29 | end 30 | -------------------------------------------------------------------------------- /config.ru: -------------------------------------------------------------------------------- 1 | # This file is used by Rack-based servers to start the application. 2 | 3 | require ::File.expand_path('../config/environment', __FILE__) 4 | run Rails.application 5 | -------------------------------------------------------------------------------- /config/application.rb: -------------------------------------------------------------------------------- 1 | require File.expand_path('../boot', __FILE__) 2 | 3 | require "rails" 4 | # Pick the frameworks you want: 5 | require "active_model/railtie" 6 | require "active_job/railtie" 7 | require "active_record/railtie" 8 | require "action_controller/railtie" 9 | require "action_mailer/railtie" 10 | require "action_view/railtie" 11 | # require "sprockets/railtie" 12 | require "rails/test_unit/railtie" 13 | 14 | # Require the gems listed in Gemfile, including any gems 15 | # you've limited to :test, :development, or :production. 16 | Bundler.require(*Rails.groups) 17 | 18 | module BooksApp 19 | class Application < Rails::Application 20 | # Settings in config/environments/* take precedence over those specified here. 21 | # Application configuration should go into files in config/initializers 22 | # -- all .rb files in that directory are automatically loaded. 23 | 24 | # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. 25 | # Run "rake time:zones:all" for a time zone names list. Default is UTC. 26 | # config.time_zone = 'Central Time (US & Canada)' 27 | 28 | # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. 29 | # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] 30 | # config.i18n.default_locale = :de 31 | 32 | # Only loads a smaller set of middleware suitable for API only apps. 33 | # Middleware like session, flash, cookies can be added back manually. 34 | # Skip views, helpers and assets when generating a new resource. 35 | config.api_only = true 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /config/boot.rb: -------------------------------------------------------------------------------- 1 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) 2 | 3 | require 'bundler/setup' # Set up gems listed in the Gemfile. 4 | -------------------------------------------------------------------------------- /config/database.yml: -------------------------------------------------------------------------------- 1 | # SQLite version 3.x 2 | # gem install sqlite3 3 | # 4 | # Ensure the SQLite 3 gem is defined in your Gemfile 5 | # gem 'sqlite3' 6 | # 7 | default: &default 8 | adapter: sqlite3 9 | pool: 5 10 | timeout: 5000 11 | 12 | development: 13 | <<: *default 14 | database: db/development.sqlite3 15 | 16 | # Warning: The database defined as "test" will be erased and 17 | # re-generated from your development database when you run "rake". 18 | # Do not set this db to the same as development or production. 19 | test: 20 | <<: *default 21 | database: db/test.sqlite3 22 | 23 | production: 24 | <<: *default 25 | database: db/production.sqlite3 26 | -------------------------------------------------------------------------------- /config/environment.rb: -------------------------------------------------------------------------------- 1 | # Load the Rails application. 2 | require File.expand_path('../application', __FILE__) 3 | 4 | # Initialize the Rails application. 5 | Rails.application.initialize! 6 | -------------------------------------------------------------------------------- /config/environments/development.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb. 3 | 4 | # In the development environment your application's code is reloaded on 5 | # every request. This slows down response time but is perfect for development 6 | # since you don't have to restart the web server when you make code changes. 7 | config.cache_classes = false 8 | 9 | # Do not eager load code on boot. 10 | config.eager_load = false 11 | 12 | # Show full error reports. 13 | config.consider_all_requests_local = true 14 | 15 | # Enable/disable caching. By default caching is disabled. 16 | if Rails.root.join('tmp/caching-dev.txt').exist? 17 | config.action_controller.perform_caching = true 18 | config.cache_store = :memory_store 19 | config.public_file_server.headers = { 20 | 'Cache-Control' => 'public, max-age=172800' 21 | } 22 | else 23 | config.action_controller.perform_caching = false 24 | config.cache_store = :null_store 25 | end 26 | 27 | 28 | # Don't care if the mailer can't send. 29 | config.action_mailer.raise_delivery_errors = false 30 | 31 | # Print deprecation notices to the Rails logger. 32 | config.active_support.deprecation = :log 33 | 34 | # Raise an error on page load if there are pending migrations. 35 | config.active_record.migration_error = :page_load 36 | 37 | 38 | # Raises error for missing translations 39 | # config.action_view.raise_on_missing_translations = true 40 | end 41 | -------------------------------------------------------------------------------- /config/environments/production.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb. 3 | 4 | # Code is not reloaded between requests. 5 | config.cache_classes = true 6 | 7 | # Eager load code on boot. This eager loads most of Rails and 8 | # your application in memory, allowing both threaded web servers 9 | # and those relying on copy on write to perform better. 10 | # Rake tasks automatically ignore this option for performance. 11 | config.eager_load = true 12 | 13 | # Full error reports are disabled and caching is turned on. 14 | config.consider_all_requests_local = false 15 | config.action_controller.perform_caching = true 16 | 17 | # Disable serving static files from the `/public` folder by default since 18 | # Apache or NGINX already handles this. 19 | config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? 20 | 21 | 22 | # Enable serving of images, stylesheets, and JavaScripts from an asset server. 23 | # config.action_controller.asset_host = 'http://assets.example.com' 24 | 25 | # Specifies the header that your server uses for sending files. 26 | # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache 27 | # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX 28 | 29 | # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 30 | # config.force_ssl = true 31 | 32 | # Use the lowest log level to ensure availability of diagnostic information 33 | # when problems arise. 34 | config.log_level = :debug 35 | 36 | # Prepend all log lines with the following tags. 37 | # config.log_tags = [ :subdomain, :request_id ] 38 | 39 | # Use a different logger for distributed setups. 40 | # require 'syslog/logger' 41 | # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') 42 | 43 | # Use a different cache store in production. 44 | # config.cache_store = :mem_cache_store 45 | 46 | # Use a real queuing backend for Active Job (and separate queues per environment) 47 | # config.active_job.queue_adapter = :resque 48 | # config.active_job.queue_name_prefix = "books_app_#{Rails.env}" 49 | 50 | # Ignore bad email addresses and do not raise email delivery errors. 51 | # Set this to true and configure the email server for immediate delivery to raise delivery errors. 52 | # config.action_mailer.raise_delivery_errors = false 53 | 54 | # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 55 | # the I18n.default_locale when a translation cannot be found). 56 | config.i18n.fallbacks = true 57 | 58 | # Send deprecation notices to registered listeners. 59 | config.active_support.deprecation = :notify 60 | 61 | # Use default logging formatter so that PID and timestamp are not suppressed. 62 | config.log_formatter = ::Logger::Formatter.new 63 | 64 | # Do not dump schema after migrations. 65 | config.active_record.dump_schema_after_migration = false 66 | end 67 | -------------------------------------------------------------------------------- /config/environments/test.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb. 3 | 4 | # The test environment is used exclusively to run your application's 5 | # test suite. You never need to work with it otherwise. Remember that 6 | # your test database is "scratch space" for the test suite and is wiped 7 | # and recreated between test runs. Don't rely on the data there! 8 | config.cache_classes = true 9 | 10 | # Do not eager load code on boot. This avoids loading your whole application 11 | # just for the purpose of running a single test. If you are using a tool that 12 | # preloads Rails for running tests, you may have to set it to true. 13 | config.eager_load = false 14 | 15 | # Configure public file server for tests with Cache-Control for performance. 16 | config.public_file_server.enabled = true 17 | config.public_file_server.headers = { 18 | 'Cache-Control' => 'public, max-age=3600' 19 | } 20 | 21 | # Show full error reports and disable caching. 22 | config.consider_all_requests_local = true 23 | config.action_controller.perform_caching = false 24 | 25 | # Raise exceptions instead of rendering exception templates. 26 | config.action_dispatch.show_exceptions = false 27 | 28 | # Disable request forgery protection in test environment. 29 | config.action_controller.allow_forgery_protection = false 30 | 31 | # Tell Action Mailer not to deliver emails to the real world. 32 | # The :test delivery method accumulates sent emails in the 33 | # ActionMailer::Base.deliveries array. 34 | config.action_mailer.delivery_method = :test 35 | 36 | # Randomize the order test cases are executed. 37 | config.active_support.test_order = :random 38 | 39 | # Print deprecation notices to the stderr. 40 | config.active_support.deprecation = :stderr 41 | 42 | # Raises error for missing translations 43 | # config.action_view.raise_on_missing_translations = true 44 | end 45 | -------------------------------------------------------------------------------- /config/initializers/active_model_serializer.rb: -------------------------------------------------------------------------------- 1 | ActiveModel::Serializer.config.adapter = ActiveModel::Serializer::Adapter::JsonApi -------------------------------------------------------------------------------- /config/initializers/active_record_belongs_to_required_by_default.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Require `belongs_to` associations by default. 4 | Rails.application.config.active_record.belongs_to_required_by_default = true 5 | -------------------------------------------------------------------------------- /config/initializers/application_controller_renderer.rb: -------------------------------------------------------------------------------- 1 | ## Change renderer defaults here. 2 | # 3 | # ApplicationController.renderer.defaults.merge!( 4 | # http_host: 'example.org', 5 | # https: false 6 | # ) 7 | -------------------------------------------------------------------------------- /config/initializers/backtrace_silencers.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. 4 | # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ } 5 | 6 | # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code. 7 | # Rails.backtrace_cleaner.remove_silencers! 8 | -------------------------------------------------------------------------------- /config/initializers/callback_terminator.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Do not halt callback chains when a callback returns false. 4 | ActiveSupport.halt_callback_chains_on_return_false = false 5 | -------------------------------------------------------------------------------- /config/initializers/cors.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Avoid CORS issues when API is called from the frontend app. 4 | # Handle Cross-Origin Resource Sharing (CORS) in order to accept cross-origin AJAX requests. 5 | 6 | # Read more: https://github.com/cyu/rack-cors 7 | 8 | # Rails.application.config.middleware.insert_before 0, Rack::Cors do 9 | # allow do 10 | # origins 'example.com' 11 | # 12 | # resource '*', 13 | # headers: :any, 14 | # methods: [:get, :post, :put, :patch, :delete, :options, :head] 15 | # end 16 | # end 17 | -------------------------------------------------------------------------------- /config/initializers/filter_parameter_logging.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Configure sensitive parameters which will be filtered from the log file. 4 | Rails.application.config.filter_parameters += [:password] 5 | -------------------------------------------------------------------------------- /config/initializers/inflections.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new inflection rules using the following format. Inflections 4 | # are locale specific, and you may define rules for as many different 5 | # locales as you wish. All of these examples are active by default: 6 | # ActiveSupport::Inflector.inflections(:en) do |inflect| 7 | # inflect.plural /^(ox)$/i, '\1en' 8 | # inflect.singular /^(ox)en/i, '\1' 9 | # inflect.irregular 'person', 'people' 10 | # inflect.uncountable %w( fish sheep ) 11 | # end 12 | 13 | # These inflection rules are supported but not enabled by default: 14 | # ActiveSupport::Inflector.inflections(:en) do |inflect| 15 | # inflect.acronym 'RESTful' 16 | # end 17 | -------------------------------------------------------------------------------- /config/initializers/mime_types.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new mime types for use in respond_to blocks: 4 | # Mime::Type.register "text/richtext", :rtf 5 | -------------------------------------------------------------------------------- /config/initializers/request_forgery_protection.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Enable origin-checking CSRF mitigation. 4 | Rails.application.config.action_controller.forgery_protection_origin_check = true 5 | -------------------------------------------------------------------------------- /config/initializers/swagger.rb: -------------------------------------------------------------------------------- 1 | class Swagger::Docs::Config 2 | def self.base_api_controller 3 | ActionController::API 4 | end 5 | end 6 | Swagger::Docs::Config.register_apis({ 7 | "1.0" => { 8 | :controller_base_path => "", 9 | # the extension used for the API 10 | :api_extension_type => :json, 11 | # the output location where your .json files are written to 12 | :api_file_path => "public", 13 | # the URL base path to your API 14 | :base_path => "http://api.booksapp.com", 15 | # if you want to delete all .json files at each generation 16 | :clean_directory => false, 17 | # add custom attributes to api-docs 18 | :attributes => { 19 | :info => { 20 | "title" => "Bookstore App", 21 | "description" => "This is an API for an online bookstore", 22 | "termsOfServiceUrl" => "https://github.com/switzersc/rails-5-api-tutorial", 23 | "contact" => "shelbyswitzer@gmail.com", 24 | "license" => "Apache 2.0", 25 | "licenseUrl" => "http://www.apache.org/licenses/LICENSE-2.0.html" 26 | } 27 | } 28 | } 29 | }) -------------------------------------------------------------------------------- /config/initializers/wrap_parameters.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # This file contains settings for ActionController::ParamsWrapper which 4 | # is enabled by default. 5 | 6 | # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array. 7 | ActiveSupport.on_load(:action_controller) do 8 | wrap_parameters format: [:json] 9 | end 10 | 11 | # To enable root element in JSON for ActiveRecord objects. 12 | # ActiveSupport.on_load(:active_record) do 13 | # self.include_root_in_json = true 14 | # end 15 | -------------------------------------------------------------------------------- /config/locales/en.yml: -------------------------------------------------------------------------------- 1 | # Files in the config/locales directory are used for internationalization 2 | # and are automatically loaded by Rails. If you want to use locales other 3 | # than English, add the necessary files in this directory. 4 | # 5 | # To use the locales, use `I18n.t`: 6 | # 7 | # I18n.t 'hello' 8 | # 9 | # In views, this is aliased to just `t`: 10 | # 11 | # <%= t('hello') %> 12 | # 13 | # To use a different locale, set it with `I18n.locale`: 14 | # 15 | # I18n.locale = :es 16 | # 17 | # This would use the information in config/locales/es.yml. 18 | # 19 | # To learn more, please read the Rails Internationalization guide 20 | # available at http://guides.rubyonrails.org/i18n.html. 21 | 22 | en: 23 | hello: "Hello world" 24 | -------------------------------------------------------------------------------- /config/routes.rb: -------------------------------------------------------------------------------- 1 | Rails.application.routes.draw do 2 | resources :books 3 | # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 4 | end 5 | -------------------------------------------------------------------------------- /config/secrets.yml: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Your secret key is used for verifying the integrity of signed cookies. 4 | # If you change this key, all old signed cookies will become invalid! 5 | 6 | # Make sure the secret is at least 30 characters and all random, 7 | # no regular words or you'll be exposed to dictionary attacks. 8 | # You can use `rake secret` to generate a secure secret key. 9 | 10 | # Make sure the secrets in this file are kept private 11 | # if you're sharing your code publicly. 12 | 13 | development: 14 | secret_key_base: 7a02c2bb6dc5185a72def57e1b00edde409a4aee352314cfb29c6bc908c236b3923819e0e3d18023c47625e420bffec7c172eac843ca3d48bb55d09bbb79dfaa 15 | 16 | test: 17 | secret_key_base: 3ddf42c90754b3d43a5fcecb3f55e38898119ccbe0ba444b88cdc9d3f884ffdb95f172127a63daea4658477d1fe798afd1eea3b1b290dead2c21f282fbc2b820 18 | 19 | # Do not keep production secrets in the repository, 20 | # instead read values from the environment. 21 | production: 22 | secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> 23 | -------------------------------------------------------------------------------- /db/migrate/20151127182546_create_books.rb: -------------------------------------------------------------------------------- 1 | class CreateBooks < ActiveRecord::Migration 2 | def change 3 | create_table :books do |t| 4 | t.string :title 5 | t.integer :page_count 6 | t.text :description 7 | 8 | t.timestamps 9 | end 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /db/schema.rb: -------------------------------------------------------------------------------- 1 | # encoding: UTF-8 2 | # This file is auto-generated from the current state of the database. Instead 3 | # of editing this file, please use the migrations feature of Active Record to 4 | # incrementally modify your database, and then regenerate this schema definition. 5 | # 6 | # Note that this schema.rb definition is the authoritative source for your 7 | # database schema. If you need to create the application database on another 8 | # system, you should be using db:schema:load, not running all the migrations 9 | # from scratch. The latter is a flawed and unsustainable approach (the more migrations 10 | # you'll amass, the slower it'll run and the greater likelihood for issues). 11 | # 12 | # It's strongly recommended that you check this file into your version control system. 13 | 14 | ActiveRecord::Schema.define(version: 20151127182546) do 15 | 16 | create_table "books", force: :cascade do |t| 17 | t.string "title" 18 | t.integer "page_count" 19 | t.text "description" 20 | t.datetime "created_at", null: false 21 | t.datetime "updated_at", null: false 22 | end 23 | 24 | end 25 | -------------------------------------------------------------------------------- /db/seeds.rb: -------------------------------------------------------------------------------- 1 | # This file should contain all the record creation needed to seed the database with its default values. 2 | # The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). 3 | # 4 | # Examples: 5 | # 6 | # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) 7 | # Mayor.create(name: 'Emanuel', city: cities.first) 8 | -------------------------------------------------------------------------------- /lib/tasks/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/switzersc/rails-5-api-tutorial/7eb8ac74f7c8142c1804b7fe04e53c2caac667fe/lib/tasks/.keep -------------------------------------------------------------------------------- /public/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The page you were looking for doesn't exist (404) 5 | 6 | 55 | 56 | 57 | 58 | 59 |
60 |
61 |

The page you were looking for doesn't exist.

62 |

You may have mistyped the address or the page may have moved.

63 |
64 |

If you are the application owner check the logs for more information.

65 |
66 | 67 | 68 | -------------------------------------------------------------------------------- /public/422.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The change you wanted was rejected (422) 5 | 6 | 55 | 56 | 57 | 58 | 59 |
60 |
61 |

The change you wanted was rejected.

62 |

Maybe you tried to change something you didn't have access to.

63 |
64 |

If you are the application owner check the logs for more information.

65 |
66 | 67 | 68 | -------------------------------------------------------------------------------- /public/500.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | We're sorry, but something went wrong (500) 5 | 6 | 55 | 56 | 57 | 58 | 59 |
60 |
61 |

We're sorry, but something went wrong.

62 |
63 |

If you are the application owner check the logs for more information.

64 |
65 | 66 | 67 | -------------------------------------------------------------------------------- /public/api-docs.json: -------------------------------------------------------------------------------- 1 | { 2 | "apiVersion": "1.0", 3 | "swaggerVersion": "1.2", 4 | "basePath": "http://api.booksapp.com/", 5 | "apis": [ 6 | { 7 | "path": "books.{format}", 8 | "description": "Books" 9 | } 10 | ], 11 | "info": { 12 | "title": "Bookstore App", 13 | "description": "This is an API for an online bookstore", 14 | "termsOfServiceUrl": "https://github.com/switzersc/rails-5-api-tutorial", 15 | "contact": "shelbyswitzer@gmail.com", 16 | "license": "Apache 2.0", 17 | "licenseUrl": "http://www.apache.org/licenses/LICENSE-2.0.html" 18 | } 19 | } -------------------------------------------------------------------------------- /public/books.json: -------------------------------------------------------------------------------- 1 | { 2 | "apiVersion": "1.0", 3 | "swaggerVersion": "1.2", 4 | "basePath": "http://api.booksapp.com/", 5 | "resourcePath": "books", 6 | "apis": [ 7 | { 8 | "path": "books.json", 9 | "operations": [ 10 | { 11 | "summary": "Fetches all Book items", 12 | "notes": "This lists all books", 13 | "parameters": [ 14 | { 15 | "paramType": "query", 16 | "name": "page", 17 | "type": "integer", 18 | "description": "Page number", 19 | "required": false 20 | } 21 | ], 22 | "responseMessages": [ 23 | { 24 | "code": 200, 25 | "responseModel": "Book", 26 | "message": "Success" 27 | }, 28 | { 29 | "code": 401, 30 | "responseModel": null, 31 | "message": "Unauthorized" 32 | }, 33 | { 34 | "code": 406, 35 | "responseModel": null, 36 | "message": "The request you made is not acceptable" 37 | }, 38 | { 39 | "code": 500, 40 | "responseModel": null, 41 | "message": "Requested Range Not Satisfiable" 42 | } 43 | ], 44 | "method": "get", 45 | "nickname": "Books#index" 46 | } 47 | ] 48 | }, 49 | { 50 | "path": "books.json", 51 | "operations": [ 52 | { 53 | "summary": "Creates a new Book", 54 | "parameters": [ 55 | { 56 | "paramType": "form", 57 | "name": "title", 58 | "type": "string", 59 | "description": "Title", 60 | "required": true 61 | }, 62 | { 63 | "paramType": "form", 64 | "name": "description", 65 | "type": "string", 66 | "description": "Description", 67 | "required": true 68 | }, 69 | { 70 | "paramType": "form", 71 | "name": "page_count", 72 | "type": "integer", 73 | "description": "Page count", 74 | "required": true 75 | } 76 | ], 77 | "responseMessages": [ 78 | { 79 | "code": 201, 80 | "responseModel": "Book", 81 | "message": "Success" 82 | }, 83 | { 84 | "code": 401, 85 | "responseModel": null, 86 | "message": "Unauthorized" 87 | }, 88 | { 89 | "code": 406, 90 | "responseModel": null, 91 | "message": "Not Acceptable" 92 | } 93 | ], 94 | "method": "post", 95 | "nickname": "Books#create" 96 | } 97 | ] 98 | }, 99 | { 100 | "path": "books/{id}.json", 101 | "operations": [ 102 | { 103 | "summary": "Fetches a single User item", 104 | "parameters": [ 105 | { 106 | "paramType": "path", 107 | "name": "id", 108 | "type": "integer", 109 | "description": "User Id", 110 | "required": false 111 | } 112 | ], 113 | "responseMessages": [ 114 | { 115 | "code": 200, 116 | "responseModel": "Book", 117 | "message": "Success" 118 | }, 119 | { 120 | "code": 401, 121 | "responseModel": null, 122 | "message": "Unauthorized" 123 | }, 124 | { 125 | "code": 404, 126 | "responseModel": null, 127 | "message": "Not Found" 128 | }, 129 | { 130 | "code": 406, 131 | "responseModel": null, 132 | "message": "Not Acceptable" 133 | } 134 | ], 135 | "method": "get", 136 | "nickname": "Books#show" 137 | } 138 | ] 139 | }, 140 | { 141 | "path": "books/{id}.json", 142 | "operations": [ 143 | { 144 | "summary": "Creates a new Book", 145 | "parameters": [ 146 | { 147 | "paramType": "path", 148 | "name": "id", 149 | "type": "integer", 150 | "description": "Book Id", 151 | "required": true 152 | }, 153 | { 154 | "paramType": "form", 155 | "name": "title", 156 | "type": "string", 157 | "description": "Title", 158 | "required": true 159 | }, 160 | { 161 | "paramType": "form", 162 | "name": "description", 163 | "type": "string", 164 | "description": "Description", 165 | "required": true 166 | }, 167 | { 168 | "paramType": "form", 169 | "name": "page_count", 170 | "type": "integer", 171 | "description": "Page count", 172 | "required": true 173 | } 174 | ], 175 | "responseMessages": [ 176 | { 177 | "code": 201, 178 | "responseModel": "Book", 179 | "message": "Success" 180 | }, 181 | { 182 | "code": 401, 183 | "responseModel": null, 184 | "message": "Unauthorized" 185 | }, 186 | { 187 | "code": 406, 188 | "responseModel": null, 189 | "message": "Not Acceptable" 190 | } 191 | ], 192 | "method": "patch", 193 | "nickname": "Books#update" 194 | } 195 | ] 196 | }, 197 | { 198 | "path": "books/{id}.json", 199 | "operations": [ 200 | { 201 | "summary": "Deletes an existing Book item", 202 | "parameters": [ 203 | { 204 | "paramType": "path", 205 | "name": "id", 206 | "type": "integer", 207 | "description": "Book Id", 208 | "required": false 209 | } 210 | ], 211 | "responseMessages": [ 212 | { 213 | "code": 401, 214 | "responseModel": null, 215 | "message": "Unauthorized" 216 | }, 217 | { 218 | "code": 404, 219 | "responseModel": null, 220 | "message": "Not Found" 221 | } 222 | ], 223 | "method": "delete", 224 | "nickname": "Books#destroy" 225 | } 226 | ] 227 | } 228 | ], 229 | "models": { 230 | "Book": { 231 | "id": "Book", 232 | "required": [ 233 | "id", 234 | "title" 235 | ], 236 | "properties": { 237 | "id": { 238 | "type": "integer", 239 | "description": "Book Id" 240 | }, 241 | "title": { 242 | "type": "string", 243 | "description": "Title" 244 | }, 245 | "description": { 246 | "type": "string", 247 | "description": "Description" 248 | }, 249 | "pageCount": { 250 | "type": "integer", 251 | "description": "Page count" 252 | } 253 | }, 254 | "description": "A Tag object." 255 | } 256 | } 257 | } -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/switzersc/rails-5-api-tutorial/7eb8ac74f7c8142c1804b7fe04e53c2caac667fe/public/favicon.ico -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file 2 | # 3 | # To ban all spiders from the entire site uncomment the next two lines: 4 | # User-agent: * 5 | # Disallow: / 6 | -------------------------------------------------------------------------------- /spec/controllers/books_controller_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rails_helper' 2 | 3 | # This spec was generated by rspec-rails when you ran the scaffold generator. 4 | # It demonstrates how one might use RSpec to specify the controller code that 5 | # was generated by Rails when you ran the scaffold generator. 6 | # 7 | # It assumes that the implementation code is generated by the rails scaffold 8 | # generator. If you are using any extension libraries to generate different 9 | # controller code, this generated spec may or may not pass. 10 | # 11 | # It only uses APIs available in rails and/or rspec-rails. There are a number 12 | # of tools you can use to make these specs even more expressive, but we're 13 | # sticking to rails and rspec-rails APIs to keep things simple and stable. 14 | # 15 | # Compared to earlier versions of this generator, there is very limited use of 16 | # stubs and message expectations in this spec. Stubs are only used when there 17 | # is no simpler way to get a handle on the object needed for the example. 18 | # Message expectations are only used when there is no simpler way to specify 19 | # that an instance is receiving a specific message. 20 | 21 | RSpec.describe BooksController, :type => :controller do 22 | 23 | # This should return the minimal set of attributes required to create a valid 24 | # Book. As you add validations to Book, be sure to 25 | # adjust the attributes here as well. 26 | let(:valid_attributes) { 27 | {title: "A Tale of Two Cities"} 28 | } 29 | 30 | let(:invalid_attributes) { 31 | {page_count: "ha"} 32 | } 33 | 34 | # This should return the minimal set of values that should be in the session 35 | # in order to pass any filters (e.g. authentication) defined in 36 | # BooksController. Be sure to keep this updated too. 37 | let(:valid_session) { {} } 38 | 39 | describe "GET index" do 40 | it "assigns all books as @books" do 41 | book = Book.create! valid_attributes 42 | get :index, {}, valid_session 43 | expect(assigns(:books)).to eq([book]) 44 | end 45 | end 46 | 47 | describe "GET show" do 48 | it "assigns the requested book as @book" do 49 | book = Book.create! valid_attributes 50 | get :show, {:id => book.to_param}, valid_session 51 | expect(assigns(:book)).to eq(book) 52 | end 53 | end 54 | 55 | describe "POST create" do 56 | describe "with valid params" do 57 | it "creates a new Book" do 58 | expect { 59 | post :create, {:book => valid_attributes}, valid_session 60 | }.to change(Book, :count).by(1) 61 | end 62 | 63 | it "assigns a newly created book as @book" do 64 | post :create, {:book => valid_attributes}, valid_session 65 | expect(assigns(:book)).to be_a(Book) 66 | expect(assigns(:book)).to be_persisted 67 | end 68 | end 69 | 70 | describe "with invalid params" do 71 | it "assigns a newly created but unsaved book as @book" do 72 | post :create, {:book => invalid_attributes}, valid_session 73 | expect(assigns(:book)).to be_a_new(Book) 74 | end 75 | end 76 | end 77 | 78 | describe "PUT update" do 79 | describe "with valid params" do 80 | let(:new_attributes) { 81 | skip("Add a hash of attributes valid for your model") 82 | } 83 | 84 | it "updates the requested book" do 85 | book = Book.create! valid_attributes 86 | put :update, {:id => book.to_param, :book => new_attributes}, valid_session 87 | book.reload 88 | skip("Add assertions for updated state") 89 | end 90 | 91 | it "assigns the requested book as @book" do 92 | book = Book.create! valid_attributes 93 | put :update, {:id => book.to_param, :book => valid_attributes}, valid_session 94 | expect(assigns(:book)).to eq(book) 95 | end 96 | end 97 | 98 | describe "with invalid params" do 99 | it "assigns the book as @book" do 100 | book = Book.create! valid_attributes 101 | put :update, {:id => book.to_param, :book => invalid_attributes}, valid_session 102 | expect(assigns(:book)).to eq(book) 103 | end 104 | end 105 | end 106 | 107 | describe "DELETE destroy" do 108 | it "destroys the requested book" do 109 | book = Book.create! valid_attributes 110 | expect { 111 | delete :destroy, {:id => book.to_param}, valid_session 112 | }.to change(Book, :count).by(-1) 113 | end 114 | end 115 | 116 | end 117 | -------------------------------------------------------------------------------- /spec/models/book_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rails_helper' 2 | 3 | RSpec.describe Book, :type => :model do 4 | pending "add some examples to (or delete) #{__FILE__}" 5 | end 6 | -------------------------------------------------------------------------------- /spec/rails_helper.rb: -------------------------------------------------------------------------------- 1 | # This file is copied to spec/ when you run 'rails generate rspec:install' 2 | ENV["RAILS_ENV"] ||= 'test' 3 | require 'spec_helper' 4 | require File.expand_path("../../config/environment", __FILE__) 5 | require 'rspec/rails' 6 | # Add additional requires below this line. Rails is not loaded until this point! 7 | 8 | # Requires supporting ruby files with custom matchers and macros, etc, in 9 | # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are 10 | # run as spec files by default. This means that files in spec/support that end 11 | # in _spec.rb will both be required and run as specs, causing the specs to be 12 | # run twice. It is recommended that you do not name files matching this glob to 13 | # end with _spec.rb. You can configure this pattern with the --pattern 14 | # option on the command line or in ~/.rspec, .rspec or `.rspec-local`. 15 | # 16 | # The following line is provided for convenience purposes. It has the downside 17 | # of increasing the boot-up time by auto-requiring all files in the support 18 | # directory. Alternatively, in the individual `*_spec.rb` files, manually 19 | # require only the support files necessary. 20 | # 21 | # Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } 22 | 23 | # Checks for pending migrations before tests are run. 24 | # If you are not using ActiveRecord, you can remove this line. 25 | ActiveRecord::Migration.maintain_test_schema! 26 | 27 | RSpec.configure do |config| 28 | # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 29 | config.fixture_path = "#{::Rails.root}/spec/fixtures" 30 | 31 | # If you're not using ActiveRecord, or you'd prefer not to run each of your 32 | # examples within a transaction, remove the following line or assign false 33 | # instead of true. 34 | config.use_transactional_fixtures = true 35 | 36 | # RSpec Rails can automatically mix in different behaviours to your tests 37 | # based on their file location, for example enabling you to call `get` and 38 | # `post` in specs under `spec/controllers`. 39 | # 40 | # You can disable this behaviour by removing the line below, and instead 41 | # explicitly tag your specs with their type, e.g.: 42 | # 43 | # RSpec.describe UsersController, :type => :controller do 44 | # # ... 45 | # end 46 | # 47 | # The different available types are documented in the features, such as in 48 | # https://relishapp.com/rspec/rspec-rails/docs 49 | config.infer_spec_type_from_file_location! 50 | 51 | config.include Rails::Controller::Testing::TestProcess 52 | config.include Rails::Controller::Testing::TemplateAssertions 53 | config.include Rails::Controller::Testing::Integration 54 | end 55 | -------------------------------------------------------------------------------- /spec/requests/books_spec.rb: -------------------------------------------------------------------------------- 1 | require 'rails_helper' 2 | 3 | RSpec.describe "Books", :type => :request do 4 | describe "GET /books" do 5 | it "works! (now write some real specs)" do 6 | get books_path 7 | expect(response).to have_http_status(200) 8 | end 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /spec/routing/books_routing_spec.rb: -------------------------------------------------------------------------------- 1 | require "rails_helper" 2 | 3 | RSpec.describe BooksController, :type => :routing do 4 | describe "routing" do 5 | 6 | it "routes to #index" do 7 | expect(:get => "/books").to route_to("books#index") 8 | end 9 | 10 | it "routes to #show" do 11 | expect(:get => "/books/1").to route_to("books#show", :id => "1") 12 | end 13 | 14 | it "routes to #create" do 15 | expect(:post => "/books").to route_to("books#create") 16 | end 17 | 18 | it "routes to #update" do 19 | expect(:put => "/books/1").to route_to("books#update", :id => "1") 20 | end 21 | 22 | it "routes to #destroy" do 23 | expect(:delete => "/books/1").to route_to("books#destroy", :id => "1") 24 | end 25 | 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # This file was generated by the `rails generate rspec:install` command. Conventionally, all 2 | # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. 3 | # The generated `.rspec` file contains `--require spec_helper` which will cause this 4 | # file to always be loaded, without a need to explicitly require it in any files. 5 | # 6 | # Given that it is always loaded, you are encouraged to keep this file as 7 | # light-weight as possible. Requiring heavyweight dependencies from this file 8 | # will add to the boot time of your test suite on EVERY test run, even for an 9 | # individual file that may not need all of that loaded. Instead, consider making 10 | # a separate helper file that requires the additional dependencies and performs 11 | # the additional setup, and require it from the spec files that actually need it. 12 | # 13 | # The `.rspec` file also contains a few flags that are not defaults but that 14 | # users commonly want. 15 | # 16 | # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration 17 | RSpec.configure do |config| 18 | # rspec-expectations config goes here. You can use an alternate 19 | # assertion/expectation library such as wrong or the stdlib/minitest 20 | # assertions if you prefer. 21 | config.expect_with :rspec do |expectations| 22 | # This option will default to `true` in RSpec 4. It makes the `description` 23 | # and `failure_message` of custom matchers include text for helper methods 24 | # defined using `chain`, e.g.: 25 | # be_bigger_than(2).and_smaller_than(4).description 26 | # # => "be bigger than 2 and smaller than 4" 27 | # ...rather than: 28 | # # => "be bigger than 2" 29 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true 30 | end 31 | 32 | # rspec-mocks config goes here. You can use an alternate test double 33 | # library (such as bogus or mocha) by changing the `mock_with` option here. 34 | config.mock_with :rspec do |mocks| 35 | # Prevents you from mocking or stubbing a method that does not exist on 36 | # a real object. This is generally recommended, and will default to 37 | # `true` in RSpec 4. 38 | mocks.verify_partial_doubles = true 39 | end 40 | 41 | # The settings below are suggested to provide a good initial experience 42 | # with RSpec, but feel free to customize to your heart's content. 43 | =begin 44 | # These two settings work together to allow you to limit a spec run 45 | # to individual examples or groups you care about by tagging them with 46 | # `:focus` metadata. When nothing is tagged with `:focus`, all examples 47 | # get run. 48 | config.filter_run :focus 49 | config.run_all_when_everything_filtered = true 50 | 51 | # Limits the available syntax to the non-monkey patched syntax that is recommended. 52 | # For more details, see: 53 | # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax 54 | # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ 55 | # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching 56 | config.disable_monkey_patching! 57 | 58 | # Many RSpec users commonly either run the entire suite or an individual 59 | # file, and it's useful to allow more verbose output when running an 60 | # individual spec file. 61 | if config.files_to_run.one? 62 | # Use the documentation formatter for detailed output, 63 | # unless a formatter has already been configured 64 | # (e.g. via a command-line flag). 65 | config.default_formatter = 'doc' 66 | end 67 | 68 | # Print the 10 slowest examples and example groups at the 69 | # end of the spec run, to help surface which specs are running 70 | # particularly slow. 71 | config.profile_examples = 10 72 | 73 | # Run specs in random order to surface order dependencies. If you find an 74 | # order dependency and want to debug it, you can fix the order by providing 75 | # the seed, which is printed after each run. 76 | # --seed 1234 77 | config.order = :random 78 | 79 | # Seed global randomization in this process using the `--seed` CLI option. 80 | # Setting this allows you to use `--seed` to deterministically reproduce 81 | # test failures related to randomization by passing the same `--seed` value 82 | # as the one that triggered the failure. 83 | Kernel.srand config.seed 84 | =end 85 | end 86 | -------------------------------------------------------------------------------- /test/controllers/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/switzersc/rails-5-api-tutorial/7eb8ac74f7c8142c1804b7fe04e53c2caac667fe/test/controllers/.keep -------------------------------------------------------------------------------- /test/controllers/books_controller_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class BooksControllerTest < ActionController::TestCase 4 | setup do 5 | @book = books(:one) 6 | end 7 | 8 | test "should get index" do 9 | get :index 10 | assert_response :success 11 | end 12 | 13 | test "should create book" do 14 | assert_difference('Book.count') do 15 | post :create, params: { book: { description: @book.description, page_count: @book.page_count, title: @book.title } } 16 | end 17 | 18 | assert_response 201 19 | end 20 | 21 | test "should show book" do 22 | get :show, params: { id: @book } 23 | assert_response :success 24 | end 25 | 26 | test "should update book" do 27 | patch :update, params: { id: @book, book: { description: @book.description, page_count: @book.page_count, title: @book.title } } 28 | assert_response 200 29 | end 30 | 31 | test "should destroy book" do 32 | assert_difference('Book.count', -1) do 33 | delete :destroy, params: { id: @book } 34 | end 35 | 36 | assert_response 204 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /test/fixtures/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/switzersc/rails-5-api-tutorial/7eb8ac74f7c8142c1804b7fe04e53c2caac667fe/test/fixtures/.keep -------------------------------------------------------------------------------- /test/fixtures/books.yml: -------------------------------------------------------------------------------- 1 | # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html 2 | 3 | one: 4 | title: MyString 5 | page_count: 1 6 | description: MyText 7 | 8 | two: 9 | title: MyString 10 | page_count: 1 11 | description: MyText 12 | -------------------------------------------------------------------------------- /test/fixtures/files/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/switzersc/rails-5-api-tutorial/7eb8ac74f7c8142c1804b7fe04e53c2caac667fe/test/fixtures/files/.keep -------------------------------------------------------------------------------- /test/integration/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/switzersc/rails-5-api-tutorial/7eb8ac74f7c8142c1804b7fe04e53c2caac667fe/test/integration/.keep -------------------------------------------------------------------------------- /test/mailers/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/switzersc/rails-5-api-tutorial/7eb8ac74f7c8142c1804b7fe04e53c2caac667fe/test/mailers/.keep -------------------------------------------------------------------------------- /test/models/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/switzersc/rails-5-api-tutorial/7eb8ac74f7c8142c1804b7fe04e53c2caac667fe/test/models/.keep -------------------------------------------------------------------------------- /test/models/book_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class BookTest < ActiveSupport::TestCase 4 | # test "the truth" do 5 | # assert true 6 | # end 7 | end 8 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | ENV['RAILS_ENV'] ||= 'test' 2 | require File.expand_path('../../config/environment', __FILE__) 3 | require 'rails/test_help' 4 | 5 | class ActiveSupport::TestCase 6 | # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. 7 | fixtures :all 8 | 9 | # Add more helper methods to be used by all tests here... 10 | end 11 | --------------------------------------------------------------------------------