Skip to content

Commit 82189e3

Browse files
author
Eugeny Khlopin
committed
Add apipie gem
1 parent e6962bd commit 82189e3

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ gem 'bootsnap', '>= 1.1.0', require: false
1111
gem 'search_object'
1212
gem 'search_object_graphql'
1313

14+
gem 'apipie-rails'
15+
1416
group :development, :test do
1517
gem 'faker'
1618
gem 'byebug', platforms: %i[mri mingw x64_mingw]

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ GEM
5050
annotate (2.7.4)
5151
activerecord (>= 3.2, < 6.0)
5252
rake (>= 10.4, < 13.0)
53+
apipie-rails (0.5.15)
54+
rails (>= 4.1)
5355
arel (9.0.0)
5456
ast (2.4.0)
5557
bootsnap (1.3.2)
@@ -212,6 +214,7 @@ PLATFORMS
212214
DEPENDENCIES
213215
active_model_serializers
214216
annotate
217+
apipie-rails
215218
bootsnap (>= 1.1.0)
216219
byebug
217220
database_cleaner

app/controllers/api/v1/books_controller.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,35 @@ class BooksController < ApplicationController
88

99
before_action :set_book, only: %i[show update destroy]
1010

11+
def_param_group :book do
12+
param :id, :number, desc: 'Id of book', required: true
13+
param 'book[title]', String, desc: 'Title of book', required: true, only_in: :request
14+
param 'book[description]', String, desc: 'Description of book', only_in: :request
15+
property :title, String, desc: 'Title of book'
16+
property :descriprion, String, desc: 'Description of book'
17+
property :created_at, String, desc: 'Date of book creation'
18+
property :updated_at, String, desc: 'Date of book update'
19+
end
20+
21+
api :GET, '/books/', 'Shows all books'
22+
returns array_of: :book, code: 200, desc: 'All books'
1123
def index
1224
books = Book.all
1325
render json: books
1426
end
1527

28+
api :GET, '/books/:id', 'Shows the requested book'
29+
param :id, :number, desc: 'Id of the book', required: true
30+
returns :book, code: 200, desc: 'Requested book'
31+
error code: 404, desc: 'Not Found'
1632
def show
1733
render json: @book
1834
end
1935

36+
api :POST, '/books/', 'Create a new book'
37+
returns :book, code: 200, desc: 'Created book'
38+
param_group :book
39+
2040
def create
2141
book = Book.new(book_params)
2242
if book.save
@@ -26,6 +46,10 @@ def create
2646
end
2747
end
2848

49+
api :PUT, '/books/:id', 'Updates the requested book'
50+
param_group :book
51+
returns :book, code: 200, desc: 'Updated book'
52+
error code: 404, desc: 'Not Found'
2953
def update
3054
if @book.update(book_params)
3155
render json: @book, status: :ok
@@ -34,6 +58,9 @@ def update
3458
end
3559
end
3660

61+
api :DELETE, '/books/:id', 'Deletes the requested book'
62+
returns code: 200
63+
param :id, :number, desc: 'Id of the book', required: true
3764
def destroy
3865
@book.destroy
3966
end

config/initializers/apipie.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Apipie.configure do |config|
2+
config.app_name = "Api Documentation"
3+
config.api_base_url = "/api/v1"
4+
config.doc_base_url = "/apipie"
5+
# where is your API defined?
6+
config.api_controllers_matcher = "#{Rails.root}/app/controllers/api/**/*.rb"
7+
config.translate = false
8+
end

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
Rails.application.routes.draw do
22

3+
apipie unless Rails.env.production?
4+
35
namespace :api do
46
namespace :v1 do
57
resources :authors

0 commit comments

Comments
 (0)