Permalink
Browse files

Fix #find_definition! to return one definition, not an array.

  • Loading branch information...
1 parent 9d03c84 commit 5148e9e842d53b61630c8030f1389a57ef5f3373 @myronmarston myronmarston committed Oct 18, 2012
Showing with 24 additions and 8 deletions.
  1. +1 −1 lib/interpol/configuration.rb
  2. +9 −4 lib/interpol/endpoint.rb
  3. +4 −0 lib/interpol/errors.rb
  4. +10 −3 spec/unit/interpol/endpoint_spec.rb
@@ -20,7 +20,7 @@ def find_definition(method, path, message_type, status_code = nil)
private
def find_definitions_for(endpoint, version, message_type)
- endpoint.find_definition(version, message_type) { [] }
+ endpoint.find_definitions(version, message_type) { [] }
end
def with_endpoint_matching(method, path)
View
@@ -52,23 +52,28 @@ def initialize(endpoint_hash)
end
def find_definition!(version, message_type)
- find_definition(version, message_type) do
+ defs = find_definitions(version, message_type) do
message = "No definition found for #{name} endpoint for version #{version}"
message << " and message_type #{message_type}"
raise NoEndpointDefinitionFoundError.new(message)
end
+
+ return defs.first if defs.size == 1
+
+ raise MultipleEndpointDefinitionsFoundError, "#{defs.size} endpoint definitions " +
+ "were found for #{name} / #{version} / #{message_type}"
end
- def find_definition(version, message_type, &block)
+ def find_definitions(version, message_type, &block)
@definitions_hash.fetch([message_type, version], &block)
end
def find_example_for!(version, message_type)
- find_definition!(version, message_type).first.examples.first
+ find_definition!(version, message_type).examples.first
end
def find_example_status_code_for!(version)
- find_definition!(version, 'response').first.example_status_code
+ find_definition!(version, 'response').example_status_code
end
def available_request_versions
View
@@ -32,6 +32,10 @@ def initialize(errors = [], data = nil, endpoint_description = '')
# endpoint definition for the request.
class NoEndpointDefinitionFoundError < Error; end
+ # Error raised when multiple endpoint definitions are found
+ # for a given criteria.
+ class MultipleEndpointDefinitionsFoundError < Error; end
+
# Raised when an invalid status code is found during validate_codes!
class StatusCodeMatcherArgumentError < ArgumentError; end
end
@@ -101,16 +101,23 @@ def build_hash(hash = {})
let(:endpoint) { Endpoint.new(hash) }
it 'finds the definition matching the given version and message_type' do
- definitions = endpoint.find_definition!('1.2', 'response')
- definitions.first.version.should eq('1.2')
- definitions.first.message_type.should eq('response')
+ definition = endpoint.find_definition!('1.2', 'response')
+ definition.version.should eq('1.2')
+ definition.message_type.should eq('response')
end
it 'raises an error when given a version that matches no definition' do
expect {
endpoint.find_definition!('2.1', 'response')
}.to raise_error(NoEndpointDefinitionFoundError)
end
+
+ it 'raises an error if multiple definitions match its parameters' do
+ hash['definitions'] += definitions_array
+ expect {
+ endpoint.find_definition!('1.2', 'response')
+ }.to raise_error(MultipleEndpointDefinitionsFoundError)
+ end
end
describe "#find_example_for!" do

0 comments on commit 5148e9e

Please sign in to comment.