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
|
# File 'lib/rspec-sane-http.rb', line 5
def self.extended(base)
base.class_eval do
def description
block = Proc.new do |metadata|
if metadata[:description].match(/^(GET|POST|PUT|DELETE|OPTIONS|PATCH) (.+)$/)
metadata[:description]
else
block.call(metadata[:parent_example_group])
end
end
block.call(self.class.metadata)
end
def request_method
self.description.split(' ').first
end
def request_path
self.description.split(' ').last.split('/').
map { |fragment| fragment.sub(/^:(.+)$/) { |match|
self.class.metadata[match[1..-1].to_sym]
} }.join('/')
end
let(:attrs) do
Hash.new
end
let(:instance) do
if request_method == 'POST'
factory
else
factory.save
puts "~ DB: #{factory.values}"
factory
end
end
let(:url) do
[RSpec.configuration.base_url, request_path].join('')
end
let(:response) do
if ['GET', 'DELETE'].include?(request_method)
= self.class.metadata[:headers]
request = HTTP.( || {})
log(request_method, request_path, )
request.send(request_method.downcase, url)
else
data = self.class.metadata[:data]
= self.class.metadata[:headers]
request = HTTP.( || {})
log(request_method, request_path, , data)
request.send(request_method.downcase, url, body: data)
end
end
def request(request_method, request_path = request_path, = {}, data = {})
url = [RSpec.configuration.base_url, request_path].join('')
request = HTTP.()
data = data.to_json unless data.is_a?(String) response = request.send(request_method.downcase, url, body: data)
log(request_method, request_path, , data)
JSON.parse(response.body.readpartial)
end
['POST', 'PUT'].each do |http_method|
define_method(http_method) do |request_path = request_path, = {}, body|
request(http_method, request_path, , body)
end
end
['GET', 'DELETE'].each do |http_method|
define_method(http_method) do |request_path = request_path, = {}|
request(http_method, request_path, , nil)
end
end
def log(request_method, request_path, , data = nil)
if $DEBUG
string = "~ #{request_method} #{request_path}"
string << " #{.inspect}" if && ! .empty?
string << " data: #{data.inspect}" if data
warn string
end
end
let(:response_data) do
data = JSON.parse(response).reduce(Hash.new) do |buffer, (key, value)|
buffer.merge(key.to_sym => value)
end
puts "Code: #{response.code}"
puts "Data: #{data.inspect}"
data
end
end
end
|