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
-
#custom_metadata ⇒ Object
readonly
Returns the value of attribute custom_metadata.
-
#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, validate_schema = true) ⇒ Object
Methods included from HashFetcher
Constructor Details
#initialize(endpoint, version, message_type, definition) ⇒ EndpointDefinition
Returns a new instance of EndpointDefinition.
193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/interpol/endpoint.rb', line 193 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) @custom_metadata = definition.fetch('meta') { {} } make_schema_strict!(@schema) end |
Instance Attribute Details
#custom_metadata ⇒ Object (readonly)
Returns the value of attribute custom_metadata.
186 187 188 |
# File 'lib/interpol/endpoint.rb', line 186 def @custom_metadata end |
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
186 187 188 |
# File 'lib/interpol/endpoint.rb', line 186 def endpoint @endpoint end |
#examples ⇒ Object (readonly)
Returns the value of attribute examples.
186 187 188 |
# File 'lib/interpol/endpoint.rb', line 186 def examples @examples end |
#message_type ⇒ Object (readonly)
Returns the value of attribute message_type.
186 187 188 |
# File 'lib/interpol/endpoint.rb', line 186 def @message_type end |
#path_params ⇒ Object (readonly)
Returns the value of attribute path_params.
186 187 188 |
# File 'lib/interpol/endpoint.rb', line 186 def path_params @path_params end |
#query_params ⇒ Object (readonly)
Returns the value of attribute query_params.
186 187 188 |
# File 'lib/interpol/endpoint.rb', line 186 def query_params @query_params end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
186 187 188 |
# File 'lib/interpol/endpoint.rb', line 186 def schema @schema end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
186 187 188 |
# File 'lib/interpol/endpoint.rb', line 186 def version @version end |
Instance Method Details
#description ⇒ Object
227 228 229 230 231 |
# File 'lib/interpol/endpoint.rb', line 227 def description subdescription = "#{} v. #{version}" subdescription << " for status: #{status_codes}" if == 'response' "#{endpoint_name} (#{subdescription})" end |
#endpoint_name ⇒ Object
214 215 216 |
# File 'lib/interpol/endpoint.rb', line 214 def endpoint_name @endpoint.name end |
#example_status_code ⇒ Object
241 242 243 |
# File 'lib/interpol/endpoint.rb', line 241 def example_status_code @example_status_code ||= @status_codes.example_status_code end |
#matches_status_code?(status_code) ⇒ Boolean
237 238 239 |
# File 'lib/interpol/endpoint.rb', line 237 def matches_status_code?(status_code) status_code.nil? || @status_codes.matches?(status_code) end |
#request? ⇒ Boolean
206 207 208 |
# File 'lib/interpol/endpoint.rb', line 206 def request? == "request" end |
#response? ⇒ Boolean
210 211 212 |
# File 'lib/interpol/endpoint.rb', line 210 def response? == "response" end |
#status_codes ⇒ Object
233 234 235 |
# File 'lib/interpol/endpoint.rb', line 233 def status_codes @status_codes.code_strings.join(',') end |
#validate_data!(data, validate_schema = true) ⇒ Object
218 219 220 221 222 223 224 225 |
# File 'lib/interpol/endpoint.rb', line 218 def validate_data!(data, validate_schema = true) if validate_schema errors = ::JSON::Validator.fully_validate_schema(schema) raise ValidationError.new(errors, schema, description) if errors.any? end errors = ::JSON::Validator.fully_validate(schema, data) raise ValidationError.new(errors, data, description) if errors.any? end |