Skip to content

Commit bd527e2

Browse files
committed
- Added byebug, minitest, mocha and simplecov gems;
- Setup minitest and simplecov; - Added dummy app; - Added and using en and pt_BR I18n translations; - Created RailsFakeApi::ApplicationController; - Handling errors; - Created essential tests.
1 parent 6e991a5 commit bd527e2

75 files changed

Lines changed: 2051 additions & 27 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ source "https://rubygems.org"
55
gemspec
66

77
gem "rake", "~> 13.0"
8+
gem "byebug"
89
gem "test-unit", "~> 3.0"
910
gem "rubocop", "~> 1.21"

Gemfile.lock

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
PATH
2+
remote: .
3+
specs:
4+
rails_fake_api (0.1.0)
5+
rails (>= 8.0.0)
6+
7+
GEM
8+
remote: https://rubygems.org/
9+
specs:
10+
actioncable (8.0.2)
11+
actionpack (= 8.0.2)
12+
activesupport (= 8.0.2)
13+
nio4r (~> 2.0)
14+
websocket-driver (>= 0.6.1)
15+
zeitwerk (~> 2.6)
16+
actionmailbox (8.0.2)
17+
actionpack (= 8.0.2)
18+
activejob (= 8.0.2)
19+
activerecord (= 8.0.2)
20+
activestorage (= 8.0.2)
21+
activesupport (= 8.0.2)
22+
mail (>= 2.8.0)
23+
actionmailer (8.0.2)
24+
actionpack (= 8.0.2)
25+
actionview (= 8.0.2)
26+
activejob (= 8.0.2)
27+
activesupport (= 8.0.2)
28+
mail (>= 2.8.0)
29+
rails-dom-testing (~> 2.2)
30+
actionpack (8.0.2)
31+
actionview (= 8.0.2)
32+
activesupport (= 8.0.2)
33+
nokogiri (>= 1.8.5)
34+
rack (>= 2.2.4)
35+
rack-session (>= 1.0.1)
36+
rack-test (>= 0.6.3)
37+
rails-dom-testing (~> 2.2)
38+
rails-html-sanitizer (~> 1.6)
39+
useragent (~> 0.16)
40+
actiontext (8.0.2)
41+
actionpack (= 8.0.2)
42+
activerecord (= 8.0.2)
43+
activestorage (= 8.0.2)
44+
activesupport (= 8.0.2)
45+
globalid (>= 0.6.0)
46+
nokogiri (>= 1.8.5)
47+
actionview (8.0.2)
48+
activesupport (= 8.0.2)
49+
builder (~> 3.1)
50+
erubi (~> 1.11)
51+
rails-dom-testing (~> 2.2)
52+
rails-html-sanitizer (~> 1.6)
53+
activejob (8.0.2)
54+
activesupport (= 8.0.2)
55+
globalid (>= 0.3.6)
56+
activemodel (8.0.2)
57+
activesupport (= 8.0.2)
58+
activerecord (8.0.2)
59+
activemodel (= 8.0.2)
60+
activesupport (= 8.0.2)
61+
timeout (>= 0.4.0)
62+
activestorage (8.0.2)
63+
actionpack (= 8.0.2)
64+
activejob (= 8.0.2)
65+
activerecord (= 8.0.2)
66+
activesupport (= 8.0.2)
67+
marcel (~> 1.0)
68+
activesupport (8.0.2)
69+
base64
70+
benchmark (>= 0.3)
71+
bigdecimal
72+
concurrent-ruby (~> 1.0, >= 1.3.1)
73+
connection_pool (>= 2.2.5)
74+
drb
75+
i18n (>= 1.6, < 2)
76+
logger (>= 1.4.2)
77+
minitest (>= 5.1)
78+
securerandom (>= 0.3)
79+
tzinfo (~> 2.0, >= 2.0.5)
80+
uri (>= 0.13.1)
81+
ast (2.4.3)
82+
base64 (0.2.0)
83+
benchmark (0.4.0)
84+
bigdecimal (3.2.0)
85+
builder (3.3.0)
86+
byebug (12.0.0)
87+
concurrent-ruby (1.3.5)
88+
connection_pool (2.5.3)
89+
crass (1.0.6)
90+
date (3.4.1)
91+
docile (1.4.1)
92+
drb (2.2.3)
93+
erb (5.0.1)
94+
erubi (1.13.1)
95+
globalid (1.2.1)
96+
activesupport (>= 6.1)
97+
i18n (1.14.7)
98+
concurrent-ruby (~> 1.0)
99+
io-console (0.8.0)
100+
irb (1.15.2)
101+
pp (>= 0.6.0)
102+
rdoc (>= 4.0.0)
103+
reline (>= 0.4.2)
104+
json (2.12.2)
105+
language_server-protocol (3.17.0.5)
106+
lint_roller (1.1.0)
107+
logger (1.7.0)
108+
loofah (2.24.1)
109+
crass (~> 1.0.2)
110+
nokogiri (>= 1.12.0)
111+
mail (2.8.1)
112+
mini_mime (>= 0.1.1)
113+
net-imap
114+
net-pop
115+
net-smtp
116+
marcel (1.0.4)
117+
mini_mime (1.1.5)
118+
minitest (5.25.5)
119+
mocha (2.7.1)
120+
ruby2_keywords (>= 0.0.5)
121+
net-imap (0.5.8)
122+
date
123+
net-protocol
124+
net-pop (0.1.2)
125+
net-protocol
126+
net-protocol (0.2.2)
127+
timeout
128+
net-smtp (0.5.1)
129+
net-protocol
130+
nio4r (2.7.4)
131+
nokogiri (1.18.8-aarch64-linux-gnu)
132+
racc (~> 1.4)
133+
nokogiri (1.18.8-aarch64-linux-musl)
134+
racc (~> 1.4)
135+
nokogiri (1.18.8-arm-linux-gnu)
136+
racc (~> 1.4)
137+
nokogiri (1.18.8-arm-linux-musl)
138+
racc (~> 1.4)
139+
nokogiri (1.18.8-arm64-darwin)
140+
racc (~> 1.4)
141+
nokogiri (1.18.8-x86_64-darwin)
142+
racc (~> 1.4)
143+
nokogiri (1.18.8-x86_64-linux-gnu)
144+
racc (~> 1.4)
145+
nokogiri (1.18.8-x86_64-linux-musl)
146+
racc (~> 1.4)
147+
parallel (1.27.0)
148+
parser (3.3.8.0)
149+
ast (~> 2.4.1)
150+
racc
151+
power_assert (2.0.5)
152+
pp (0.6.2)
153+
prettyprint
154+
prettyprint (0.2.0)
155+
prism (1.4.0)
156+
psych (5.2.6)
157+
date
158+
stringio
159+
racc (1.8.1)
160+
rack (3.1.15)
161+
rack-session (2.1.1)
162+
base64 (>= 0.1.0)
163+
rack (>= 3.0.0)
164+
rack-test (2.2.0)
165+
rack (>= 1.3)
166+
rackup (2.2.1)
167+
rack (>= 3)
168+
rails (8.0.2)
169+
actioncable (= 8.0.2)
170+
actionmailbox (= 8.0.2)
171+
actionmailer (= 8.0.2)
172+
actionpack (= 8.0.2)
173+
actiontext (= 8.0.2)
174+
actionview (= 8.0.2)
175+
activejob (= 8.0.2)
176+
activemodel (= 8.0.2)
177+
activerecord (= 8.0.2)
178+
activestorage (= 8.0.2)
179+
activesupport (= 8.0.2)
180+
bundler (>= 1.15.0)
181+
railties (= 8.0.2)
182+
rails-dom-testing (2.3.0)
183+
activesupport (>= 5.0.0)
184+
minitest
185+
nokogiri (>= 1.6)
186+
rails-html-sanitizer (1.6.2)
187+
loofah (~> 2.21)
188+
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
189+
railties (8.0.2)
190+
actionpack (= 8.0.2)
191+
activesupport (= 8.0.2)
192+
irb (~> 1.13)
193+
rackup (>= 1.0.0)
194+
rake (>= 12.2)
195+
thor (~> 1.0, >= 1.2.2)
196+
zeitwerk (~> 2.6)
197+
rainbow (3.1.1)
198+
rake (13.2.1)
199+
rdoc (6.14.0)
200+
erb
201+
psych (>= 4.0.0)
202+
regexp_parser (2.10.0)
203+
reline (0.6.1)
204+
io-console (~> 0.5)
205+
rubocop (1.75.8)
206+
json (~> 2.3)
207+
language_server-protocol (~> 3.17.0.2)
208+
lint_roller (~> 1.1.0)
209+
parallel (~> 1.10)
210+
parser (>= 3.3.0.2)
211+
rainbow (>= 2.2.2, < 4.0)
212+
regexp_parser (>= 2.9.3, < 3.0)
213+
rubocop-ast (>= 1.44.0, < 2.0)
214+
ruby-progressbar (~> 1.7)
215+
unicode-display_width (>= 2.4.0, < 4.0)
216+
rubocop-ast (1.44.1)
217+
parser (>= 3.3.7.2)
218+
prism (~> 1.4)
219+
ruby-progressbar (1.13.0)
220+
ruby2_keywords (0.0.5)
221+
securerandom (0.4.1)
222+
simplecov (0.22.0)
223+
docile (~> 1.1)
224+
simplecov-html (~> 0.11)
225+
simplecov_json_formatter (~> 0.1)
226+
simplecov-html (0.13.1)
227+
simplecov_json_formatter (0.1.4)
228+
stringio (3.1.7)
229+
test-unit (3.6.8)
230+
power_assert
231+
thor (1.3.2)
232+
timeout (0.4.3)
233+
tzinfo (2.0.6)
234+
concurrent-ruby (~> 1.0)
235+
unicode-display_width (3.1.4)
236+
unicode-emoji (~> 4.0, >= 4.0.4)
237+
unicode-emoji (4.0.4)
238+
uri (1.0.3)
239+
useragent (0.16.11)
240+
websocket-driver (0.8.0)
241+
base64
242+
websocket-extensions (>= 0.1.0)
243+
websocket-extensions (0.1.5)
244+
zeitwerk (2.7.3)
245+
246+
PLATFORMS
247+
aarch64-linux-gnu
248+
aarch64-linux-musl
249+
arm-linux-gnu
250+
arm-linux-musl
251+
arm64-darwin
252+
x86_64-darwin
253+
x86_64-linux-gnu
254+
x86_64-linux-musl
255+
256+
DEPENDENCIES
257+
bundler (~> 2.0)
258+
byebug
259+
minitest
260+
mocha (~> 2.0)
261+
rails_fake_api!
262+
rake (~> 13.0)
263+
rubocop (~> 1.21)
264+
simplecov (~> 0.22)
265+
test-unit (~> 3.0)
266+
267+
BUNDLED WITH
268+
2.6.7
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module RailsFakeApi
2+
class ApplicationController < ActionController::API
3+
skip_before_action :verify_authenticity_token, raise: false
4+
5+
rescue_from StandardError, with: :internal_server_error
6+
rescue_from RailsFakeApi::Errors::ResourceNotFound, with: :resource_not_found_error
7+
8+
private
9+
10+
def resource_not_found_error(exception)
11+
render json: { error: exception.message }, status: :not_found
12+
end
13+
14+
def internal_server_error(exception)
15+
Rails.logger.error "Internal Server Error: #{exception.message}\n#{exception.backtrace.join("\n")}"
16+
render json: { error: I18n.t('rails_fake_api.errors.internal_server_error') }, status: :internal_server_error
17+
end
18+
end
19+
end

