Skip to content

Commit 35442eb

Browse files
author
Alexandro castro
committed
chore: change rubocop style
1 parent 2e7b48c commit 35442eb

22 files changed

Lines changed: 139 additions & 127 deletions

backend/.rubocop.yml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Omakase Ruby styling for Rails
2-
inherit_gem: { rubocop-rails-omakase: rubocop.yml }
2+
inherit_gem:
3+
rubocop-shopify: rubocop.yml
34

45
plugins:
56
- rubocop-erb
@@ -8,6 +9,8 @@ AllCops:
89
Exclude:
910
- 'db/schema.rb'
1011
- 'db/migrate/**/*'
12+
- 'Gemfile.lock'
13+
- 'bin/*'
1114

1215
Layout/IndentationStyle:
1316
EnforcedStyle: spaces
@@ -16,4 +19,13 @@ Layout/IndentationStyle:
1619

1720
Layout/IndentationWidth:
1821
Width: 2
19-
Enabled: true
22+
Enabled: true
23+
24+
Style/ClassMethodsDefinitions:
25+
Enabled: false
26+
27+
Style/ClassAndModuleChildren:
28+
Enabled: false
29+
30+
Style/FrozenStringLiteralComment:
31+
Enabled: false

backend/Gemfile

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ gem "stimulus-rails"
2222
# Build JSON APIs with ease [https://github.com/rails/jbuilder]
2323
gem "jbuilder"
2424
# Static analysis for security vulnerabilities [https://brakemanscanner.org/]
25-
gem "brakeman", require: false, group: [ :development, :test ]
25+
gem "brakeman", require: false, group: [:development, :test]
2626
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
2727
gem "image_processing", "~> 1.2"
2828
# Deploy this application anywhere as a Docker container [https://kamal-deploy.org]
@@ -50,16 +50,16 @@ gem "pg", "~> 1.5"
5050
# Code Quality
5151
# =================
5252
# Omakase Ruby styling [https://github.com/rails/rubocop-rails-omakase/]
53-
gem "rubocop-rails-omakase", require: false, group: [ :development, :test ]
54-
gem "rubocop-erb", require: false, group: [ :development, :test ]
55-
gem "rubocop-performance", require: false, group: [ :development, :test ]
53+
gem "rubocop-rails-omakase", require: false, group: [:development, :test]
54+
gem "rubocop-erb", require: false, group: [:development, :test]
55+
gem "rubocop-performance", require: false, group: [:development, :test]
5656

5757
# =================
5858
# Tools
5959
# =================
6060
gem "metainspector"
61-
gem "annotaterb", group: [ :development, :test ]
62-
gem "web-console", group: [ :development, :test ]
61+
gem "annotaterb", group: [:development, :test]
62+
gem "web-console", group: [:development, :test]
6363
gem "httparty"
6464
# Validation helpers
6565
gem "dry-validation"
@@ -86,4 +86,4 @@ gem "redis-objects"
8686
# =================
8787
gem "sentry-ruby"
8888
gem "sentry-rails"
89-
gem "stackprof"
89+
gem "stackprof"
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
class Api::Me::ShortlinksController < ApplicationController
2-
before_action :load_link, only: [ :destroy, :show ]
2+
before_action :load_link, only: [:destroy, :show]
33

44
# GET /api/me/shortlinks/:id
55
def show
6-
render json: ShortlinkSerializer.new(@link).serialize, status: :ok
6+
render(json: ShortlinkSerializer.new(@link).serialize, status: :ok)
77
end
88

99
# GET /api/me/shortlinks
1010
def index
11-
render json: ShortlinkSerializer.new(@current_user.shortlinks).serialize(meta: {
12-
total: @current_user.shortlinks.count
13-
}), status: :ok
11+
render(
12+
json: ShortlinkSerializer.new(@current_user.shortlinks).serialize(meta: {
13+
total: @current_user.shortlinks.count,
14+
}),
15+
status: :ok,
16+
)
1417
end
1518

1619
# POST /api/me/shortlinks
1720
def create
1821
validate_contract(ShortlinkContract) do |validated_params|
1922
link = @current_user.shortlinks.new(validated_params)
2023
if link.save
21-
render json: ShortlinkSerializer.new(link).serialize, status: :created
24+
render(json: ShortlinkSerializer.new(link).serialize, status: :created)
2225
else
23-
render json: { errors: link.errors.full_messages }, status: :unprocessable_entity
26+
render(json: { errors: link.errors.full_messages }, status: :unprocessable_entity)
2427
end
2528
end
2629
end
@@ -30,15 +33,15 @@ def destroy
3033
link = @current_user.shortlinks.find_by(id: params[:id])
3134
if link
3235
link.destroy
33-
head :no_content
36+
head(:no_content)
3437
else
35-
render json: { error: "Link not found" }, status: :not_found
38+
render(json: { error: "Link not found" }, status: :not_found)
3639
end
3740
end
3841

3942
private
4043

41-
def load_link
42-
@link = @current_user.shortlinks.find(params[:id])
43-
end
44+
def load_link
45+
@link = @current_user.shortlinks.find(params[:id])
46+
end
4447
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Api::Me::UsersController < ApplicationController
22
def show
3-
render json: UserSerializer.new(current_user).serialize, status: :ok
3+
render(json: UserSerializer.new(current_user).serialize, status: :ok)
44
end
55
end
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
class Api::SessionsController < ApplicationController
2-
skip_before_action :authenticate_user!, only: [ :create, :verify ]
2+
skip_before_action :authenticate_user!, only: [:create, :verify]
33

44
# POST /api/login_request
55
def create
66
if params[:email].present?
77
user = User.find_or_create_by(email: params[:email])
88
user.send_magic_link
99

10-
render json: { message: "If the email exists, the link has been sent." }
10+
render(json: { message: "If the email exists, the link has been sent." })
1111
else
12-
render json: { error: "Email is required" }, status: :unprocessable_entity
12+
render(json: { error: "Email is required" }, status: :unprocessable_entity)
1313
end
1414
end
1515

@@ -21,17 +21,16 @@ def verify
2121
token = generate_jwt(user)
2222
user.clear_login_token!
2323

24-
25-
render json: UserSerializer.new(user).serialize(meta: { token: token }), status: :ok
24+
render(json: UserSerializer.new(user).serialize(meta: { token: token }), status: :ok)
2625
else
27-
render json: { error: "Token invalid or expired" }, status: :unauthorized
26+
render(json: { error: "Token invalid or expired" }, status: :unauthorized)
2827
end
2928
end
3029

3130
private
3231

33-
def generate_jwt(user)
34-
payload = { sub: user.id, exp: 1.hour.from_now.to_i }
35-
JWT.encode(payload, Rails.application.secret_key_base, "HS256")
36-
end
32+
def generate_jwt(user)
33+
payload = { sub: user.id, exp: 1.hour.from_now.to_i }
34+
JWT.encode(payload, Rails.application.secret_key_base, "HS256")
35+
end
3736
end

backend/app/controllers/application_controller.rb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,35 @@ def validate_contract(contract_class, context: nil)
1111
# Contract validation
1212
contract = contract_class.new.call(params.to_unsafe_h, context: context)
1313
if contract.errors.any?
14-
render json: { errors: contract.errors.to_h }, status: :unprocessable_entity
15-
else
16-
yield contract.to_h, params if block_given?
14+
render(json: { errors: contract.errors.to_h }, status: :unprocessable_entity)
15+
elsif block_given?
16+
yield contract.to_h, params
1717
end
1818
end
1919

2020
private
2121

22-
def record_not_found(exception)
23-
render json: { message: "Resource not found" }, status: :not_found
24-
end
22+
def record_not_found(exception)
23+
render(json: { message: "Resource not found" }, status: :not_found)
24+
end
2525

2626
def record_not_destroyed(exception)
27-
render json: { message: "Resource could not be destroyed", details: exception.record.errors.full_messages }, status: :unprocessable_entity
27+
render(json: { message: "Resource could not be destroyed", details: exception.record.errors.full_messages }, status: :unprocessable_entity)
2828
end
2929

3030
def record_invalid(exception)
31-
render json: { message: "Resource is invalid", details: exception.record.errors.full_messages }, status: :unprocessable_entity
31+
render(json: { message: "Resource is invalid", details: exception.record.errors.full_messages }, status: :unprocessable_entity)
3232
end
3333

34-
3534
def authenticate_user!
3635
auth_header = request.headers["Authorization"]
37-
return render json: { message: "Missing token" }, status: :unauthorized unless auth_header
36+
return render(json: { message: "Missing token" }, status: :unauthorized) unless auth_header
3837

3938
token = auth_header.split(" ").last
4039
decoded = JWT.decode(token, Rails.application.secret_key_base, true, algorithm: "HS256")
4140
user_id = decoded.first["sub"]
4241
@current_user = User.find(user_id)
4342
rescue JWT::ExpiredSignature, JWT::DecodeError
44-
render json: { message: "Invalid or expired token" }, status: :unauthorized
43+
render(json: { message: "Invalid or expired token" }, status: :unauthorized)
4544
end
4645
end

