Permalink
Browse files

Add support for custom metadata on endpoint definitions.

  • Loading branch information...
1 parent acd68a7 commit 4606974b47e18b8c4bc61e6abc21f56c7883dd66 @myronmarston myronmarston committed Jan 17, 2013
Showing with 33 additions and 17 deletions.
  1. +1 −0 Rakefile
  2. +10 −9 lib/interpol/endpoint.rb
  3. +22 −8 spec/unit/interpol/endpoint_spec.rb
View
@@ -18,6 +18,7 @@ if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'ruby' # MRI only
cane.abc_exclude = %w[
Interpol::StubApp::Builder#initialize
+ Interpol::EndpointDefinition#initialize
]
cane.style_exclude = %w[
View
@@ -153,21 +153,22 @@ def validate_name!
class EndpointDefinition
include HashFetcher
attr_reader :endpoint, :message_type, :version, :schema,
- :path_params, :query_params, :examples
+ :path_params, :query_params, :examples, :custom_metadata
extend Forwardable
def_delegators :endpoint, :route
DEFAULT_PARAM_HASH = { 'type' => 'object', 'properties' => {} }
def initialize(endpoint, version, message_type, definition)
- @endpoint = endpoint
- @message_type = message_type
- @status_codes = StatusCodeMatcher.new(definition['status_codes'])
- @version = version
- @schema = fetch_from(definition, 'schema')
- @path_params = definition.fetch('path_params', DEFAULT_PARAM_HASH.dup)
- @query_params = definition.fetch('query_params', DEFAULT_PARAM_HASH.dup)
- @examples = extract_examples_from(definition)
+ @endpoint = endpoint
+ @message_type = message_type
+ @status_codes = StatusCodeMatcher.new(definition['status_codes'])
+ @version = version
+ @schema = fetch_from(definition, 'schema')
+ @path_params = definition.fetch('path_params', DEFAULT_PARAM_HASH.dup)
+ @query_params = definition.fetch('query_params', DEFAULT_PARAM_HASH.dup)
+ @examples = extract_examples_from(definition)
+ @custom_metadata = definition.fetch('meta') { {} }
make_schema_strict!(@schema)
end
@@ -2,6 +2,19 @@
require 'interpol/endpoint'
module Interpol
+ shared_examples_for "custom_metadata" do
+ it "initializes custom_metadata from the meta field" do
+ instance = new_with('meta' => {'key' => 'value'})
+ expect(instance.custom_metadata).to eq('key' => 'value')
+ end
+
+ context "when no meta key is provided" do
+ it "initializes custom_metadata to an empty hash" do
+ expect(new_with({}).custom_metadata).to eq({})
+ end
+ end
+ end
+
describe Endpoint do
def build_hash(hash = {})
{
@@ -18,14 +31,9 @@ def build_hash(hash = {})
end
end
- it "initializes custom_metadata from the meta field" do
- endpoint = Endpoint.new(build_hash 'meta' => {'key' => 'value'})
- expect(endpoint.custom_metadata).to eq('key' => 'value')
- end
-
- context "when no meta key is provided" do
- it "initializes custom_metadata to an empty hash" do
- expect(Endpoint.new(build_hash).custom_metadata).to eq({})
+ it_behaves_like "custom_metadata" do
+ def new_with(hash)
+ Endpoint.new(build_hash hash)
end
end
@@ -258,6 +266,12 @@ def build_hash(hash = {})
end
end
+ it_behaves_like "custom_metadata" do
+ def new_with(hash)
+ EndpointDefinition.new(endpoint, version, 'response', build_hash(hash))
+ end
+ end
+
describe "#request?" do
it 'returns true if message_type == request' do
ed = EndpointDefinition.new(endpoint, version, 'request', build_hash)

0 comments on commit 4606974

Please sign in to comment.