From 79ad34fd7d424e1bd6738fa7c6ecd31b54c86218 Mon Sep 17 00:00:00 2001 From: Bertrand Mutangana Ishimwe Date: Tue, 7 Jun 2022 19:21:56 +0200 Subject: [PATCH 1/7] setup model and controllers --- Gemfile | 3 ++ Gemfile.lock | 9 ++++ .../api/v1/greetings_controller.rb | 7 +++ app/models/greeting.rb | 2 + app/serializers/greeting_serializer.rb | 3 ++ config/database.yml | 19 ++------ config/routes.rb | 9 ++-- db/migrate/20220607165059_create_greetings.rb | 9 ++++ db/schema.rb | 23 ++++++++++ db/seeds.rb | 43 ++++++++++++++++--- 10 files changed, 100 insertions(+), 27 deletions(-) create mode 100644 app/controllers/api/v1/greetings_controller.rb create mode 100644 app/models/greeting.rb create mode 100644 app/serializers/greeting_serializer.rb create mode 100644 db/migrate/20220607165059_create_greetings.rb create mode 100644 db/schema.rb diff --git a/Gemfile b/Gemfile index 20158d8..c5a6d6e 100644 --- a/Gemfile +++ b/Gemfile @@ -15,6 +15,9 @@ gem "puma", "~> 5.0" # Build JSON APIs with ease [https://github.com/rails/jbuilder] # gem "jbuilder" +#Serialize data to JSON easily with Active Model Serializers +gem 'active_model_serializers' + # Use Redis adapter to run Action Cable in production # gem "redis", "~> 4.0" diff --git a/Gemfile.lock b/Gemfile.lock index 71a5845..10dcc69 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -46,6 +46,11 @@ GEM erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.1, >= 1.2.0) + active_model_serializers (0.10.13) + actionpack (>= 4.1, < 7.1) + activemodel (>= 4.1, < 7.1) + case_transform (>= 0.2) + jsonapi-renderer (>= 0.1.1.beta1, < 0.3) activejob (7.0.3) activesupport (= 7.0.3) globalid (>= 0.3.6) @@ -69,6 +74,8 @@ GEM bootsnap (1.12.0) msgpack (~> 1.2) builder (3.2.4) + case_transform (0.2) + activesupport concurrent-ruby (1.1.10) crass (1.0.6) debug (1.5.0) @@ -83,6 +90,7 @@ GEM io-console (0.5.11) irb (1.4.1) reline (>= 0.3.0) + jsonapi-renderer (0.2.2) loofah (2.18.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) @@ -162,6 +170,7 @@ PLATFORMS x64-mingw-ucrt DEPENDENCIES + active_model_serializers bootsnap debug pg (~> 1.1) diff --git a/app/controllers/api/v1/greetings_controller.rb b/app/controllers/api/v1/greetings_controller.rb new file mode 100644 index 0000000..c267108 --- /dev/null +++ b/app/controllers/api/v1/greetings_controller.rb @@ -0,0 +1,7 @@ +class Api::V1::GreetingsController < ApplicationController + def index + random_id = rand 1..Greeting.count + @greeting = Greeting.find(random_id) + render json: { status: 'SUCCESS', message: 'rendered successfully', data: @greeting }, status: :ok # 200 + end +end diff --git a/app/models/greeting.rb b/app/models/greeting.rb new file mode 100644 index 0000000..830c3aa --- /dev/null +++ b/app/models/greeting.rb @@ -0,0 +1,2 @@ +class Greeting < ApplicationRecord +end diff --git a/app/serializers/greeting_serializer.rb b/app/serializers/greeting_serializer.rb new file mode 100644 index 0000000..6b1a37b --- /dev/null +++ b/app/serializers/greeting_serializer.rb @@ -0,0 +1,3 @@ +class GreetingSerializer < ActiveModel::Serializer + attributes :id, :text +end diff --git a/config/database.yml b/config/database.yml index cf1326a..d492c3e 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,25 +1,12 @@ -# PostgreSQL. Versions 9.3 and up are supported. -# -# Install the pg driver: -# gem install pg -# On macOS with Homebrew: -# gem install pg -- --with-pg-config=/usr/local/bin/pg_config -# On macOS with MacPorts: -# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config -# On Windows: -# gem install pg -# Choose the win32 build. -# Install PostgreSQL and put its /bin directory on your path. -# -# Configure Using Gemfile -# gem "pg" -# + default: &default adapter: postgresql encoding: unicode # For details on connection pooling, see Rails configuration guide # https://guides.rubyonrails.org/configuring.html#database-pooling pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> + username: postgres + password: admin development: <<: *default diff --git a/config/routes.rb b/config/routes.rb index 262ffd5..a09a2ba 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,7 @@ Rails.application.routes.draw do - # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html - - # Defines the root path route ("/") - # root "articles#index" + namespace :api do + namespace :v1 do + resources :greetings, only: [:index] + end + end end diff --git a/db/migrate/20220607165059_create_greetings.rb b/db/migrate/20220607165059_create_greetings.rb new file mode 100644 index 0000000..13c57e2 --- /dev/null +++ b/db/migrate/20220607165059_create_greetings.rb @@ -0,0 +1,9 @@ +class CreateGreetings < ActiveRecord::Migration[7.0] + def change + create_table :greetings do |t| + t.string :text + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..71ea730 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,23 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema[7.0].define(version: 2022_06_07_165059) do + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + + create_table "greetings", force: :cascade do |t| + t.string "text" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end diff --git a/db/seeds.rb b/db/seeds.rb index bc25fce..4db0afa 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,7 +1,36 @@ -# This file should contain all the record creation needed to seed the database with its default values. -# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup). -# -# Examples: -# -# movies = Movie.create([{ name: "Star Wars" }, { name: "Lord of the Rings" }]) -# Character.create(name: "Luke", movie: movies.first) +greetings = [ + { + text: 'Good mornig' + }, + { + text: 'Salut!' + }, + { + text: 'Bonjour!' + }, + { + text: 'Muraho!' + }, + { + text: 'Hujambo!' + }, + { + text: 'Guten Morgen!' + }, + { + text: 'Bongiorno!' + }, + { + text: 'Dia dius!' + }, + { + text: 'Salve' + }, + { + text: 'Hello' + } +] + +greetings.each do |greeting| + Greeting.create(text: greeting[:text]) +end \ No newline at end of file From 9534898a074a656acfe6c7874a928a0ae1b74959 Mon Sep 17 00:00:00 2001 From: Bertrand Mutangana Ishimwe Date: Tue, 7 Jun 2022 19:32:42 +0200 Subject: [PATCH 2/7] correct linter errors --- .github/workflows/linters.yml | 22 +++++++ .rubocop.yml | 60 +++++++++++++++++++ Gemfile | 19 +++--- .../api/v1/greetings_controller.rb | 10 ++-- app/mailers/application_mailer.rb | 4 +- config.ru | 2 +- 6 files changed, 99 insertions(+), 18 deletions(-) create mode 100644 .github/workflows/linters.yml create mode 100644 .rubocop.yml diff --git a/.github/workflows/linters.yml b/.github/workflows/linters.yml new file mode 100644 index 0000000..fbd9f76 --- /dev/null +++ b/.github/workflows/linters.yml @@ -0,0 +1,22 @@ +name: Linters + +on: pull_request + +env: + FORCE_COLOR: 1 + +jobs: + rubocop: + name: Rubocop + runs-on: ubuntu-18.04 + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-ruby@v1 + with: + ruby-version: 3.0.x + - name: Setup Rubocop + run: | + gem install --no-document rubocop -v '>= 1.0, < 2.0' # https://docs.rubocop.org/en/stable/installation/ + [ -f .rubocop.yml ] || wget https://raw.githubusercontent.com/microverseinc/linters-config/master/ror/.rubocop.yml + - name: Rubocop Report + run: rubocop --color diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..8f14e4c --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,60 @@ +AllCops: + NewCops: enable + Exclude: + - "db/**/*" + - "bin/*" + - "config/**/*" + - "Guardfile" + - "Rakefile" + - "node_modules/**/*" + + DisplayCopNames: true + +Layout/LineLength: + Max: 120 +Metrics/MethodLength: + Include: + - "app/controllers/*" + - "app/models/*" + Max: 20 +Metrics/AbcSize: + Include: + - "app/controllers/*" + - "app/models/*" + Max: 50 +Metrics/ClassLength: + Max: 150 +Metrics/BlockLength: + IgnoredMethods: ['describe'] + Max: 30 + +Style/Documentation: + Enabled: false +Style/ClassAndModuleChildren: + Enabled: false +Style/EachForSimpleLoop: + Enabled: false +Style/AndOr: + Enabled: false +Style/DefWithParentheses: + Enabled: false +Style/FrozenStringLiteralComment: + EnforcedStyle: never + +Layout/HashAlignment: + EnforcedColonStyle: key +Layout/ExtraSpacing: + AllowForAlignment: false +Layout/MultilineMethodCallIndentation: + Enabled: true + EnforcedStyle: indented +Lint/RaiseException: + Enabled: false +Lint/StructNewOverride: + Enabled: false +Style/HashEachMethods: + Enabled: false +Style/HashTransformKeys: + Enabled: false +Style/HashTransformValues: + Enabled: false diff --git a/Gemfile b/Gemfile index c5a6d6e..3cfaa6f 100644 --- a/Gemfile +++ b/Gemfile @@ -1,21 +1,21 @@ -source "https://rubygems.org" +source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } -ruby "3.1.1" +ruby '3.1.1' # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" -gem "rails", "~> 7.0.3" +gem 'rails', '~> 7.0.3' # Use postgresql as the database for Active Record -gem "pg", "~> 1.1" +gem 'pg', '~> 1.1' # Use the Puma web server [https://github.com/puma/puma] -gem "puma", "~> 5.0" +gem 'puma', '~> 5.0' # Build JSON APIs with ease [https://github.com/rails/jbuilder] # gem "jbuilder" -#Serialize data to JSON easily with Active Model Serializers +# Serialize data to JSON easily with Active Model Serializers gem 'active_model_serializers' # Use Redis adapter to run Action Cable in production @@ -28,10 +28,10 @@ gem 'active_model_serializers' # gem "bcrypt", "~> 3.1.7" # Windows does not include zoneinfo files, so bundle the tzinfo-data gem -gem "tzinfo-data" +gem 'tzinfo-data' # Reduces boot times through caching; required in config/boot.rb -gem "bootsnap", require: false +gem 'bootsnap', require: false # Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images] # gem "image_processing", "~> 1.2" @@ -41,11 +41,10 @@ gem "bootsnap", require: false group :development, :test do # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem - gem "debug", platforms: %i[ mri mingw x64_mingw ] + gem 'debug', platforms: %i[mri mingw x64_mingw] end group :development do # Speed up commands on slow machines / big apps [https://github.com/rails/spring] # gem "spring" end - diff --git a/app/controllers/api/v1/greetings_controller.rb b/app/controllers/api/v1/greetings_controller.rb index c267108..8ea3f97 100644 --- a/app/controllers/api/v1/greetings_controller.rb +++ b/app/controllers/api/v1/greetings_controller.rb @@ -1,7 +1,7 @@ class Api::V1::GreetingsController < ApplicationController - def index - random_id = rand 1..Greeting.count - @greeting = Greeting.find(random_id) - render json: { status: 'SUCCESS', message: 'rendered successfully', data: @greeting }, status: :ok # 200 - end + def index + random_id = rand 1..Greeting.count + @greeting = Greeting.find(random_id) + render json: { status: 'SUCCESS', message: 'rendered successfully', data: @greeting }, status: :ok # 200 + end end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 3c34c81..286b223 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -1,4 +1,4 @@ class ApplicationMailer < ActionMailer::Base - default from: "from@example.com" - layout "mailer" + default from: 'from@example.com' + layout 'mailer' end diff --git a/config.ru b/config.ru index 4a3c09a..ad1fbf2 100644 --- a/config.ru +++ b/config.ru @@ -1,6 +1,6 @@ # This file is used by Rack-based servers to start the application. -require_relative "config/environment" +require_relative 'config/environment' run Rails.application Rails.application.load_server From 847d8e27a5e0b6b7f15f165f41360525a671e89b Mon Sep 17 00:00:00 2001 From: Bertrand Mutangana Ishimwe Date: Wed, 8 Jun 2022 15:02:45 +0200 Subject: [PATCH 3/7] avoid cors issues --- config/initializers/cors.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/config/initializers/cors.rb b/config/initializers/cors.rb index e5a82f1..2f576ea 100644 --- a/config/initializers/cors.rb +++ b/config/initializers/cors.rb @@ -5,12 +5,12 @@ # Read more: https://github.com/cyu/rack-cors -# Rails.application.config.middleware.insert_before 0, Rack::Cors do -# allow do -# origins "example.com" -# -# resource "*", -# headers: :any, -# methods: [:get, :post, :put, :patch, :delete, :options, :head] -# end -# end +Rails.application.config.middleware.insert_before 0, Rack::Cors do + allow do + origins "example.com" + + resource "*", + headers: :any, + methods: [:get, :post, :put, :patch, :delete, :options, :head] + end +end From 6763c4f6fe5f8ab70093ab3f0bc5bf1793fd3654 Mon Sep 17 00:00:00 2001 From: Bertrand Mutangana Ishimwe Date: Wed, 8 Jun 2022 15:15:09 +0200 Subject: [PATCH 4/7] added cors --- Gemfile | 3 +++ Gemfile.lock | 3 +++ config/initializers/cors.rb | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 3cfaa6f..39b6a54 100644 --- a/Gemfile +++ b/Gemfile @@ -18,6 +18,9 @@ gem 'puma', '~> 5.0' # Serialize data to JSON easily with Active Model Serializers gem 'active_model_serializers' +# rack-cors gem for CORS support +gem 'rack-cors' + # Use Redis adapter to run Action Cable in production # gem "redis", "~> 4.0" diff --git a/Gemfile.lock b/Gemfile.lock index 10dcc69..4546ec8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -123,6 +123,8 @@ GEM nio4r (~> 2.0) racc (1.6.0) rack (2.2.3.1) + rack-cors (1.1.1) + rack (>= 2.0.0) rack-test (1.1.0) rack (>= 1.0, < 3) rails (7.0.3) @@ -175,6 +177,7 @@ DEPENDENCIES debug pg (~> 1.1) puma (~> 5.0) + rack-cors rails (~> 7.0.3) tzinfo-data diff --git a/config/initializers/cors.rb b/config/initializers/cors.rb index 2f576ea..38fdfd8 100644 --- a/config/initializers/cors.rb +++ b/config/initializers/cors.rb @@ -7,7 +7,7 @@ Rails.application.config.middleware.insert_before 0, Rack::Cors do allow do - origins "example.com" + origins "*" resource "*", headers: :any, From 88a2b60a78b9422c4705102942c949dc6561a60b Mon Sep 17 00:00:00 2001 From: Bertrand Mutangana Ishimwe Date: Wed, 8 Jun 2022 16:29:08 +0200 Subject: [PATCH 5/7] add readme --- README.md | 107 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 94 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 7db80e4..d8796a7 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,105 @@ -# README +![](https://img.shields.io/badge/Hello-blue) -This README would normally document whatever steps are necessary to get the -application up and running. +# Hello-rails-react -Things you may want to cover: +> This is the hello api. Built with Rails -* Ruby version +# Built with +- Ruby +- Ruby on Rails +- Rubocop +- Gems + - rack-cors -* System dependencies +# Deployed on Heroku + [https://chello-frontend.herokuapp.com/](https://chello-frontend.herokuapp.com/) + +## Endpoints +- /api/v1/greetings -* Configuration -* Database creation +# Get Started +Follow these steps below: -* Database initialization +## Pre-requisites -* How to run the test suite +```bash +- Ruby +- Rails +- PostgreSQL +``` -* Services (job queues, cache servers, search engines, etc.) +## Setup +Clone the repo and run bundle install in the command line to install the dependencies -* Deployment instructions +```bash +$ git clone https://github.com/BertrandConxy/hello-rails-backend.git +$ cd hello-rails-backend +``` -* ... +## Install gems with: + +```bash +$ bundle install +``` + +## Install style linters with +```bash +$ npm install +``` + +## Play with the code +``` +rails c +``` + +## Populate the db with dummy data +``` +rails db:migrate +rails db:seed +``` + +## Run linters +``` +rubocop -A +``` + +## Start webpacker dev server to compile assets +``` +./bin/webpack-dev-server +``` + +## Start rails server (puma) + +```bash +$ rails server +``` + +Open [http://localhost:3000/](http://localhost:3000/) in your browser + + +## Author + +👤 **Bertrand Mutangana Ishimwe** + +- GitHub: [@BertrandConxy](https://github.com/BertrandConxy) +- Twitter: [@Bconxy](https://twitter.com/BertrandMutanga) +- LinkedIn: [Bertrand Mutangana Ishimwe](https://www.linkedin.com/in/bertrandmutangana) + +## 🤝 Contributing + +Contributions, issues, and feature requests are welcome! + +Feel free to check the [issues page](../../issues/). + +## Show your support + +Give a ⭐️ if you like this project! + +## Acknowledgments +- Many thanks to Microverse +- Thanks to everyone whose ideas were used to achieve this project + +## 📝 License + +This project is [MIT](./MIT.md) licensed. From 91f68ad8616f6e55aff4c2b33df03217d9d9ef6e Mon Sep 17 00:00:00 2001 From: Bertrand Mutangana Ishimwe Date: Wed, 8 Jun 2022 16:37:54 +0200 Subject: [PATCH 6/7] update readme --- README.md | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d8796a7..1a21ca6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ![](https://img.shields.io/badge/Hello-blue) -# Hello-rails-react +# Hello-rails-backend > This is the hello api. Built with Rails @@ -13,7 +13,7 @@ # Deployed on Heroku [https://chello-frontend.herokuapp.com/](https://chello-frontend.herokuapp.com/) - + ## Endpoints - /api/v1/greetings @@ -64,11 +64,6 @@ rails db:seed rubocop -A ``` -## Start webpacker dev server to compile assets -``` -./bin/webpack-dev-server -``` - ## Start rails server (puma) ```bash From 27e540c3af567b0de55aabf59fe15cc0a5745af8 Mon Sep 17 00:00:00 2001 From: Bertrand Mutangana Ishimwe Date: Wed, 8 Jun 2022 16:50:51 +0200 Subject: [PATCH 7/7] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 1a21ca6..8102fed 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,9 @@ # Deployed on Heroku [https://chello-frontend.herokuapp.com/](https://chello-frontend.herokuapp.com/) + +# Front-end repo +[Hello-react-frontend](https://github.com/BertrandConxy/hello-react-frontend) ## Endpoints - /api/v1/greetings