app/controllers/rails_fake_api/fake_api_controller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module RailsFakeApi
22
class FakeApiController < ApplicationController
3-
skip_before_action :verify_authenticity_token
3+
skip_before_action :verify_authenticity_token, raise: false
44

55
before_action :set_resource_name
66
before_action :set_resource_data, only: [:show, :update, :destroy]
@@ -15,7 +15,7 @@ def show
1515
if @resource_data
1616
render json: @resource_data
1717
else
18-
render json: { error: "Resource not found" }, status: :not_found
18+
render json: { error: I18n.t('rails_fake_api.errors.resource_not_found') }, status: :not_found
1919
end
2020
end
2121

@@ -44,7 +44,7 @@ def update
4444
JsonFileStore.write(@resource_name, data)
4545
render json: @resource_data
4646
else
47-
render json: { error: "Resource not found" }, status: :not_found
47+
render json: { error: I18n.t('rails_fake_api.errors.resource_not_found') }, status: :not_found
4848
end
4949
end
5050

@@ -56,7 +56,7 @@ def destroy
5656
JsonFileStore.write(@resource_name, data)
5757
head :no_content
5858
else
59-
render json: { error: "Resource not found" }, status: :not_found
59+
render json: { error: I18n.t('rails_fake_api.errors.resource_not_found') }, status: :not_found
6060
end
6161
end
6262

