interpol 0.4.3 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +15 -0
- data/lib/interpol/configuration.rb +22 -1
- data/lib/interpol/endpoint.rb +6 -9
- data/lib/interpol/errors.rb +4 -0
- data/lib/interpol/stub_app.rb +12 -11
- data/lib/interpol/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -231,6 +231,21 @@ Interpol.default_configuration do |config|
|
|
231
231
|
example.data["current_url"] = Rack::Request.new(request_env).url
|
232
232
|
end
|
233
233
|
|
234
|
+
# Sets a callback that will be used to determine which example
|
235
|
+
# to return from the stub app. If you provide an endpoint name,
|
236
|
+
# the block will apply only to requests to that endpoint.
|
237
|
+
# If no name is provided, the block will set the default selector
|
238
|
+
# logic. By default, if this config is not set, interpol will use
|
239
|
+
# the first example.
|
240
|
+
#
|
241
|
+
# Used by Interpol::StubApp.
|
242
|
+
config.select_example_response('some-endpoint') do |endpoint_def, request_env|
|
243
|
+
endpoint_def.examples[3]
|
244
|
+
end
|
245
|
+
config.select_example_response do |endpoint_def, request_env|
|
246
|
+
endpoint_def.examples.first
|
247
|
+
end
|
248
|
+
|
234
249
|
# Determines what to do when Interpol::Sinatra::RequestParamsParser
|
235
250
|
# detects invalid path or query parameters based on their schema
|
236
251
|
# definitions. This block will be eval'd in the context of your
|
@@ -20,7 +20,7 @@ module Interpol
|
|
20
20
|
private
|
21
21
|
|
22
22
|
def find_definitions_for(endpoint, version, message_type)
|
23
|
-
endpoint.
|
23
|
+
endpoint.find_definitions(version, message_type) { [] }
|
24
24
|
end
|
25
25
|
|
26
26
|
def with_endpoint_matching(method, path)
|
@@ -141,6 +141,19 @@ module Interpol
|
|
141
141
|
filter_example_data_blocks << block
|
142
142
|
end
|
143
143
|
|
144
|
+
def select_example_response(endpoint_name = nil, &block)
|
145
|
+
if endpoint_name
|
146
|
+
named_example_selectors[endpoint_name] = block
|
147
|
+
else
|
148
|
+
named_example_selectors.default = block
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def example_response_for(endpoint_def, env)
|
153
|
+
selector = named_example_selectors[endpoint_def.endpoint_name]
|
154
|
+
selector.call(endpoint_def, env)
|
155
|
+
end
|
156
|
+
|
144
157
|
def self.default
|
145
158
|
@default ||= Configuration.new
|
146
159
|
end
|
@@ -187,6 +200,10 @@ module Interpol
|
|
187
200
|
'Content-Length' => json.bytesize }, [json]]
|
188
201
|
end
|
189
202
|
|
203
|
+
def named_example_selectors
|
204
|
+
@named_example_selectors ||= {}
|
205
|
+
end
|
206
|
+
|
190
207
|
def register_default_callbacks
|
191
208
|
request_version do
|
192
209
|
raise ConfigurationError, "request_version has not been configured"
|
@@ -229,6 +246,10 @@ module Interpol
|
|
229
246
|
on_invalid_sinatra_request_params do |error|
|
230
247
|
halt 400, JSON.dump(:error => error.message)
|
231
248
|
end
|
249
|
+
|
250
|
+
select_example_response do |endpoint_def, _|
|
251
|
+
endpoint_def.examples.first
|
252
|
+
end
|
232
253
|
end
|
233
254
|
end
|
234
255
|
end
|
data/lib/interpol/endpoint.rb
CHANGED
@@ -52,23 +52,20 @@ module Interpol
|
|
52
52
|
end
|
53
53
|
|
54
54
|
def find_definition!(version, message_type)
|
55
|
-
|
55
|
+
defs = find_definitions(version, message_type) do
|
56
56
|
message = "No definition found for #{name} endpoint for version #{version}"
|
57
57
|
message << " and message_type #{message_type}"
|
58
58
|
raise NoEndpointDefinitionFoundError.new(message)
|
59
59
|
end
|
60
|
-
end
|
61
60
|
|
62
|
-
|
63
|
-
@definitions_hash.fetch([message_type, version], &block)
|
64
|
-
end
|
61
|
+
return defs.first if defs.size == 1
|
65
62
|
|
66
|
-
|
67
|
-
|
63
|
+
raise MultipleEndpointDefinitionsFoundError, "#{defs.size} endpoint definitions " +
|
64
|
+
"were found for #{name} / #{version} / #{message_type}"
|
68
65
|
end
|
69
66
|
|
70
|
-
def
|
71
|
-
|
67
|
+
def find_definitions(version, message_type, &block)
|
68
|
+
@definitions_hash.fetch([message_type, version], &block)
|
72
69
|
end
|
73
70
|
|
74
71
|
def available_request_versions
|
data/lib/interpol/errors.rb
CHANGED
@@ -32,6 +32,10 @@ module Interpol
|
|
32
32
|
# endpoint definition for the request.
|
33
33
|
class NoEndpointDefinitionFoundError < Error; end
|
34
34
|
|
35
|
+
# Error raised when multiple endpoint definitions are found
|
36
|
+
# for a given criteria.
|
37
|
+
class MultipleEndpointDefinitionsFoundError < Error; end
|
38
|
+
|
35
39
|
# Raised when an invalid status code is found during validate_codes!
|
36
40
|
class StatusCodeMatcherArgumentError < ArgumentError; end
|
37
41
|
end
|
data/lib/interpol/stub_app.rb
CHANGED
@@ -44,24 +44,25 @@ module Interpol
|
|
44
44
|
|
45
45
|
def endpoint_definition(endpoint)
|
46
46
|
lambda do
|
47
|
-
|
48
|
-
|
49
|
-
example_and_version_for(endpoint, self)
|
47
|
+
endpoint_def = settings.stub_app_builder.endpoint_def_for(endpoint, self)
|
48
|
+
example = settings.stub_app_builder.example_for(endpoint_def, self)
|
50
49
|
example.validate! if settings.perform_validations?
|
51
|
-
status
|
50
|
+
status endpoint_def.example_status_code
|
52
51
|
JSON.dump(example.data)
|
53
52
|
end
|
54
53
|
end
|
55
54
|
|
56
|
-
def
|
55
|
+
def endpoint_def_for(endpoint, app)
|
57
56
|
version = config.response_version_for(app.request.env, endpoint)
|
58
|
-
|
57
|
+
endpoint_def = endpoint.find_definition!(version, 'response')
|
59
58
|
rescue NoEndpointDefinitionFoundError
|
60
|
-
config.sinatra_request_version_unavailable
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
59
|
+
config.sinatra_request_version_unavailable \
|
60
|
+
app, version, endpoint.available_response_versions
|
61
|
+
end
|
62
|
+
|
63
|
+
def example_for(endpoint_def, app)
|
64
|
+
example = config.example_response_for(endpoint_def, app.request.env)
|
65
|
+
example.apply_filters(config.filter_example_data_blocks, app.request.env)
|
65
66
|
end
|
66
67
|
end
|
67
68
|
end
|
data/lib/interpol/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: interpol
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-10-
|
12
|
+
date: 2012-10-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|