├── .codeclimate.yml ├── .gitignore ├── .rspec ├── .rubocop.yml ├── .travis.yml ├── Gemfile ├── Gemfile.lock ├── README.md ├── Rakefile ├── app ├── channels │ └── application_cable │ │ ├── channel.rb │ │ └── connection.rb ├── controllers │ ├── api │ │ └── v1 │ │ │ ├── api_controller.rb │ │ │ ├── concerns │ │ │ ├── authenticator.rb │ │ │ ├── error_handler.rb │ │ │ ├── internationalizator.rb │ │ │ └── version_expiration_handler.rb │ │ │ ├── notes_controller.rb │ │ │ ├── sessions_controller.rb │ │ │ ├── users_controller.rb │ │ │ └── versions_controller.rb │ ├── application_controller.rb │ ├── concerns │ │ └── .keep │ ├── pages_controller.rb │ ├── users_controller.rb │ └── view_controller.rb ├── jobs │ └── application_job.rb ├── mailers │ ├── application_mailer.rb │ └── user_mailer.rb ├── models │ ├── application_record.rb │ ├── concerns │ │ ├── authenticatable.rb │ │ ├── confirmable.rb │ │ └── recoverable.rb │ ├── note.rb │ └── user.rb ├── serializers │ └── api │ │ └── v1 │ │ ├── note_serializer.rb │ │ └── user_serializer.rb └── views │ ├── pages │ ├── privacy.html.erb │ └── terms.html.erb │ ├── user_mailer │ ├── ask_email_confirmation.html.erb │ ├── ask_email_confirmation.text.erb │ ├── ask_reset_password.html.erb │ └── ask_reset_password.text.erb │ └── users │ ├── confirmed.html.erb │ ├── invalid_confirmation.html.erb │ ├── invalid_reset_confirmation.html.erb │ └── reset_confirmed.html.erb ├── bin ├── bundle ├── create_psql_user ├── rails ├── rake ├── remove_notes ├── rename_project ├── reset_git ├── setup ├── spring └── update ├── config.ru ├── config ├── application.rb ├── boot.rb ├── cable.yml ├── database.yml ├── environment.rb ├── environments │ ├── development.rb │ ├── production.rb │ └── test.rb ├── initializers │ ├── 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 │ └── wrap_parameters.rb ├── locales │ ├── en.yml │ └── es.yml ├── puma.rb ├── redis │ └── cable.yml ├── routes.rb └── secrets.yml ├── db ├── migrate │ ├── 20160112191931_create_users.rb │ └── 20160122193254_create_notes.rb ├── schema.rb └── seeds.rb ├── lib ├── api_constraints.rb └── tasks │ └── .keep ├── log └── .keep ├── public ├── 404.html ├── 422.html ├── 500.html ├── favicon.ico └── robots.txt └── spec ├── controllers ├── api │ └── v1 │ │ ├── concerns │ │ ├── authenticator_spec.rb │ │ ├── error_handler_spec.rb │ │ ├── internationalizator_spec.rb │ │ └── version_expiration_handler_spec.rb │ │ ├── notes_controller_spec.rb │ │ ├── sessions_controller_spec.rb │ │ ├── users_controller_spec.rb │ │ └── versions_controller_spec.rb ├── pages_controller_spec.rb └── users_controller_spec.rb ├── factories ├── notes.rb └── users.rb ├── lib └── api_constraints_spec.rb ├── mailers └── user_mailer_spec.rb ├── models ├── concerns │ ├── authenticatable_spec.rb │ ├── confirmable_spec.rb │ └── recoverable_spec.rb ├── note_spec.rb └── user_spec.rb ├── serializers └── api │ └── v1 │ ├── note_serializer_spec.rb │ └── user_serializer_spec.rb ├── spec_helper.rb └── support ├── request_helpers.rb └── test_exception_localization_handler.rb /.codeclimate.yml: -------------------------------------------------------------------------------- 1 | --- 2 | before_script: tail -n +3 .rubocop.yml > .codeclimate-rubocop.yml 3 | engines: 4 | brakeman: 5 | enabled: true 6 | bundler-audit: 7 | enabled: true 8 | duplication: 9 | enabled: true 10 | config: 11 | languages: 12 | - ruby 13 | exclude_paths: 14 | - config/ 15 | - db/ 16 | - spec/ 17 | - vendor/ 18 | fixme: 19 | enabled: true 20 | rubocop: 21 | enabled: true 22 | shellcheck: 23 | enabled: true 24 | ratings: 25 | paths: 26 | - Gemfile.lock 27 | - "**.erb" 28 | - "**.haml" 29 | - "**.rb" 30 | - "**.rhtml" 31 | - "**.slim" 32 | - "**.inc" 33 | - "**.js" 34 | - "**.jsx" 35 | - "**.module" 36 | - "**.php" 37 | - "**.py" 38 | -------------------------------------------------------------------------------- /.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 | # Ignore Byebug command history file. 21 | .byebug_history 22 | 23 | # Ignore code-climate coverage information files. 24 | /coverage/* 25 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | Rails: 2 | Enabled: true 3 | Documentation: 4 | Enabled: false 5 | AllCops: 6 | Include: 7 | - '**/Rakefile' 8 | - '**/config.ru' 9 | Exclude: 10 | - 'db/**/*' 11 | - 'config/**/*' 12 | - 'script/**/*' 13 | - !ruby/regexp /old_and_unused\.rb$/ 14 | - 'bin/*' 15 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - 2.3.0-p0 4 | services: 5 | - postgresql 6 | before_script: 7 | - psql -c "CREATE USER railsapibase_user WITH PASSWORD 'railsapibase_pass';" -U postgres 8 | - psql -c "ALTER USER railsapibase_user CREATEDB;" -U postgres 9 | - bin/rails db:setup 10 | addons: 11 | code_climate: 12 | repo_token: d52e0b4bc1ef543d8484a456329b3cff32bf93eb2ebf11df139e06694752ca57 13 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | ruby '2.3.0' 4 | 5 | # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 6 | gem 'rails' 7 | gem 'rails-i18n' 8 | # Use postgresql as the database for Active Record 9 | gem 'pg' 10 | # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 11 | # gem 'jbuilder', '~> 2.0' 12 | # Use Puma as the app server 13 | gem 'puma' 14 | 15 | # Use ActiveModel has_secure_password 16 | gem 'bcrypt' 17 | 18 | # json formatter 19 | gem 'active_model_serializers' 20 | 21 | # Force mail version to prevent security issue 22 | gem 'mail', '2.6.6.rc1' 23 | 24 | # Use Capistrano for deployment 25 | # gem 'capistrano-rails', group: :development 26 | 27 | # Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible 28 | # gem 'rack-cors' 29 | 30 | group :development, :test do 31 | # Call 'byebug' anywhere in the code to stop execution and get a debugger console 32 | gem 'byebug' 33 | end 34 | 35 | group :development do 36 | # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 37 | gem 'spring' 38 | end 39 | 40 | group :test do 41 | gem 'rspec-rails' 42 | gem 'factory_girl_rails' 43 | gem 'ffaker' 44 | gem 'codeclimate-test-reporter', require: nil 45 | gem 'rails-controller-testing' 46 | end 47 | 48 | # Windows does not include zoneinfo files, so bundle the tzinfo-data gem 49 | gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 50 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | actioncable (5.1.4) 5 | actionpack (= 5.1.4) 6 | nio4r (~> 2.0) 7 | websocket-driver (~> 0.6.1) 8 | actionmailer (5.1.4) 9 | actionpack (= 5.1.4) 10 | actionview (= 5.1.4) 11 | activejob (= 5.1.4) 12 | mail (~> 2.5, >= 2.5.4) 13 | rails-dom-testing (~> 2.0) 14 | actionpack (5.1.4) 15 | actionview (= 5.1.4) 16 | activesupport (= 5.1.4) 17 | rack (~> 2.0) 18 | rack-test (>= 0.6.3) 19 | rails-dom-testing (~> 2.0) 20 | rails-html-sanitizer (~> 1.0, >= 1.0.2) 21 | actionview (5.1.4) 22 | activesupport (= 5.1.4) 23 | builder (~> 3.1) 24 | erubi (~> 1.4) 25 | rails-dom-testing (~> 2.0) 26 | rails-html-sanitizer (~> 1.0, >= 1.0.3) 27 | active_model_serializers (0.10.6) 28 | actionpack (>= 4.1, < 6) 29 | activemodel (>= 4.1, < 6) 30 | case_transform (>= 0.2) 31 | jsonapi-renderer (>= 0.1.1.beta1, < 0.2) 32 | activejob (5.1.4) 33 | activesupport (= 5.1.4) 34 | globalid (>= 0.3.6) 35 | activemodel (5.1.4) 36 | activesupport (= 5.1.4) 37 | activerecord (5.1.4) 38 | activemodel (= 5.1.4) 39 | activesupport (= 5.1.4) 40 | arel (~> 8.0) 41 | activesupport (5.1.4) 42 | concurrent-ruby (~> 1.0, >= 1.0.2) 43 | i18n (~> 0.7) 44 | minitest (~> 5.1) 45 | tzinfo (~> 1.1) 46 | arel (8.0.0) 47 | bcrypt (3.1.11) 48 | builder (3.2.3) 49 | byebug (9.1.0) 50 | case_transform (0.2) 51 | activesupport 52 | codeclimate-test-reporter (1.0.8) 53 | simplecov (<= 0.13) 54 | concurrent-ruby (1.0.5) 55 | crass (1.0.2) 56 | diff-lcs (1.3) 57 | docile (1.1.5) 58 | erubi (1.7.0) 59 | factory_girl (4.8.1) 60 | activesupport (>= 3.0.0) 61 | factory_girl_rails (4.8.0) 62 | factory_girl (~> 4.8.0) 63 | railties (>= 3.0.0) 64 | ffaker (2.7.0) 65 | globalid (0.4.0) 66 | activesupport (>= 4.2.0) 67 | i18n (0.8.6) 68 | json (2.1.0) 69 | jsonapi-renderer (0.1.3) 70 | loofah (2.1.1) 71 | crass (~> 1.0.2) 72 | nokogiri (>= 1.5.9) 73 | mail (2.6.6.rc1) 74 | mime-types (>= 1.16, < 4) 75 | method_source (0.9.0) 76 | mime-types (3.1) 77 | mime-types-data (~> 3.2015) 78 | mime-types-data (3.2016.0521) 79 | mini_portile2 (2.3.0) 80 | minitest (5.10.3) 81 | nio4r (2.1.0) 82 | nokogiri (1.8.1) 83 | mini_portile2 (~> 2.3.0) 84 | pg (0.21.0) 85 | puma (3.10.0) 86 | rack (2.0.3) 87 | rack-test (0.7.0) 88 | rack (>= 1.0, < 3) 89 | rails (5.1.4) 90 | actioncable (= 5.1.4) 91 | actionmailer (= 5.1.4) 92 | actionpack (= 5.1.4) 93 | actionview (= 5.1.4) 94 | activejob (= 5.1.4) 95 | activemodel (= 5.1.4) 96 | activerecord (= 5.1.4) 97 | activesupport (= 5.1.4) 98 | bundler (>= 1.3.0) 99 | railties (= 5.1.4) 100 | sprockets-rails (>= 2.0.0) 101 | rails-controller-testing (1.0.2) 102 | actionpack (~> 5.x, >= 5.0.1) 103 | actionview (~> 5.x, >= 5.0.1) 104 | activesupport (~> 5.x) 105 | rails-dom-testing (2.0.3) 106 | activesupport (>= 4.2.0) 107 | nokogiri (>= 1.6) 108 | rails-html-sanitizer (1.0.3) 109 | loofah (~> 2.0) 110 | rails-i18n (5.0.4) 111 | i18n (~> 0.7) 112 | railties (~> 5.0) 113 | railties (5.1.4) 114 | actionpack (= 5.1.4) 115 | activesupport (= 5.1.4) 116 | method_source 117 | rake (>= 0.8.7) 118 | thor (>= 0.18.1, < 2.0) 119 | rake (12.1.0) 120 | rspec-core (3.6.0) 121 | rspec-support (~> 3.6.0) 122 | rspec-expectations (3.6.0) 123 | diff-lcs (>= 1.2.0, < 2.0) 124 | rspec-support (~> 3.6.0) 125 | rspec-mocks (3.6.0) 126 | diff-lcs (>= 1.2.0, < 2.0) 127 | rspec-support (~> 3.6.0) 128 | rspec-rails (3.6.1) 129 | actionpack (>= 3.0) 130 | activesupport (>= 3.0) 131 | railties (>= 3.0) 132 | rspec-core (~> 3.6.0) 133 | rspec-expectations (~> 3.6.0) 134 | rspec-mocks (~> 3.6.0) 135 | rspec-support (~> 3.6.0) 136 | rspec-support (3.6.0) 137 | simplecov (0.13.0) 138 | docile (~> 1.1.0) 139 | json (>= 1.8, < 3) 140 | simplecov-html (~> 0.10.0) 141 | simplecov-html (0.10.2) 142 | spring (2.0.2) 143 | activesupport (>= 4.2) 144 | sprockets (3.7.1) 145 | concurrent-ruby (~> 1.0) 146 | rack (> 1, < 3) 147 | sprockets-rails (3.2.1) 148 | actionpack (>= 4.0) 149 | activesupport (>= 4.0) 150 | sprockets (>= 3.0.0) 151 | thor (0.20.0) 152 | thread_safe (0.3.6) 153 | tzinfo (1.2.3) 154 | thread_safe (~> 0.1) 155 | websocket-driver (0.6.5) 156 | websocket-extensions (>= 0.1.0) 157 | websocket-extensions (0.1.2) 158 | 159 | PLATFORMS 160 | ruby 161 | 162 | DEPENDENCIES 163 | active_model_serializers 164 | bcrypt 165 | byebug 166 | codeclimate-test-reporter 167 | factory_girl_rails 168 | ffaker 169 | mail (= 2.6.6.rc1) 170 | pg 171 | puma 172 | rails 173 | rails-controller-testing 174 | rails-i18n 175 | rspec-rails 176 | spring 177 | tzinfo-data 178 | 179 | BUNDLED WITH 180 | 1.11.2 181 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rails Api Base 2 | 3 | [](https://codeclimate.com/github/jordifierro/rails-api-base) 4 | [](https://codeclimate.com/github/jordifierro/rails-api-base/coverage) 5 | [](https://travis-ci.org/jordifierro/rails-api-base) 6 | [](https://gemnasium.com/jordifierro/rails-api-base) 7 | 8 | ## Introduction 9 | 10 | This projects aims to be: 11 | 12 | * An api example to discuss about Rails 13 | setup and development. 14 | * A basic template to start projects from it. 15 | 16 | Specification summary: 17 | 18 | * RESTful api. 19 | * Api versioning. 20 | * Notes app example. 21 | * Patterns and good practices. 22 | * Users management. 23 | * Version expiration. 24 | * Internationalization. 25 | * Secret api key. 26 | * Rspec testing. 27 | * Setup scripts. 28 | * Postgres database. 29 | * Versions up-to-date. 30 | * Ruby Style Guide. 31 | * Json serialization 32 | 33 | Here is its counterpart client mobile app that consumes data from this api -> 34 | [android-base](https://github.com/jordifierro/android-base) 35 | 36 | 37 | ## Quick start 38 | 39 | * Install ruby version 2.3.0 and set it with your ruby environment manager 40 | ([more info here](https://www.ruby-lang.org/en/documentation/installation/)). 41 | 42 | * Install Postgres and start the PostgreSQL server in the foreground 43 | ([more info here](https://wiki.postgresql.org/wiki/Detailed_installation_guides)). 44 | 45 | * Clone the repository and get inside it: 46 | ``` 47 | git clone git://github.com/jordifierro/rails-api-base.git --origin rails-api-base your-project-name 48 | cd your-project-name 49 | ``` 50 | 51 | * Rename whole project and reset README.md: 52 | ``` 53 | ./bin/rename_project YourProjectName 54 | ``` 55 | 56 | * Remove all 'note' related code (optional): 57 | ``` 58 | ./bin/remove_notes 59 | ``` 60 | 61 | * Create a postgres role to let rails manage the db: 62 | ``` 63 | ./bin/create_psql_user yourprojectname 64 | ``` 65 | 66 | * Setup the gems and databases: 67 | ``` 68 | ./bin/setup 69 | ``` 70 | 71 | * Run tests: 72 | ``` 73 | rspec 74 | ``` 75 | 76 | * Once all tests are green, create a new remote repository 77 | and then execute this to reset the repo and push it: 78 | ``` 79 | ./bin/reset_git https://github.com/yourusername/your-project-name.git 80 | ``` 81 | 82 | That's it, you can now start developing your own app! 83 | 84 | (While developing on localhost, start [mailcatcher](https://github.com/sj26/mailcatcher) 85 | in order to receive user confirmation and recover password emails) 86 | ``` 87 | gem install mailcatcher 88 | mailcatcher 89 | ``` 90 | 91 | ## Documentation 92 | 93 | The application itself is almost empty, 94 | it only aims to provide some basic modules, 95 | implement the structures with some patterns and give sample code. 96 | Here are the specifications: 97 | 98 | #### RESTful Api 99 | The app includes only the rails-api related modules, 100 | so it's thinner than a normal app but lacks some features 101 | (that can be manually added if required). 102 | The architecture of the api follows rails and http restful good practices 103 | such as: 104 | * Usage of http methods/verbs. 105 | * Structured endpoints. 106 | * Return appropriate status code. 107 | 108 | #### Api Versioning 109 | The endpoint routes and the code structure are ready to add new api versions. 110 | The version is chosen via `headers['Accept']` with values like 111 | `application/vnd.railsapibase.v1` to use the first version. 112 | 113 | #### Notes Example Code 114 | To provide the app sample code, it has been developed code to manage `notes` 115 | (like handwritten paper notes representation), 116 | composed by a `title` and a `content`. 117 | Thus, the app has the notes routes, controller, model and rspecs 118 | to work with that notes. 119 | 120 | Its unique purpose is to be a guide of how to add new code, 121 | so it will be deleted by the `bin/remove_notes` shell script. 122 | 123 | #### Concerns and ApiController Pattern 124 | [](https://codeclimate.com/github/jordifierro/rails-api-base) 125 | 126 | To structure the global controller features of the api, 127 | different modules have been implemented as 128 | [ActiveSupport::Concern](http://api.rubyonrails.org/classes/ActiveSupport/Concern.html) 129 | and tested using fake controllers. 130 | Those modules are included to the ApiController, 131 | which is the father controller of the rest of controllers 132 | ([check this post](http://jordifierro.com/rails-apicontroller-and-concerns)). 133 | At the moment there are 4 modules: authentication, error handling, 134 | internationalization and version expiration 135 | ([check this other](http://jordifierro.com/rails-api-modules)). 136 | [Code Climate](https://codeclimate.com/) is the service used 137 | to check that this and all the rest of the code follows good practices 138 | (you have to activate it for your project to use it). 139 | 140 | Codeclimate can also be run locally with its 141 | [CLI](https://github.com/codeclimate/codeclimate). 142 | 143 | 144 | #### Users Management 145 | Almost every api requires users, sessions and authentications, 146 | so this is the most important feature of this app. 147 | The chosen solution uses `has_secure_password` and `has_secure_token` 148 | with a custom implementation to handle sessions and users: 149 | 150 | * Create and delete users. 151 | * Login and logout users. 152 | * Authenticate users by token. 153 | * Confirm email with sending an email with token. 154 | * Reset password when forgotten with email verification. 155 | 156 | A token is returned when the users login 157 | and it has to be set to `headers['Authorization']` 158 | on later requests to authenticate them. 159 | More info about that on 160 | ([this post](http://jordifierro.com/rails-api-modules#authenticator)) 161 | 162 | #### Version Expiration System 163 | To check if a version can still be used, 164 | there's a module that filters that before each method call. 165 | It will return error if the version has expired 166 | and there's also an endpoint to check the expiration date from the client 167 | (e.g.: to warn the user to update the app). 168 | If you want to set expiration date to a concrete version, 169 | simply set a integer formatted to string to `ENV['LAST_EXPIRED_VERSION']`. 170 | All versions equal or below the specified will send upgrade error message 171 | when asked. The system to set a warning to some versions is the same, 172 | using `ENV['LAST_WARNED_VERSION']` to set the higher version that you want 173 | to warn. 174 | More info about that on 175 | ([this post](http://jordifierro.com/rails-api-modules#version-expiration-handler)) 176 | 177 | #### Internationalization 178 | The app is translated to English (default language) 179 | and Spanish (just as translation example). 180 | There is a simple module that takes the locale from 181 | `request.env['HTTP_ACCEPT_LANGUAGE']` 182 | (that can be set throught the `Accept-Languange` header) 183 | and sets it to the system 184 | to automatically return the appropriate translation. 185 | More info about that on 186 | ([this post](http://jordifierro.com/rails-api-modules#internationalizator)) 187 | 188 | To test that all needed translations are set for an specific language, 189 | uncomment the following line to the `spec_helper.rb` file, 190 | place there the target language and run `rspec`: 191 | ``` 192 | I18n.default_locale = :es 193 | ``` 194 | 195 | #### Secret Api Key 196 | In order to add some control over the api clients, 197 | there's an secret api key verification system that can be 198 | activated to make sure that is a valid client 199 | who creates the user. 200 | To activate this service 201 | just set a value to `ENV['SECRET_API_KEY']`. 202 | The secret api key must be sent at `headers['Authorization']` 203 | when calling create new user method. 204 | 205 | #### Rspec Testing 206 | [](https://codeclimate.com/github/jordifierro/rails-api-base/coverage) 207 | [](https://travis-ci.org/jordifierro/rails-api-base) 208 | 209 | This project has been developed using TDD process 210 | and all code is tested using Rspec, 211 | following best practices guidelines defined at 212 | [betterspecs.org](http://betterspecs.org/). 213 | It's important to keep it that way. 214 | [Code Climate](https://codeclimate.com/) checks 215 | that the tests cover all the code cases. 216 | [Travis-CI](https://travis-ci.org/) is a continous integration system 217 | that runs the tests every time a push is made. 218 | If you want to use this services, you have to enable them at their websites. 219 | If you don't, simply delete the `.travis.yml` file. 220 | 221 | #### Setup scripts 222 | To avoid the burden of manually modify the code to prepare 223 | the files to start a new project, some scripts 224 | have been implemented. You can find them inside `bin/` folder 225 | (they are self destroyed after use). 226 | 227 | They have been analyzed by 228 | [ShellCheck](https://github.com/koalaman/shellcheck). 229 | 230 | #### Postgres Database 231 | To avoid deployment problems, 232 | [Postgres](http://www.postgresql.org/) database 233 | has been setup from the beginning 234 | as the database system for testing and development. 235 | The fact that [Heroku](https://www.heroku.com/) 236 | uses it as its default db system has been considered too. 237 | 238 | #### Latest Ruby and Gems Versions 239 | [](https://gemnasium.com/jordifierro/rails-api-base) 240 | 241 | The project uses Rails 5.1.4 (API module) and Ruby 2.3.0 242 | and intends to be kept up to date using 243 | [Gemnasium](https://gemnasium.com) service. 244 | You must activate this service for your repo if you want to use it. 245 | 246 | #### Follow Ruby Style Guide 247 | In order to increase the code elegance and readability, 248 | this [Ruby Style Guide](https://github.com/bbatsov/ruby-style-guide) 249 | has been used as reference. 250 | [Rubocop gem](https://github.com/bbatsov/rubocop) 251 | is a Ruby static code analizer based on that style guide. 252 | Just run: 253 | ``` 254 | gem install rubocop 255 | rubocop 256 | ``` 257 | Remember that `.rubocop.yml` file defines the configuration 258 | (remove it if not used). 259 | 260 | #### Json output serialization 261 | The responses are formatted using the 262 | [ActiveModelSerializers](https://github.com/rails-api/active_model_serializers) 263 | gem. 264 | Serializers are responsible to format the output json, 265 | and are a good way to decouple this layer from models and controllers. 266 | Furthermore, they are versioned like controllers (e.g.: `Api::V1::Serializer`) 267 | because they directly interfere with the output of each api version. 268 | This will help us keeping old version contracts. 269 | 270 | #### Todo List 271 | 272 | - [ ] Add elements pagination. 273 | - [x] Upgrade to ruby and rails latest versions. 274 | 275 | 276 | Here is its counterpart client mobile app that consumes data from this api -> 277 | [android-base](https://github.com/jordifierro/android-base) 278 | 279 | 280 | ## Contribute 281 | 282 | I'm not a rails experienced developer 283 | so all suggestions and contributions are more than welcome! 284 | 285 | * Fork this repo. 286 | * Create your feature branch (git checkout -b feature-name). 287 | * Develop your feature and test it. 288 | * Run tests and code style checker successfully: 289 | ``` 290 | rspec 291 | rubocop 292 | ``` 293 | * Commit your changes (git commit -m 'Implement new function'). 294 | * Push the changes (git push origin feature-name). 295 | * Create a pull request and I'll merge it with the project. 296 | 297 | #### Contributors 298 | 299 | Unfortunately, there are no contributors yet. 300 | 301 | ______________________ 302 | http://jordifierro.com 303 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # Add your own tasks in files placed in lib/tasks ending in .rake, 2 | # for example lib/tasks/capistrano.rake, 3 | # and they will automatically be available to Rake. 4 | 5 | require File.expand_path('../config/application', __FILE__) 6 | 7 | Rails.application.load_tasks 8 | -------------------------------------------------------------------------------- /app/channels/application_cable/channel.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | # Action Cable runs in an EventMachine 3 | # loop that does not support auto reloading. 4 | module ApplicationCable 5 | class Channel < ActionCable::Channel::Base 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /app/channels/application_cable/connection.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | # Action Cable runs in an EventMachine loop 3 | # that does not support auto reloading. 4 | module ApplicationCable 5 | class Connection < ActionCable::Connection::Base 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /app/controllers/api/v1/api_controller.rb: -------------------------------------------------------------------------------- 1 | module Api 2 | module V1 3 | class ApiController < ApplicationController 4 | include ::ActionController::Serialization 5 | 6 | include Concerns::Authenticator 7 | include Concerns::ErrorHandler 8 | include Concerns::VersionExpirationHandler 9 | include Concerns::Internationalizator 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /app/controllers/api/v1/concerns/authenticator.rb: -------------------------------------------------------------------------------- 1 | module Api 2 | module V1 3 | module Concerns 4 | module Authenticator 5 | extend ActiveSupport::Concern 6 | 7 | included do 8 | before_action :auth_with_token! 9 | end 10 | 11 | # Devise methods overwrites 12 | def current_user 13 | @current_user ||= 14 | User.find_by(auth_token: request.headers['Authorization']) 15 | end 16 | 17 | def user_signed_in? 18 | current_user.present? 19 | end 20 | 21 | def auth_with_token! 22 | head :unauthorized unless user_signed_in? 23 | end 24 | end 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /app/controllers/api/v1/concerns/error_handler.rb: -------------------------------------------------------------------------------- 1 | module Api 2 | module V1 3 | module Concerns 4 | module ErrorHandler 5 | extend ActiveSupport::Concern 6 | 7 | included do 8 | rescue_from ActiveRecord::RecordNotFound, with: :not_found 9 | end 10 | 11 | def render_error(message, status) 12 | status_code = Rack::Utils::SYMBOL_TO_STATUS_CODE[status] 13 | render json: { error: { status: status_code, message: message } }, 14 | status: status 15 | end 16 | 17 | def not_found 18 | render_error(I18n.t('errors.messages.not_found'), :not_found) 19 | end 20 | end 21 | end 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /app/controllers/api/v1/concerns/internationalizator.rb: -------------------------------------------------------------------------------- 1 | module Api 2 | module V1 3 | module Concerns 4 | module Internationalizator 5 | extend ActiveSupport::Concern 6 | 7 | included do 8 | before_action :set_locale 9 | end 10 | 11 | def set_locale 12 | I18n.locale = extract_locale_from_accept_language_header || 13 | I18n.default_locale 14 | end 15 | 16 | private 17 | 18 | def extract_locale_from_accept_language_header 19 | req_lang = request.env['HTTP_ACCEPT_LANGUAGE'] 20 | if req_lang 21 | language = req_lang.scan(/^[a-z]{2}/).first 22 | language if I18n.available_locales.include?(language.to_sym) 23 | end 24 | end 25 | end 26 | end 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /app/controllers/api/v1/concerns/version_expiration_handler.rb: -------------------------------------------------------------------------------- 1 | require 'date' 2 | 3 | module Api 4 | module V1 5 | module Concerns 6 | module VersionExpirationHandler 7 | extend ActiveSupport::Concern 8 | OK = 'OK'.freeze 9 | WARNED = 'WARNED'.freeze 10 | EXPIRED = 'EXPIRED'.freeze 11 | 12 | included do 13 | before_action :check_expiration! 14 | end 15 | 16 | def api_version 17 | self.class.superclass.name.to_s.split('::').second.sub('V', '').to_i 18 | end 19 | 20 | def expiration_state 21 | @expired ||= ENV['LAST_EXPIRED_VERSION'] 22 | return EXPIRED if @expired && api_version <= @expired.to_i 23 | @warned ||= ENV['LAST_WARNED_VERSION'] 24 | return WARNED if @warned && api_version <= @warned.to_i 25 | OK 26 | end 27 | 28 | def check_expiration! 29 | render_error(I18n.t('version.expired'), 30 | :upgrade_required) unless supported_version? 31 | end 32 | 33 | def supported_version? 34 | expiration_state && expiration_state != EXPIRED 35 | end 36 | end 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /app/controllers/api/v1/notes_controller.rb: -------------------------------------------------------------------------------- 1 | module Api 2 | module V1 3 | class NotesController < ApiController 4 | def index 5 | render json: current_user.notes.all 6 | end 7 | 8 | def show 9 | render json: current_user.notes.find(params[:id]) 10 | end 11 | 12 | def create 13 | note = current_user.notes.new(note_params) 14 | if note.save 15 | render json: note, status: :created 16 | else 17 | render_error(note.errors.full_messages[0], :unprocessable_entity) 18 | end 19 | end 20 | 21 | def update 22 | note = current_user.notes.find(params[:id]) 23 | if note.update(note_params) 24 | render json: note 25 | else 26 | render_error(note.errors.full_messages[0], :unprocessable_entity) 27 | end 28 | end 29 | 30 | def destroy 31 | note = current_user.notes.find(params[:id]) 32 | note.destroy 33 | head :no_content 34 | end 35 | 36 | private 37 | 38 | def note_params 39 | params.require(:note).permit(:title, :content) 40 | end 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /app/controllers/api/v1/sessions_controller.rb: -------------------------------------------------------------------------------- 1 | module Api 2 | module V1 3 | class SessionsController < ApiController 4 | skip_before_action :auth_with_token!, only: [:create] 5 | 6 | def create 7 | user = login_user 8 | if user 9 | render json: user, status: :ok 10 | else 11 | render_error(I18n.t('authentication.error', 12 | authentication_keys: 'email'), 13 | :unprocessable_entity) 14 | end 15 | end 16 | 17 | def destroy 18 | current_user.regenerate_auth_token 19 | head :no_content 20 | end 21 | 22 | private 23 | 24 | def login_user 25 | user = User.find_by_email(params[:user][:email]) 26 | if user && user.authenticate(params[:user][:password]) 27 | user.regenerate_auth_token 28 | user 29 | end 30 | end 31 | end 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /app/controllers/api/v1/users_controller.rb: -------------------------------------------------------------------------------- 1 | module Api 2 | module V1 3 | class UsersController < ApiController 4 | skip_before_action :auth_with_token!, only: [:create, :reset_password] 5 | 6 | def create 7 | if correct_secret_api_key? 8 | user = User.new(user_params) 9 | if user.save 10 | render json: user, status: :created 11 | else 12 | render_error(user.errors.full_messages[0], :unprocessable_entity) 13 | end 14 | end 15 | end 16 | 17 | def destroy 18 | current_user.notes.destroy_all 19 | current_user.destroy 20 | head :no_content 21 | end 22 | 23 | def reset_password 24 | user = User.find_by_email(user_params[:email]) 25 | if user 26 | user.ask_reset_password(user_params[:new_password], 27 | user_params[:new_password_confirmation]) 28 | else 29 | user = User.new(user_params) 30 | user.password = '12345678' 31 | end 32 | reset_password_output(user) 33 | end 34 | 35 | private 36 | 37 | def user_params 38 | params.require(:user).permit(:email, :password, :password_confirmation, 39 | :new_password, :new_password_confirmation) 40 | end 41 | 42 | def correct_secret_api_key? 43 | if request.headers['Authorization'] == ENV['SECRET_API_KEY'] 44 | true 45 | else 46 | head :unauthorized 47 | false 48 | end 49 | end 50 | 51 | def reset_password_output(user) 52 | if user.valid? 53 | render json: { message: I18n.t('reset_password.sent') }, 54 | status: :accepted 55 | else 56 | render_error(user.errors.full_messages[0], :unprocessable_entity) 57 | end 58 | end 59 | end 60 | end 61 | end 62 | -------------------------------------------------------------------------------- /app/controllers/api/v1/versions_controller.rb: -------------------------------------------------------------------------------- 1 | module Api 2 | module V1 3 | class VersionsController < ApiController 4 | skip_before_action :check_expiration! 5 | 6 | def state 7 | render json: { state: expiration_state } 8 | end 9 | end 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /app/controllers/application_controller.rb: -------------------------------------------------------------------------------- 1 | class ApplicationController < ActionController::API 2 | end 3 | -------------------------------------------------------------------------------- /app/controllers/concerns/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jordifierro/rails-api-base/2bd3084454045d7a417bc8580396bed68027edf9/app/controllers/concerns/.keep -------------------------------------------------------------------------------- /app/controllers/pages_controller.rb: -------------------------------------------------------------------------------- 1 | class PagesController < ViewController 2 | def privacy 3 | end 4 | 5 | def terms 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /app/controllers/users_controller.rb: -------------------------------------------------------------------------------- 1 | class UsersController < ViewController 2 | def confirm 3 | user = User.find_by_confirmation_token(params[:token]) 4 | if user 5 | user.confirmed_at = DateTime.current 6 | user.confirmation_token = nil 7 | user.save 8 | render '/users/confirmed' 9 | else 10 | render 'users/invalid_confirmation' 11 | end 12 | end 13 | 14 | def confirm_reset 15 | user = User.find_by_reset_password_token(params[:token]) 16 | if user && user.reset_password_digest 17 | user.password_digest = user.reset_password_digest 18 | user.reset_password_token = nil 19 | user.reset_password_digest = nil 20 | user.save 21 | render '/users/reset_confirmed' 22 | else 23 | render 'users/invalid_reset_confirmation' 24 | end 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /app/controllers/view_controller.rb: -------------------------------------------------------------------------------- 1 | class ViewController < ActionController::Base 2 | include Api::V1::Concerns::Internationalizator 3 | end 4 | -------------------------------------------------------------------------------- /app/jobs/application_job.rb: -------------------------------------------------------------------------------- 1 | class ApplicationJob < ActiveJob::Base 2 | end 3 | -------------------------------------------------------------------------------- /app/mailers/application_mailer.rb: -------------------------------------------------------------------------------- 1 | class ApplicationMailer < ActionMailer::Base 2 | default from: 'from@example.com' 3 | end 4 | -------------------------------------------------------------------------------- /app/mailers/user_mailer.rb: -------------------------------------------------------------------------------- 1 | class UserMailer < ApplicationMailer 2 | def ask_email_confirmation(user) 3 | @url = users_confirm_url(user.confirmation_token) 4 | mail to: user.email, 5 | subject: I18n.t('email_confirmation.subject') 6 | end 7 | 8 | def ask_reset_password(user) 9 | @url = users_confirm_reset_url(user.reset_password_token) 10 | mail to: user.email, 11 | subject: I18n.t('reset_password.subject') 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /app/models/application_record.rb: -------------------------------------------------------------------------------- 1 | class ApplicationRecord < ActiveRecord::Base 2 | self.abstract_class = true 3 | end 4 | -------------------------------------------------------------------------------- /app/models/concerns/authenticatable.rb: -------------------------------------------------------------------------------- 1 | module Concerns 2 | module Authenticatable 3 | extend ActiveSupport::Concern 4 | 5 | included do 6 | has_secure_password 7 | has_secure_token :auth_token 8 | 9 | validates :email, uniqueness: true, format: /@/ 10 | validates :password, length: { minimum: 8 }, on: :create 11 | end 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /app/models/concerns/confirmable.rb: -------------------------------------------------------------------------------- 1 | module Concerns 2 | module Confirmable 3 | extend ActiveSupport::Concern 4 | 5 | included do 6 | has_secure_token :confirmation_token 7 | 8 | after_create :ask_email_confirmation 9 | end 10 | 11 | private 12 | 13 | def ask_email_confirmation 14 | self.confirmation_sent_at = DateTime.current 15 | save 16 | UserMailer.ask_email_confirmation(self).deliver 17 | end 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /app/models/concerns/recoverable.rb: -------------------------------------------------------------------------------- 1 | module Concerns 2 | module Recoverable 3 | extend ActiveSupport::Concern 4 | 5 | included do 6 | has_secure_token :reset_password_token 7 | 8 | attr_accessor :new_password, :new_password_confirmation 9 | validates :new_password, confirmation: true, length: { minimum: 8 }, 10 | allow_nil: true 11 | end 12 | 13 | def ask_reset_password(new_password, new_password_confirmation) 14 | self.new_password = new_password 15 | self.new_password_confirmation = new_password_confirmation 16 | self.reset_password_digest = generate_password_digest(new_password) 17 | if valid? 18 | regenerate_reset_password_token 19 | self.reset_password_sent_at = DateTime.current 20 | UserMailer.ask_reset_password(self).deliver if save 21 | else 22 | self.reset_password_digest = nil 23 | end 24 | end 25 | 26 | private 27 | 28 | def generate_password_digest(new_password) 29 | current_password_digest = password_digest 30 | self.password = new_password 31 | self.password_confirmation = new_password 32 | new_password_digest = password_digest 33 | self.password_digest = current_password_digest 34 | new_password_digest 35 | end 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /app/models/note.rb: -------------------------------------------------------------------------------- 1 | class Note < ApplicationRecord 2 | belongs_to :user 3 | 4 | validates :title, presence: true 5 | end 6 | -------------------------------------------------------------------------------- /app/models/user.rb: -------------------------------------------------------------------------------- 1 | class User < ApplicationRecord 2 | include Concerns::Authenticatable 3 | include Concerns::Confirmable 4 | include Concerns::Recoverable 5 | 6 | has_many :notes 7 | end 8 | -------------------------------------------------------------------------------- /app/serializers/api/v1/note_serializer.rb: -------------------------------------------------------------------------------- 1 | module Api 2 | module V1 3 | class NoteSerializer < ActiveModel::Serializer 4 | attributes :id, :title, :content 5 | end 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /app/serializers/api/v1/user_serializer.rb: -------------------------------------------------------------------------------- 1 | module Api 2 | module V1 3 | class UserSerializer < ActiveModel::Serializer 4 | attributes :email, :auth_token 5 | end 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /app/views/pages/privacy.html.erb: -------------------------------------------------------------------------------- 1 |
<%= t('privacy.content_1') %>
3 | -------------------------------------------------------------------------------- /app/views/pages/terms.html.erb: -------------------------------------------------------------------------------- 1 |<%= t('terms.content_1') %>
3 | -------------------------------------------------------------------------------- /app/views/user_mailer/ask_email_confirmation.html.erb: -------------------------------------------------------------------------------- 1 |2 | <%= I18n.t('email_confirmation.ask') %> 3 | <%= @url %>. 4 |
5 | -------------------------------------------------------------------------------- /app/views/user_mailer/ask_email_confirmation.text.erb: -------------------------------------------------------------------------------- 1 | <%= I18n.t('email_confirmation.ask') + " " + @url + "." %> 2 | -------------------------------------------------------------------------------- /app/views/user_mailer/ask_reset_password.html.erb: -------------------------------------------------------------------------------- 1 |2 | <%= I18n.t('reset_password.ask') %> 3 | <%= @url %>. 4 |
5 | -------------------------------------------------------------------------------- /app/views/user_mailer/ask_reset_password.text.erb: -------------------------------------------------------------------------------- 1 | <%= I18n.t('reset_password.ask') + " " + @url + "." %> 2 | -------------------------------------------------------------------------------- /app/views/users/confirmed.html.erb: -------------------------------------------------------------------------------- 1 |<%= I18n.t('email_confirmation.success') %>
2 | -------------------------------------------------------------------------------- /app/views/users/invalid_confirmation.html.erb: -------------------------------------------------------------------------------- 1 |<%= I18n.t('email_confirmation.error') %>
2 | -------------------------------------------------------------------------------- /app/views/users/invalid_reset_confirmation.html.erb: -------------------------------------------------------------------------------- 1 |<%= I18n.t('reset_password.error') %>
2 | -------------------------------------------------------------------------------- /app/views/users/reset_confirmed.html.erb: -------------------------------------------------------------------------------- 1 |<%= I18n.t('reset_password.success') %>
2 | -------------------------------------------------------------------------------- /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/create_psql_user: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | APP_NAME="$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')" 4 | USER_NAME="$APP_NAME"_user 5 | USER_PASS="$APP_NAME"_pass 6 | psql -c "CREATE USER $USER_NAME WITH PASSWORD '$USER_PASS';" 7 | psql -c "ALTER USER $USER_NAME CREATEDB;" 8 | 9 | rm bin/create_psql_user 10 | -------------------------------------------------------------------------------- /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/remove_notes: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | find . -name "*note*" -type f -delete 4 | 5 | sed -i.bak '/note/d' ./config/routes.rb; rm config/routes.rb.bak 6 | sed -i.bak '/note/d' ./app/models/user.rb; rm app/models/user.rb.bak 7 | sed -i.bak '/note/d' ./app/controllers/api/v1/users_controller.rb 8 | rm app/controllers/api/v1/users_controller.rb.bak 9 | sed -i.bak '/note/d' ./spec/controllers/api/v1/users_controller_spec.rb 10 | rm spec/controllers/api/v1/users_controller_spec.rb.bak 11 | sed -i.bak '/note/d' ./spec/models/user_spec.rb; 12 | rm spec/models/user_spec.rb.bak 13 | 14 | echo Notes related code files removed 15 | -------------------------------------------------------------------------------- /bin/rename_project: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | APP_NAME=$1 4 | find . -not -path './.*/*' -type f -exec \ 5 | sed -i '' -e "s/RailsApiBase/$APP_NAME/g" '{}' + 6 | echo "# $APP_NAME" > README.md 7 | 8 | APP_NAME="$(printf '%s' "$APP_NAME" | tr '[:upper:]' '[:lower:]')" 9 | find . -not -path './.*/*' -type f -exec \ 10 | sed -i '' -e "s/railsapibase/$APP_NAME/g" '{}' + 11 | 12 | echo Project name changed and README.md content emptied 13 | 14 | rm bin/rename_project 15 | -------------------------------------------------------------------------------- /bin/reset_git: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | rm bin/reset_git 4 | 5 | rm -rf .git/ 6 | 7 | git init 8 | git add . 9 | git commit -m "Initialize project from rails-api-base" 10 | git remote add origin "$1" 11 | git push -u origin master 12 | -------------------------------------------------------------------------------- /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! 'bin/rails db:setup' 28 | 29 | puts "\n== Removing old logs and tempfiles ==" 30 | system! 'bin/rails log:clear tmp:clear' 31 | 32 | puts "\n== Restarting application server ==" 33 | system! 'bin/rails restart' 34 | end 35 | -------------------------------------------------------------------------------- /bin/spring: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | # This file loads spring without using Bundler, in order to be fast. 4 | # It gets overwritten when you run the `spring binstub` command. 5 | 6 | unless defined?(Spring) 7 | require 'rubygems' 8 | require 'bundler' 9 | 10 | if (match = Bundler.default_lockfile.read.match(/^GEM$.*?^ (?: )*spring \((.*?)\)$.*?^$/m)) 11 | Gem.paths = { 'GEM_PATH' => [Bundler.bundle_path.to_s, *Gem.path].uniq } 12 | gem 'spring', match[1] 13 | require 'spring/binstub' 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /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/rails db:migrate' 23 | 24 | puts "\n== Removing old logs and tempfiles ==" 25 | system! 'bin/rails log:clear tmp:clear' 26 | 27 | puts "\n== Restarting application server ==" 28 | system! 'bin/rails 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 | 5 | # Action Cable uses EventMachine which requires 6 | # that all classes are loaded in advance 7 | Rails.application.eager_load! 8 | 9 | run Rails.application 10 | -------------------------------------------------------------------------------- /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 "action_cable/engine" 12 | # require "sprockets/railtie" 13 | require "rails/test_unit/railtie" 14 | 15 | # Require the gems listed in Gemfile, including any gems 16 | # you've limited to :test, :development, or :production. 17 | Bundler.require(*Rails.groups) 18 | 19 | module RailsApiBase 20 | class Application < Rails::Application 21 | # Settings in config/environments/* take precedence over those specified here. 22 | # Application configuration should go into files in config/initializers 23 | # -- all .rb files in that directory are automatically loaded. 24 | 25 | # Only loads a smaller set of middleware suitable for API only apps. 26 | # Middleware like session, flash, cookies can be added back manually. 27 | # Skip views, helpers and assets when generating a new resource. 28 | config.api_only = true 29 | 30 | # don't generate RSpec tests for views and helpers 31 | config.generators do |g| 32 | g.test_framework :rspec, fixture: true 33 | g.fixture_replacement :factory_girl, dir: 'spec/factories' 34 | g.view_specs false 35 | g.helper_specs false 36 | g.stylesheets = false 37 | g.javascripts = false 38 | g.helper = false 39 | end 40 | 41 | config.autoload_paths += %W(\#{config.root}/lib) 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /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/cable.yml: -------------------------------------------------------------------------------- 1 | # Action Cable uses Redis by default to administer connections, channels, and sending/receiving messages over the WebSocket. 2 | production: 3 | adapter: redis 4 | url: redis://localhost:6379/1 5 | 6 | development: 7 | adapter: async 8 | 9 | test: 10 | adapter: async 11 | -------------------------------------------------------------------------------- /config/database.yml: -------------------------------------------------------------------------------- 1 | default: &default 2 | adapter: postgresql 3 | encoding: unicode 4 | pool: 5 5 | timeout: 5000 6 | 7 | development: 8 | <<: *default 9 | database: railsapibase_development 10 | username: railsapibase_user 11 | password: railsapibase_pass 12 | 13 | # Warning: The database defined as "test" will be erased and 14 | # re-generated from your development database when you run "rake". 15 | # Do not set this db to the same as development or production. 16 | test: 17 | <<: *default 18 | database: railsapibase_test 19 | username: railsapibase_user 20 | password: railsapibase_pass 21 | 22 | production: 23 | <<: *default 24 | -------------------------------------------------------------------------------- /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 | 19 | config.action_mailer.perform_caching = false 20 | 21 | config.cache_store = :memory_store 22 | config.public_file_server.headers = { 23 | 'Cache-Control' => 'public, max-age=172800' 24 | } 25 | else 26 | config.action_controller.perform_caching = false 27 | 28 | config.action_mailer.perform_caching = false 29 | 30 | config.cache_store = :null_store 31 | end 32 | 33 | # Don't care if the mailer can't send. 34 | config.action_mailer.perform_deliveries = true 35 | config.action_mailer.raise_delivery_errors = true 36 | config.action_mailer.default_url_options = { :host => 'localhost:3000' } 37 | config.action_mailer.delivery_method = :smtp 38 | config.action_mailer.smtp_settings = { :address => "localhost", 39 | :port => 1025 } 40 | 41 | # Print deprecation notices to the Rails logger. 42 | config.active_support.deprecation = :log 43 | 44 | # Raise an error on page load if there are pending migrations. 45 | config.active_record.migration_error = :page_load 46 | 47 | 48 | # Raises error for missing translations 49 | config.action_view.raise_on_missing_translations = true 50 | config.i18n.available_locales = [:en, :es] 51 | 52 | # Use an evented file watcher to asynchronously detect changes in source code, 53 | # routes, locales, etc. This feature depends on the listen gem. 54 | # config.file_watcher = ActiveSupport::EventedFileUpdateChecker 55 | end 56 | -------------------------------------------------------------------------------- /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 | # Action Cable endpoint configuration 30 | # config.action_cable.url = 'wss://example.com/cable' 31 | # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ] 32 | 33 | # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 34 | # config.force_ssl = true 35 | 36 | # Use the lowest log level to ensure availability of diagnostic information 37 | # when problems arise. 38 | config.log_level = :debug 39 | 40 | # Prepend all log lines with the following tags. 41 | config.log_tags = [ :request_id ] 42 | 43 | # Use a different logger for distributed setups. 44 | # require 'syslog/logger' 45 | # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') 46 | 47 | if ENV["RAILS_LOG_TO_STDOUT"].present? 48 | config.logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT)) 49 | end 50 | 51 | # Use a different cache store in production. 52 | # config.cache_store = :mem_cache_store 53 | 54 | # Use a real queuing backend for Active Job (and separate queues per environment) 55 | # config.active_job.queue_adapter = :resque 56 | # config.active_job.queue_name_prefix = "rails_api_base_#{Rails.env}" 57 | config.action_mailer.perform_caching = false 58 | 59 | # Ignore bad email addresses and do not raise email delivery errors. 60 | # Set this to true and configure the email server for immediate delivery to raise delivery errors. 61 | # config.action_mailer.raise_delivery_errors = false 62 | 63 | # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 64 | # the I18n.default_locale when a translation cannot be found). 65 | config.i18n.fallbacks = true 66 | config.i18n.available_locales = [:en, :es] 67 | 68 | # Send deprecation notices to registered listeners. 69 | config.active_support.deprecation = :notify 70 | 71 | # Use default logging formatter so that PID and timestamp are not suppressed. 72 | config.log_formatter = ::Logger::Formatter.new 73 | 74 | # Do not dump schema after migrations. 75 | config.active_record.dump_schema_after_migration = false 76 | end 77 | -------------------------------------------------------------------------------- /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 | config.action_mailer.perform_caching = false 31 | 32 | # Tell Action Mailer not to deliver emails to the real world. 33 | # The :test delivery method accumulates sent emails in the 34 | # ActionMailer::Base.deliveries array. 35 | config.action_mailer.delivery_method = :test 36 | config.action_mailer.default_url_options = { :host => "test.host" } 37 | 38 | # Print deprecation notices to the stderr. 39 | config.active_support.deprecation = :stderr 40 | 41 | # Raises error for missing translations 42 | config.action_view.raise_on_missing_translations = true 43 | config.i18n.available_locales = [:en, :es] 44 | end 45 | -------------------------------------------------------------------------------- /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. This is a new Rails 5.0 4 | # default, so it is introduced as a configuration option to ensure that apps 5 | # made on earlier versions of Rails are not affected when upgrading. 6 | Rails.application.config.active_record.belongs_to_required_by_default = true 7 | -------------------------------------------------------------------------------- /config/initializers/application_controller_renderer.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 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. This is a new 4 | # Rails 5.0 default, so it is introduced as a configuration option to ensure 5 | # that apps made with earlier versions of Rails are not affected when upgrading. 6 | ActiveSupport.halt_callback_chains_on_return_false = false 7 | -------------------------------------------------------------------------------- /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/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 | authentication: 24 | error: "Invalid %{authentication_keys} or password." 25 | version: 26 | expired: "Your current version has expired, please update to the latest one." 27 | email_confirmation: 28 | subject: "Email confirmation" 29 | ask: "Please, confirm your email by clicking here:" 30 | success: "Your email address has been successfully confirmed." 31 | error: "Invalid confirmation token!" 32 | reset_password: 33 | subject: "Reset password confirmation" 34 | sent: "Check your email to confirm the new password" 35 | ask: "Please, confirm you have asked a password reset by clicking here:" 36 | success: "Your password has been successfully updated." 37 | error: "Invalid reset password token!" 38 | privacy: 39 | title_1: "Privacy policy" 40 | content_1: "Place here your privacy policy." 41 | terms: 42 | title_1: "Terms and conditions of service" 43 | content_1: "Place here your terms and conditions." 44 | errors: 45 | messages: 46 | not_found: "Not found." 47 | -------------------------------------------------------------------------------- /config/locales/es.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 | es: 23 | authentication: 24 | error: "%{authentication_keys} o contraseña incorrectos." 25 | version: 26 | expired: "Tu versión actual ha expirado, por favor actualiza a una versión más nueva." 27 | email_confirmation: 28 | subject: "Confirmar correo" 29 | ask: "Por favor, confirma tu email clicando aquí:" 30 | success: "Tu dirección de correo ha sido confirmada correctamente." 31 | error: "Clave de confirmación inválida!" 32 | reset_password: 33 | subject: "Confirmar cambio de contraseña" 34 | sent: "Comprueba tu correo para confirmar la nueva contraseña" 35 | ask: "Por favor, confirma que has pedido un cambio de contraseña clicando aquí:" 36 | success: "Tu contraseña ha sido actualizada correctamente." 37 | error: "Clave de cambio de contraseña incorrecto!" 38 | privacy: 39 | title_1: "Política de privacidad" 40 | content_1: "Coloca aquí tu política de privacidad." 41 | terms: 42 | title_1: "Terminos y condiciones de uso" 43 | content_1: "Coloca aquí tus terminos y condiciones." 44 | errors: 45 | messages: 46 | not_found: "No se ha encontrado." 47 | -------------------------------------------------------------------------------- /config/puma.rb: -------------------------------------------------------------------------------- 1 | # Puma can serve each request in a thread from an internal thread pool. 2 | # The `threads` method setting takes two numbers a minimum and maximum. 3 | # Any libraries that use thread pools should be configured to match 4 | # the maximum value specified for Puma. Default is set to 5 threads for minimum 5 | # and maximum, this matches the default thread size of Active Record. 6 | # 7 | threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i 8 | threads threads_count, threads_count 9 | 10 | # Specifies the `port` that Puma will listen on to receive requests, default is 3000. 11 | # 12 | port ENV.fetch("PORT") { 3000 } 13 | 14 | # Specifies the `environment` that Puma will run in. 15 | # 16 | environment ENV.fetch("RAILS_ENV") { "development" } 17 | 18 | # Specifies the number of `workers` to boot in clustered mode. 19 | # Workers are forked webserver processes. If using threads and workers together 20 | # the concurrency of the application would be max `threads` * `workers`. 21 | # Workers do not work on JRuby or Windows (both of which do not support 22 | # processes). 23 | # 24 | # workers ENV.fetch("WEB_CONCURRENCY") { 2 } 25 | 26 | # Use the `preload_app!` method when specifying a `workers` number. 27 | # This directive tells Puma to first boot the application and load code 28 | # before forking the application. This takes advantage of Copy On Write 29 | # process behavior so workers use less memory. If you use this option 30 | # you need to make sure to reconnect any threads in the `on_worker_boot` 31 | # block. 32 | # 33 | # preload_app! 34 | 35 | # The code in the `on_worker_boot` will be called if you are using 36 | # clustered mode by specifying a number of `workers`. After each worker 37 | # process is booted this block will be run, if you are using `preload_app!` 38 | # option you will want to use this block to reconnect to any threads 39 | # or connections that may have been created at application boot, Ruby 40 | # cannot share connections between processes. 41 | # 42 | # on_worker_boot do 43 | # ActiveRecord::Base.establish_connection if defined?(ActiveRecord) 44 | # end 45 | 46 | # Allow puma to be restarted by `rails restart` command. 47 | plugin :tmp_restart 48 | -------------------------------------------------------------------------------- /config/redis/cable.yml: -------------------------------------------------------------------------------- 1 | # Action Cable uses Redis to administer connections, channels, and sending/receiving messages over the WebSocket. 2 | production: 3 | url: redis://localhost:6379/1 4 | 5 | development: 6 | url: redis://localhost:6379/2 7 | 8 | test: 9 | url: redis://localhost:6379/3 10 | -------------------------------------------------------------------------------- /config/routes.rb: -------------------------------------------------------------------------------- 1 | require 'api_constraints.rb' 2 | 3 | Rails.application.routes.draw do 4 | # For details on the DSL available within this file, 5 | # see http://guides.rubyonrails.org/routing.html 6 | 7 | # Serve websocket cable requests in-process 8 | # mount ActionCable.server => '/cable' 9 | 10 | # Api definition 11 | scope module: :api, defaults: { format: :json } do 12 | scope module: :v1, constraints: ApiConstraints.new(version: 1, 13 | default: true) do 14 | get 'versions/state' => 'versions#state' 15 | post 'users/login' => 'sessions#create' 16 | delete 'users/logout' => 'sessions#destroy' 17 | post 'users/reset_password' => 'users#reset_password' 18 | resources :users, only: [:create, :destroy] 19 | resources :notes 20 | end 21 | end 22 | 23 | get 'users/confirm/:token', to: 'users#confirm', as: 'users_confirm' 24 | get 'users/confirm_reset/:token', to: 'users#confirm_reset', 25 | as: 'users_confirm_reset' 26 | get 'privacy', to: 'pages#privacy' 27 | get 'terms', to: 'pages#terms' 28 | end 29 | -------------------------------------------------------------------------------- /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 `rails 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: c4f8f0968292f2e7b15b02321546d13cb0a5f51e0d6d55019457fe0e46706b94109c0ac8ff94dd891168fe0546addbaedf4d761f52a2313c85f82a5d7fb4e80c 15 | 16 | test: 17 | secret_key_base: 35f7f595c6fc7a8568a361e4e23baf7952b912f8507c25f2ffd22349f2aeacf91f1bd0759726d339ddce5fbfd603c9f8ed3778ce700ab46a9b3cfc0fde7e642f 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/20160112191931_create_users.rb: -------------------------------------------------------------------------------- 1 | class CreateUsers < ActiveRecord::Migration[5.0] 2 | def change 3 | create_table(:users) do |t| 4 | t.string :email 5 | 6 | t.string :password_digest 7 | t.string :auth_token 8 | 9 | t.string :confirmation_token 10 | t.datetime :confirmation_sent_at 11 | t.datetime :confirmed_at 12 | 13 | t.string :reset_password_digest 14 | t.string :reset_password_token 15 | t.datetime :reset_password_sent_at 16 | 17 | t.timestamps 18 | end 19 | 20 | add_index :users, :email, unique: true 21 | add_index :users, :auth_token, unique: true 22 | add_index :users, :confirmation_token, unique: true 23 | add_index :users, :reset_password_token, unique: true 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /db/migrate/20160122193254_create_notes.rb: -------------------------------------------------------------------------------- 1 | class CreateNotes < ActiveRecord::Migration[5.0] 2 | def change 3 | create_table :notes do |t| 4 | t.string :title 5 | t.text :content 6 | t.belongs_to :user, index: true, foreign_key: true 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: 20160122193254) do 15 | 16 | # These are extensions that must be enabled in order to support this database 17 | enable_extension "plpgsql" 18 | 19 | create_table "notes", force: :cascade do |t| 20 | t.string "title" 21 | t.text "content" 22 | t.integer "user_id" 23 | t.datetime "created_at", null: false 24 | t.datetime "updated_at", null: false 25 | end 26 | 27 | add_index "notes", ["user_id"], name: "index_notes_on_user_id", using: :btree 28 | 29 | create_table "users", force: :cascade do |t| 30 | t.string "email" 31 | t.string "password_digest" 32 | t.string "auth_token" 33 | t.string "confirmation_token" 34 | t.datetime "confirmation_sent_at" 35 | t.datetime "confirmed_at" 36 | t.string "reset_password_digest" 37 | t.string "reset_password_token" 38 | t.datetime "reset_password_sent_at" 39 | t.datetime "created_at", null: false 40 | t.datetime "updated_at", null: false 41 | end 42 | 43 | add_index "users", ["auth_token"], name: "index_users_on_auth_token", unique: true, using: :btree 44 | add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree 45 | add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree 46 | add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree 47 | 48 | add_foreign_key "notes", "users" 49 | end 50 | -------------------------------------------------------------------------------- /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 rails db:seed (or created alongside the db with db:setup). 3 | # 4 | # Examples: 5 | # 6 | # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) 7 | # Character.create(name: 'Luke', movie: movies.first) 8 | -------------------------------------------------------------------------------- /lib/api_constraints.rb: -------------------------------------------------------------------------------- 1 | class ApiConstraints 2 | def initialize(options) 3 | @version = options[:version] 4 | @default = options[:default] 5 | end 6 | 7 | def matches?(req) 8 | @default || 9 | (req.respond_to?('headers') && 10 | req.headers.key?('Accept') && 11 | req.headers['Accept'].eql?( 12 | "application/vnd.railsapibase.v#{@version}")) 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /lib/tasks/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jordifierro/rails-api-base/2bd3084454045d7a417bc8580396bed68027edf9/lib/tasks/.keep -------------------------------------------------------------------------------- /log/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jordifierro/rails-api-base/2bd3084454045d7a417bc8580396bed68027edf9/log/.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 |