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 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.find_definition(version, message_type) { [] }
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
@@ -52,23 +52,20 @@ module Interpol
52
52
  end
53
53
 
54
54
  def find_definition!(version, message_type)
55
- find_definition(version, message_type) do
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
- def find_definition(version, message_type, &block)
63
- @definitions_hash.fetch([message_type, version], &block)
64
- end
61
+ return defs.first if defs.size == 1
65
62
 
66
- def find_example_for!(version, message_type)
67
- find_definition!(version, message_type).first.examples.first
63
+ raise MultipleEndpointDefinitionsFoundError, "#{defs.size} endpoint definitions " +
64
+ "were found for #{name} / #{version} / #{message_type}"
68
65
  end
69
66
 
70
- def find_example_status_code_for!(version)
71
- find_definition!(version, 'response').first.example_status_code
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
@@ -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
@@ -44,24 +44,25 @@ module Interpol
44
44
 
45
45
  def endpoint_definition(endpoint)
46
46
  lambda do
47
- example, version = settings.
48
- stub_app_builder.
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 endpoint.find_example_status_code_for!(version)
50
+ status endpoint_def.example_status_code
52
51
  JSON.dump(example.data)
53
52
  end
54
53
  end
55
54
 
56
- def example_and_version_for(endpoint, app)
55
+ def endpoint_def_for(endpoint, app)
57
56
  version = config.response_version_for(app.request.env, endpoint)
58
- example = endpoint.find_example_for!(version, 'response')
57
+ endpoint_def = endpoint.find_definition!(version, 'response')
59
58
  rescue NoEndpointDefinitionFoundError
60
- config.sinatra_request_version_unavailable(app, version,
61
- endpoint.available_response_versions)
62
- else
63
- example = example.apply_filters(config.filter_example_data_blocks, app.request.env)
64
- return example, version
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
@@ -1,3 +1,3 @@
1
1
  module Interpol
2
- VERSION = "0.4.3"
2
+ VERSION = "0.5.0"
3
3
  end
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.3
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-18 00:00:00.000000000 Z
12
+ date: 2012-10-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack