-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_path_format.rb
55 lines (48 loc) · 1.7 KB
/
api_path_format.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true
module RuboCop
module Cop
module Boxt
# Clients expect to interface with our API using kebab-case.
#
# This cop ensures that our API paths are formatted using the correct case.
#
# Underscores are acceptable in path variables (e.g. "/path/:path_id/update")
#
# API style guide: https://github.com/boxt/boxt-docs/blob/main/coding-styles/api.md#camel-cased-endpoints
#
# @example
# # bad
# post "/admin/orders/:order_id/contact_details/update"
# get "/installation_days"
# namespace "password_resets"
# namespace :password_resets
#
# # good
# post "/admin/orders/:order_id/contact-details/update"
# get "/installation-days"
# namespace "password-resets"
#
class ApiPathFormat < RuboCop::Cop::Base
def_node_matcher :path_defining_method_with_string_path, <<~PATTERN
(send nil? {:get | :post | :put | :patch | :delete | :namespace} (:str $_))
PATTERN
def_node_matcher :namespace_with_symbol, <<~PATTERN
(send nil? :namespace (:sym $_))
PATTERN
MSG = "Use kebab-case for the API path"
def on_send(node)
path_defining_method_with_string_path(node) do |path|
add_offense(node) if path_name_does_not_follow_kebab_case?(path)
end
namespace_with_symbol(node) do |path|
add_offense(node) if path.to_s.include?("_")
end
end
private
def path_name_does_not_follow_kebab_case?(path)
path.split("/").any? { |split| !split.start_with?(":") && split.include?("_") }
end
end
end
end
end