-
Notifications
You must be signed in to change notification settings - Fork 7
/
endpoints.rb
109 lines (97 loc) · 2.3 KB
/
endpoints.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# frozen_string_literal: true
module Starter
module Templates
# defining the endpoints -> http methods of a resource
module Endpoints
def crud
%i[
post
get_all
get_specific
put_specific
patch_specific
delete_specific
]
end
def singular_one
%i[
post
get_one
put_one
patch_one
delete_one
]
end
# available API/HTTP methods
# POST
def post
"
desc 'create #{resource.singularize}'
params do
# TODO: specify the parameters
end
post do
# your code goes here
end"
end
# GET
def get_all
"
desc 'get all of #{resource.pluralize}',
is_array: true
get do
# your code goes here
end"
end
%w[get put patch delete].each do |verb|
define_method(:"#{verb}_one") do
"
desc '#{verb} #{resource.singularize}'
#{verb} do
# your code goes here
end"
end
end
%w[get put patch delete].each do |verb|
define_method(:"#{verb}_specific") do
"
desc '#{verb} specific #{resource.singularize}'
params do
requires :id
end
#{verb} ':id' do
# your code goes here
end"
end
end
# request specs shared examples
#
def post_spec
"it_behaves_like 'POST', params: {}"
end
def get_all_spec
"it_behaves_like 'GET all'"
end
%w[get delete].each do |verb|
define_method(:"#{verb}_one_spec") do
"it_behaves_like '#{verb.upcase} one'"
end
end
%w[put patch].each do |verb|
define_method(:"#{verb}_one_spec") do
"it_behaves_like '#{verb.upcase} one', params: {}"
end
end
%w[get delete].each do |verb|
define_method(:"#{verb}_specific_spec") do
"it_behaves_like '#{verb.upcase} specific', key: 1"
end
end
%w[put patch].each do |verb|
define_method(:"#{verb}_specific_spec") do
"it_behaves_like '#{verb.upcase} specific', key: 1, params: {}"
end
end
end
end
end