Skip to content

Commit 5fee08c

Browse files
authored
Merge pull request #20 from tuanle03/clr-76-create-swagger-ui-with-basic-authenticate
[CLR-76] Create Swagger UI and Basic Authenticate
2 parents 4c5afaa + abe8dc8 commit 5fee08c

File tree

10 files changed

+132
-1
lines changed

10 files changed

+132
-1
lines changed

app/api/web/feedbacks_api.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module API
22
class Web::FeedbacksAPI < Grape::API
3+
34
resource :feedbacks do
5+
46
desc 'Create feedback'
57
params do
68
requires :content, type: String, allow_blank: false

app/api/web/posts_api.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,34 @@ def format_post(post)
221221
end
222222
}
223223
end
224+
225+
desc 'Create a feedback for a post'
226+
params do
227+
requires :id, type: Integer, desc: 'Post id'
228+
requires :content, type: String, desc: 'Feedback content', allow_blank: false
229+
end
230+
post '/:id/feedbacks' do
231+
authenticate_user!
232+
post = Post.find_by(id: params[:id])
233+
return status 400 unless post
234+
feedback = post.feedbacks.new({
235+
user_id: current_user.id,
236+
content: params[:content]
237+
})
238+
if feedback.save
239+
status 200
240+
{
241+
success: true,
242+
message: 'Feedback created successfully'
243+
}
244+
else
245+
status 400
246+
{
247+
success: false,
248+
error: 'Feedback cannot be created'
249+
}
250+
end
251+
end
224252
end
225253
end
226254
end

app/controllers/docs_controller.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class DocsController < ApplicationController
2+
3+
http_basic_authenticate_with(
4+
name: Rails.application.credentials.dig(:basic_auth, :api_username),
5+
password: Rails.application.credentials.dig(:basic_auth, :api_password),
6+
only: :index,
7+
)
8+
9+
def index
10+
end
11+
end

app/helpers/docs_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module DocsHelper
2+
end

app/views/docs/index.html.erb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<title>Docs</title>
6+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.1.1/swagger-ui.css">
7+
</head>
8+
9+
<body>
10+
<div id="ui-wrapper-new" data-spec="/api/web/doc.json">
11+
Loading....
12+
</div>
13+
</body>
14+
<script src="https://unpkg.com/swagger-ui-dist@5.1.1/swagger-ui-bundle.js"></script>
15+
<script>
16+
var swaggerUIOptions = {
17+
url: '/api/web/doc.json',
18+
dom_id: '#ui-wrapper-new',
19+
docExpansion: 'list',
20+
deepLinking: true,
21+
filter: true,
22+
presets: [
23+
SwaggerUIBundle.presets.apis,
24+
SwaggerUIBundle.SwaggerUIStandalonePreset
25+
],
26+
plugins: [
27+
SwaggerUIBundle.plugins.DownloadUrl
28+
],
29+
requestInterceptor: function (request) {
30+
request.headers["Cache-Control"] = "no-cache, no-store, must-revalidate";
31+
request.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
32+
return request;
33+
},
34+
}
35+
36+
var ui = SwaggerUIBundle(swaggerUIOptions)
37+
38+
window.ui = ui
39+
</script>
40+
41+
</html>