config/locales/en.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
en:
2+
rails_fake_api:
3+
errors:
4+
resource_not_found: "Resource not found."
5+
internal_server_error: "Internal server error."

config/locales/pt-BR.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pt-BR:
2+
rails_fake_api:
3+
errors:
4+
resource_not_found: "Recurso não encontrado."
5+
internal_server_error: "Ocorreu um erro interno no servidor."

lib/rails_fake_api.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# frozen_string_literal: true
22

3-
require_relative "rails_fake_api/version"
4-
require_relative "rails_fake_api/engine"
5-
require_relative "rails_fake_api/json_file_store"
3+
require "rails_fake_api/version"
4+
require "rails_fake_api/engine"
5+
require "rails_fake_api/json_file_store"
6+
require "rails_fake_api/errors"
67

78
module RailsFakeApi
8-
class Error < StandardError; end
99
end

lib/rails_fake_api/engine.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ class Engine < ::Rails::Engine
77
app.config.autoload_paths += %W(#{config.root}/app/controllers)
88
end
99

10+
initializer "rails_fake_api.add_locales" do |app|
11+
app.config.i18n.load_path += Dir[config.root.join('config', 'locales', '**', '*.{rb,yml}')]
12+
# app.config.i18n.default_locale = :"pt-BR"
13+
end
14+
1015
config.generators.api_only = true
1116
end
1217
end

lib/rails_fake_api/errors.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module RailsFakeApi
2+
module Errors
3+
class ResourceNotFound < StandardError
4+
end
5+
end
6+
end

rails_fake_api.gemspec

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ Gem::Specification.new do |spec|
4141
# Dependências de desenvolvimento (opcional, mas bom ter)
4242
spec.add_development_dependency "bundler", "~> 2.0"
4343
spec.add_development_dependency "rake", "~> 13.0"
44+
spec.add_development_dependency "byebug"
45+
spec.add_development_dependency "minitest"
46+
spec.add_development_dependency "mocha", "~> 2.0"
47+
spec.add_development_dependency "simplecov", "~> 0.22"
4448

4549
# For more information and examples about making a new gem, check out our
4650
# guide at: https://bundler.io/guides/creating_gem.html

0 commit comments

Comments
 (0)