Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Expose the route from an endpoint definition.

We need this for the request param validator.
  • Loading branch information...
commit 77e4b6521d183de3880d1f778eb0fd3e9ec6b581 1 parent a5db32e
@myronmarston myronmarston authored
Showing with 30 additions and 16 deletions.
  1. +11 −4 lib/interpol/endpoint.rb
  2. +19 −12 spec/unit/interpol/endpoint_spec.rb
View
15 lib/interpol/endpoint.rb
@@ -1,5 +1,6 @@
require 'json-schema'
require 'interpol/errors'
+require 'forwardable'
module JSON
# The JSON-schema namespace
@@ -118,7 +119,7 @@ def extract_definitions_from(endpoint_hash)
fetch_from(definition, 'versions').each do |version|
message_type = definition.fetch('message_type', DEFAULT_MESSAGE_TYPE)
key = [message_type, version]
- endpoint_definition = EndpointDefinition.new(name, version, message_type, definition)
+ endpoint_definition = EndpointDefinition.new(self, version, message_type, definition)
definitions[key] << endpoint_definition
all_definitions << endpoint_definition
end
@@ -139,11 +140,13 @@ def validate_name!
# Provides the means to validate data against that version of the schema.
class EndpointDefinition
include HashFetcher
- attr_reader :endpoint_name, :message_type, :version, :schema,
+ attr_reader :endpoint, :message_type, :version, :schema,
:path_params, :query_params, :examples
+ extend Forwardable
+ def_delegators :endpoint, :route
- def initialize(endpoint_name, version, message_type, definition)
- @endpoint_name = endpoint_name
+ def initialize(endpoint, version, message_type, definition)
+ @endpoint = endpoint
@message_type = message_type
@status_codes = StatusCodeMatcher.new(definition['status_codes'])
@version = version
@@ -154,6 +157,10 @@ def initialize(endpoint_name, version, message_type, definition)
make_schema_strict!(@schema)
end
+ def endpoint_name
+ @endpoint.name
+ end
+
def validate_data!(data)
errors = ::JSON::Validator.fully_validate_schema(schema)
raise ValidationError.new(errors, nil, description) if errors.any?
View
31 spec/unit/interpol/endpoint_spec.rb
@@ -157,37 +157,44 @@ def build_hash(hash = {})
end
let(:version) { '1.0' }
+ let(:endpoint) { fire_double("Interpol::Endpoint").as_null_object }
- it 'initializes the endpoint_name' do
- endpoint_def = EndpointDefinition.new("e-name", version, 'response', build_hash)
- endpoint_def.endpoint_name.should eq("e-name")
+ it 'initializes the endpoint' do
+ endpoint_def = EndpointDefinition.new(endpoint, version, 'response', build_hash)
+ endpoint_def.endpoint.should be(endpoint)
+ end
+
+ it 'exposes the endpoint name' do
+ endpoint.stub(:name => 'e-name')
+ endpoint_def = EndpointDefinition.new(endpoint, version, 'response', build_hash)
+ endpoint_def.endpoint_name.should eq('e-name')
end
it 'initializes the version' do
- endpoint_def = EndpointDefinition.new("name", '2.3', 'response', build_hash)
+ endpoint_def = EndpointDefinition.new(endpoint, '2.3', 'response', build_hash)
endpoint_def.version.should eq('2.3')
end
it 'default initialized the message type' do
- endpoint_def = EndpointDefinition.new("name", '2.3', 'response', build_hash)
+ endpoint_def = EndpointDefinition.new(endpoint, '2.3', 'response', build_hash)
endpoint_def.message_type.should eq('response')
end
it 'initializes the message type' do
hash = build_hash('message_type' => 'request')
- endpoint_def = EndpointDefinition.new("name", '2.3', 'request', hash)
+ endpoint_def = EndpointDefinition.new(endpoint, '2.3', 'request', hash)
endpoint_def.message_type.should eq('request')
end
it 'initializes the example data' do
hash = build_hash('examples' => [{'a' => 5}])
- v = EndpointDefinition.new("name", version, 'response', hash)
+ v = EndpointDefinition.new(endpoint, version, 'response', hash)
v.examples.map(&:data).should eq([{ 'a' => 5 }])
end
it 'initializes the schema' do
hash = build_hash('schema' => {'the' => 'schema'})
- v = EndpointDefinition.new("name", version, 'response', hash)
+ v = EndpointDefinition.new(endpoint, version, 'response', hash)
v.schema['the'].should eq('schema')
end
@@ -195,14 +202,14 @@ def build_hash(hash = {})
it "raises an error if not initialized with '#{attr}'" do
hash = build_hash.reject { |k, v| k == attr }
expect {
- EndpointDefinition.new("name", version, 'response', hash)
+ EndpointDefinition.new(endpoint, version, 'response', hash)
}.to raise_error(/key not found.*#{attr}/)
end
end
%w[ path_params query_params ].each do |attr|
it "initializes #{attr} to an empty hash if no value is provided" do
- v = EndpointDefinition.new("name", version, 'response', build_hash)
+ v = EndpointDefinition.new(endpoint, version, 'response', build_hash)
v.send(attr).should eq({})
end
end
@@ -211,7 +218,7 @@ def build_hash(hash = {})
it "initializes #{attr} to the provided value" do
params = {'key' => 'param'}
hash = build_hash(attr => params)
- v = EndpointDefinition.new("name", version, 'response', hash)
+ v = EndpointDefinition.new(endpoint, version, 'response', hash)
v.send(attr).should eq(params)
end
end
@@ -223,7 +230,7 @@ def build_hash(hash = {})
} end
subject {
- EndpointDefinition.new("e-name", version, 'response', build_hash('schema' => schema))
+ EndpointDefinition.new(endpoint, version, 'response', build_hash('schema' => schema))
}
it 'raises a validation error when given data of the wrong type' do
Please sign in to comment.
Something went wrong with that request. Please try again.