├── .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 |You may have mistyped the address or the page may have moved.
63 |If you are the application owner check the logs for more information.
65 |Maybe you tried to change something you didn't have access to.
63 |If you are the application owner check the logs for more information.
65 |If you are the application owner check the logs for more information.
64 |