backend/app/jobs/concerns/retryable_job.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ module RetryableJob
99

1010
private
1111

12-
def handle_retry(exception)
13-
retry_count = (arguments.last[:retry_count] || 0) + 1
14-
max_retries = 5
15-
wait_seconds = 2**retry_count
12+
def handle_retry(exception)
13+
retry_count = (arguments.last[:retry_count] || 0) + 1
14+
max_retries = 5
15+
wait_seconds = 2**retry_count
1616

17-
if retry_count <= max_retries
18-
Rails.logger.warn("Retrying #{self.class.name} (#{retry_count}/#{max_retries}) after #{wait_seconds}s due to #{exception.class}: #{exception.message}")
19-
self.class.set(wait: wait_seconds.seconds).perform_later(*arguments.first(1), retry_count: retry_count)
20-
else
21-
Rails.logger.error("#{self.class.name} failed permanently after #{max_retries} attempts")
22-
end
17+
if retry_count <= max_retries
18+
Rails.logger.warn("Retrying #{self.class.name} (#{retry_count}/#{max_retries}) after #{wait_seconds}s due to #{exception.class}: #{exception.message}")
19+
self.class.set(wait: wait_seconds.seconds).perform_later(*arguments.first(1), retry_count: retry_count)
20+
else
21+
Rails.logger.error("#{self.class.name} failed permanently after #{max_retries} attempts")
2322
end
23+
end
2424
end

backend/app/jobs/ipaddr_job.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class IpaddrJob < ApplicationJob
22
include RetryableJob
33
queue_as :default
4-
4+
55
def perform(event_id, retry_count: 3)
66
event = Event.find_by(id: event_id)
77
return if event.nil?
@@ -16,8 +16,7 @@ def perform(event_id, retry_count: 3)
1616
event.country_code = nil
1717
event.region = nil
1818
end
19-
19+
2020
event.save!
2121
end
22-
2322
end

backend/app/models/event.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ class Event < ApplicationRecord
4444

4545
private
4646

47-
def set_clicked_at
48-
self.clicked_at ||= Time.current
49-
end
47+
def set_clicked_at
48+
self.clicked_at ||= Time.current
49+
end
5050

5151
def update_last_visited
52-
self.shortlink.update(last_accessed_at: clicked_at)
52+
shortlink.update(last_accessed_at: clicked_at)
5353
end
5454

5555
def ipaddr_job
56-
IpaddrJob.perform_later(self.id)
56+
IpaddrJob.perform_later(id)
5757
end
5858
end

backend/app/models/shortlink.rb

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,41 +26,41 @@ class Shortlink < ApplicationRecord
2626
# Validations
2727
# ===============
2828
validates :original_url, presence: true
29-
validates :short_code, presence: true, uniqueness: true
30-
validates :user_id, presence: true
29+
validates :short_code, presence: true, uniqueness: true
30+
validates :user_id, presence: true
3131

32-
# ===============
33-
# Associations
34-
# ===============
35-
belongs_to :user
36-
has_many :events, dependent: :destroy
32+
# ===============
33+
# Associations
34+
# ===============
35+
belongs_to :user
36+
has_many :events, dependent: :destroy
3737

38-
# ===============
39-
# Callbacks
40-
# ===============
41-
after_commit :save_cache, on: :create
42-
before_validation :generate_short_code, on: :create
38+
# ===============
39+
# Callbacks
40+
# ===============
41+
after_commit :save_cache, on: :create
42+
before_validation :generate_short_code, on: :create
4343

44-
private
44+
private
4545

46-
def short_url
47-
"#{ENV['EDGE_API']}/#{short_code}"
48-
end
46+
def short_url
47+
"#{ENV["EDGE_API"]}/#{short_code}"
48+
end
4949

50-
def save_cache
51-
Rails.cache.redis.with do |conn|
52-
conn.set(cache_key, original_url)
53-
end
50+
def save_cache
51+
Rails.cache.redis.with do |conn|
52+
conn.set(cache_key, original_url)
5453
end
54+
end
5555

56-
def cache_key
57-
"shortlink:#{short_code}"
58-
end
56+
def cache_key
57+
"shortlink:#{short_code}"
58+
end
5959

60-
def generate_short_code
61-
self.short_code ||= loop do
62-
code = SecureRandom.alphanumeric(6)
63-
break code unless Shortlink.exists?(short_code: code)
64-
end
60+
def generate_short_code
61+
self.short_code ||= loop do
62+
code = SecureRandom.alphanumeric(6)
63+
break code unless Shortlink.exists?(short_code: code)
6564
end
65+
end
6666
end

0 commit comments

Comments
 (0)