Class: Interpol::EndpointDefinition
- Inherits:
-
Object
- Object
- Interpol::EndpointDefinition
- Extended by:
- Forwardable
- Includes:
- HashFetcher
- Defined in:
- lib/interpol/endpoint.rb
Overview
Wraps a single versioned definition for an endpoint. Provides the means to validate data against that version of the schema.
Constant Summary collapse
- DEFAULT_PARAM_HASH =
{ 'type' => 'object', 'properties' => {} }
Instance Attribute Summary collapse
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#examples ⇒ Object
readonly
Returns the value of attribute examples.
-
#message_type ⇒ Object
readonly
Returns the value of attribute message_type.
-
#path_params ⇒ Object
readonly
Returns the value of attribute path_params.
-
#query_params ⇒ Object
readonly
Returns the value of attribute query_params.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
- #description ⇒ Object
- #endpoint_name ⇒ Object
- #example_status_code ⇒ Object
-
#initialize(endpoint, version, message_type, definition) ⇒ EndpointDefinition
constructor
A new instance of EndpointDefinition.
- #matches_status_code?(status_code) ⇒ Boolean
- #request? ⇒ Boolean
- #response? ⇒ Boolean
- #status_codes ⇒ Object
- #validate_data!(data) ⇒ Object
Methods included from HashFetcher
Constructor Details
#initialize(endpoint, version, message_type, definition) ⇒ EndpointDefinition
Returns a new instance of EndpointDefinition.
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/interpol/endpoint.rb', line 162 def initialize(endpoint, version, , definition) @endpoint = endpoint @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) make_schema_strict!(@schema) end |
Instance Attribute Details
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
155 156 157 |
# File 'lib/interpol/endpoint.rb', line 155 def endpoint @endpoint end |
#examples ⇒ Object (readonly)
Returns the value of attribute examples.
155 156 157 |
# File 'lib/interpol/endpoint.rb', line 155 def examples @examples end |
#message_type ⇒ Object (readonly)
Returns the value of attribute message_type.
155 156 157 |
# File 'lib/interpol/endpoint.rb', line 155 def @message_type end |
#path_params ⇒ Object (readonly)
Returns the value of attribute path_params.
155 156 157 |
# File 'lib/interpol/endpoint.rb', line 155 def path_params @path_params end |
#query_params ⇒ Object (readonly)
Returns the value of attribute query_params.
155 156 157 |
# File 'lib/interpol/endpoint.rb', line 155 def query_params @query_params end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
155 156 157 |
# File 'lib/interpol/endpoint.rb', line 155 def schema @schema end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
155 156 157 |
# File 'lib/interpol/endpoint.rb', line 155 def version @version end |
Instance Method Details
#description ⇒ Object
193 194 195 196 197 |
# File 'lib/interpol/endpoint.rb', line 193 def description subdescription = "#{} v. #{version}" subdescription << " for status: #{status_codes}" if == 'response' "#{endpoint_name} (#{subdescription})" end |
#endpoint_name ⇒ Object
182 183 184 |
# File 'lib/interpol/endpoint.rb', line 182 def endpoint_name @endpoint.name end |
#example_status_code ⇒ Object
207 208 209 |
# File 'lib/interpol/endpoint.rb', line 207 def example_status_code @example_status_code ||= @status_codes.example_status_code end |
#matches_status_code?(status_code) ⇒ Boolean
203 204 205 |
# File 'lib/interpol/endpoint.rb', line 203 def matches_status_code?(status_code) status_code.nil? || @status_codes.matches?(status_code) end |
#request? ⇒ Boolean
174 175 176 |
# File 'lib/interpol/endpoint.rb', line 174 def request? == "request" end |
#response? ⇒ Boolean
178 179 180 |
# File 'lib/interpol/endpoint.rb', line 178 def response? == "response" end |
#status_codes ⇒ Object
199 200 201 |
# File 'lib/interpol/endpoint.rb', line 199 def status_codes @status_codes.code_strings.join(',') end |
#validate_data!(data) ⇒ Object
186 187 188 189 190 191 |
# File 'lib/interpol/endpoint.rb', line 186 def validate_data!(data) errors = ::JSON::Validator.fully_validate_schema(schema) raise ValidationError.new(errors, nil, description) if errors.any? errors = ::JSON::Validator.fully_validate(schema, data) raise ValidationError.new(errors, data, description) if errors.any? end |