config/credentials.yml.enc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
bnMbcO9jI+ZVwANkEbRXicq4F3Cz65ANfWLuPoCiCdkq0aRrgJY9ytGg2F+eFBbFBcSIUXiUsapE+1O8FO8SG3uk/6OPT5PYpvd4oZrnVUG71bkQY8+gz3VWhHm6L9U6CdQCViktRGkdIbTyX5vQWEjZQXD1gzeQpYrWgOFJDux8DlqQCvWSTzwmIdSC2i5tZjZ0ViidDZ6ac1LOvQniFjtXlV2TXwsPIUbwVEXVoVajKRu7IS3qb77kXPfxuvS8wYh/xg6Z75w6hQ/3OxgTeYDKeqVfvAxDULmMAqx32/Rug14hb5PXp/MODk0Su/ddz/GCYRtIZrDwmwTnyHppCdPMGZhpVRFfInCjlRaHSZw0BreIwUU1GHDr9hiLq4sMm7MhDgWp1CW/28bg2kfETiq97ln2R0ssNlt7j4r/KnRlq4hzwfJ3ZPxVCRC2Fx4kx3vMotZ/0QVBUqJH/dA=--gM9qn9Lvnfpo8C0L--k5jFAP7f8FpGe/K0HRa0wg==
1+
+UfHNzDJjgk1PaWIDGPPY6M/uzF4xQ0NvaAq1X3LbXdqPbVYf0u2VjDQOnGodaUhDiMBWlFA2q3y90MNhljuQACKpZglwu5txQGkbUCJrGnLieqHtgWtdzUQtiAX04jFTJloMGUvaWtKyfc1WqzhivSqzKbdyuOu7IckdgqPOng0BMGbcKdFJvMsqjsYFYEqbkzZ/Bsgz2FSg/OOCIW3E3x02OXoH/Uzqm/oDaUIIQVLyOGmX6g0P4a37uVS71AOy9n4WgD8lLy81zBmCcGWd+FUjC42Z2CRnVNknM4SQlLk4TsUwbSXbVNVriZr0XR3eRx9x3YzoVi9zPVO5eNKR+6YIp7j2A9Hf6bbSAdzvkhYdjO3Dmxsxsv85Pf5INTUB3zrAH30n47FouzI9RsQgMhPrO1QaZYyRBQvzzx3IPQnKOgbRxAm8hLS6VYYezRltSpORgdYTuPHGbHujExbVe2jJr7mKo8riql1dbwNI14jg/VcPK+9Egn5zaAgYv1wSJyUI/L8DOATgb2zCeDZLKBWjgPCU3IsVivofuEuU/OJ--F9U6OQqHz18UMU+f--Q2VZpp4pgMFNs1QqdJAa3g==

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44

55
# Defines the root path route ("/")
66
# root "articles#index"
7+
get '/docs', to: 'docs#index'
8+
79
mount ApplicationAPI => '/api'
810
end

spec/api/web/posts_api_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,29 @@ def app
228228
end
229229
end
230230
end
231+
232+
describe 'POST /web/posts/:id/feedbacks' do
233+
before do
234+
create(:post, status: 'approved', user: user)
235+
end
236+
237+
context 'with valid parameters' do
238+
it 'creates a feedback' do
239+
post "/web/posts/1/feedbacks", { content: 'Lorem ipsum' }, 'HTTP_AUTHORIZATION' => "Bearer #{token}"
240+
241+
expect(last_response.status).to eq(200)
242+
json = JSON.parse(last_response.body)
243+
expect(json['success']).to eq(true)
244+
expect(json['message']).to eq('Feedback created successfully')
245+
end
246+
end
247+
248+
context 'with invalid parameters' do
249+
it 'returns an error message' do
250+
post "/web/posts/1/feedbacks", { content: '' }, 'HTTP_AUTHORIZATION' => "Bearer #{token}"
251+
252+
expect(last_response.status).to eq(400)
253+
end
254+
end
255+
end
231256
end

spec/helpers/docs_helper_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'rails_helper'
2+
3+
# Specs in this file have access to a helper object that includes
4+
# the DocsHelper. For example:
5+
#
6+
# describe DocsHelper do
7+
# describe "string concat" do
8+
# it "concats two strings with spaces" do
9+
# expect(helper.concat_strings("this","that")).to eq("this that")
10+
# end
11+
# end
12+
# end
13+
RSpec.describe DocsHelper, type: :helper do
14+
end

spec/requests/docs_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe "Docs", type: :request do
4+
describe "GET /index" do
5+
end
6+
end

0 commit comments

Comments
 (0)