Module: RspecApiDocumentation::DSL::Endpoint
- Extended by:
- ActiveSupport::Concern
- Includes:
- Rack::Test::Utils
- Defined in:
- lib/rspec_api_documentation/dsl/endpoint.rb,
lib/rspec_api_documentation/dsl/endpoint/params.rb,
lib/rspec_api_documentation/dsl/endpoint/set_param.rb
Overview
DSL methods available inside the RSpec example.
Defined Under Namespace
Modules: ClassMethods
Classes: Params, SetParam
Constant Summary
collapse
- URL_PARAMS_REGEX =
/[:\{](\w+)\}?/.freeze
Instance Method Summary
collapse
Instance Method Details
#authentication(type, value, opts = {}) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 80
def authentication(type, value, opts = {})
name, new_opts =
case type
when :basic then ['Authorization', opts.merge(type: type)]
when :apiKey then [opts[:name], opts.merge(type: type, in: :header)]
else raise 'Not supported type for authentication'
end
(name, value)
example.metadata[:authentications] ||= {}
example.metadata[:authentications][name] = new_opts
end
|
#do_request(extra_params = {}) ⇒ Object
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
|
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 35
def do_request( = {})
@extra_params =
params_or_body = nil
path_or_query = path
extended_parameters
if http_method == :get && !query_string.blank?
path_or_query += "?#{query_string}"
else
if respond_to?(:raw_post)
params_or_body = raw_post
else
formatter = RspecApiDocumentation.configuration.request_body_formatter
case formatter
when :json
params_or_body = params.empty? ? nil : params.to_json
when :xml
params_or_body = params.to_xml
when Proc
params_or_body = formatter.call(params)
else
params_or_body = params
end
end
end
rspec_api_documentation_client.send(http_method, path_or_query, params_or_body, )
end
|
#example ⇒ Object
158
159
160
|
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 158
def example
RSpec.current_example
end
|
#explanation(text) ⇒ Object
154
155
156
|
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 154
def explanation(text)
example.metadata[:explanation] = text
end
|
#extended_parameters ⇒ Object
106
107
108
|
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 106
def extended_parameters
example.metadata[:extended_parameters] ||= Params.new(self, example, ).extended
end
|
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 92
def
example.metadata[:route].gsub(URL_PARAMS_REGEX) do |match|
value =
if .keys.include?($1)
[$1]
elsif respond_to?($1)
send($1)
else
match
end
extended_parameters << {name: match[1..-1], value: value, in: :path}
end
end
|
75
76
77
78
|
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 75
def (name, value)
example.metadata[:headers] ||= {}
example.metadata[:headers][name] = value
end
|
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 110
def
return unless example.metadata[:headers]
example.metadata[:headers].inject({}) do |hash, (, value)|
if value.is_a?(Symbol)
hash[] = send(value) if respond_to?(value)
else
hash[] = value
end
hash
end
end
|
#http_method ⇒ Object
122
123
124
|
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 122
def http_method
example.metadata[:method]
end
|
#in_path?(param) ⇒ Boolean
134
135
136
|
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 134
def in_path?(param)
path_params.include?(param)
end
|
#method ⇒ Object
126
127
128
|
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 126
def method
http_method
end
|
#params ⇒ Object
71
72
73
|
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 71
def params
Params.new(self, example, ).call
end
|
#path ⇒ Object
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 142
def path
example.metadata[:route].gsub(URL_PARAMS_REGEX) do |match|
if .keys.include?($1)
($1)
elsif respond_to?($1)
escape send($1)
else
escape match
end
end
end
|
#path_params ⇒ Object
138
139
140
|
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 138
def path_params
example.metadata[:route].scan(URL_PARAMS_REGEX).flatten
end
|
#query_string ⇒ Object
67
68
69
|
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 67
def query_string
build_nested_query(params || {})
end
|
#status ⇒ Object
130
131
132
|
# File 'lib/rspec_api_documentation/dsl/endpoint.rb', line 130
def status
rspec_api_documentation_client.status
end
|