-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_reqres.rb
153 lines (139 loc) · 3.98 KB
/
example_reqres.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# This file is following the Request Hash specification:
# https://github.com/MarioRuiz/Request-Hash
# to get any value from this file directly from command line:
# ruby -r "./requests/example_reqres.rb" -e "p Requests::ExampleReqres.create_user._data"
# The responses included in this example are not mandatory to have them but
# they can be used to check if the WS is responding according to them
require "nice_hash"
module Requests
module ExampleReqres
# including example of responses in Ruby Hash object
def self.get_user(id)
{
path: "/api/users/#{id}",
responses: {
"200": {
message: "OK",
data: {
id: 2,
first_name: "Janet",
last_name: "Weaver",
avatar: "https://s3.amazonaws.com/uifaces/faces/twitter/josephstein/128.jpg",
},
},
"404": {
message: "Not Found",
},
},
}
end
# including example of response 200 in JSON string format
def self.list_users(page)
{
path: "/api/users?page=#{page}",
responses: {
'200': {
message: "OK",
data: '{
"page": 2,
"per_page": 3,
"total": 12,
"total_pages": 4,
"data": [
{
"id": 4,
"first_name": "Eve",
"last_name": "Holt",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"
},
{
"id": 5,
"first_name": "Charles",
"last_name": "Morris",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg"
},
{
"id": 6,
"first_name": "Tracey",
"last_name": "Ramos",
"avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg"
}
]
}',
},
},
}
end
# example of fixed post data, take in consideration instead, the examples on
# create_user_ex1 and create_user_ex2 request hashes
def self.create_user()
{
path: "/api/users",
data: {
name: "morpheus",
job: "leader",
},
responses: {
'201': {
message: "Created",
data: {
name: "morpheus",
job: "leader",
id: "440",
createdAt: "2019-02-08T13:27:08.740Z",
},
},
},
}
end
# to be used instead of create_user, example of
# post data using random patterns: nice_hash gem
def self.create_user_ex1()
{
path: "/api/users",
data: {
name: :"5-15:Ln", #from 5 to 15 letters and/or numbers
job: :"leader|developer|tester|accountant", #one of these
}
}
end
# to be used instead of create_user, example of
# post data using random patterns, defaults and wrongs: nice_hash gem
def self.create_user_ex2()
{
path: "/api/users",
data: {
name: {pattern: :"5-15:Ln", default: "morpheus", wrong: "^"},
job: :"leader|developer|tester|accountant",
}
}
end
def self.update_user(id)
{
path: "/api/users#{id}",
data: {
name: "morpheus",
job: "zion resident",
},
responses: {
'200': {
message: "Updated",
data: {
name: "morpheus",
job: "zion resident",
updatedAt: "2019-02-08T13:27:08.740Z",
},
},
},
}
end
def self.delete_user(id)
{
path: "/api/users/#{id}",
responses: {
'204': {message: "OK"},
},
}
end
end
end