Class: Interpol::EndpointDefinition

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from HashFetcher

#fetch_from

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, 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)
  make_schema_strict!(@schema)
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



155
156
157
# File 'lib/interpol/endpoint.rb', line 155

def endpoint
  @endpoint
end

#examplesObject (readonly)

Returns the value of attribute examples.



155
156
157
# File 'lib/interpol/endpoint.rb', line 155

def examples
  @examples
end

#message_typeObject (readonly)

Returns the value of attribute message_type.



155
156
157
# File 'lib/interpol/endpoint.rb', line 155

def message_type
  @message_type
end

#path_paramsObject (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_paramsObject (readonly)

Returns the value of attribute query_params.



155
156
157
# File 'lib/interpol/endpoint.rb', line 155

def query_params
  @query_params
end

#schemaObject (readonly)

Returns the value of attribute schema.



155
156
157
# File 'lib/interpol/endpoint.rb', line 155

def schema
  @schema
end

#versionObject (readonly)

Returns the value of attribute version.



155
156
157
# File 'lib/interpol/endpoint.rb', line 155

def version
  @version
end

Instance Method Details

#descriptionObject



193
194
195
196
197
# File 'lib/interpol/endpoint.rb', line 193

def description
  subdescription = "#{message_type} v. #{version}"
  subdescription << " for status: #{status_codes}" if message_type == 'response'
  "#{endpoint_name} (#{subdescription})"
end

#endpoint_nameObject



182
183
184
# File 'lib/interpol/endpoint.rb', line 182

def endpoint_name
  @endpoint.name
end

#example_status_codeObject



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

Returns:

  • (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

Returns:

  • (Boolean)


174
175
176
# File 'lib/interpol/endpoint.rb', line 174

def request?
  message_type == "request"
end

#response?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/interpol/endpoint.rb', line 178

def response?
  message_type == "response"
end

#status_codesObject



199
200
201
# File 'lib/interpol/endpoint.rb', line 199

def status_codes
  @status_codes.code_strings.join(',')
end

#validate_data!(data) ⇒ Object

Raises:



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