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.



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/interpol/endpoint.rb', line 193

def initialize(endpoint, version, message_type, definition)
  @endpoint        = endpoint
  @message_type    = message_type
  @status_codes    = StatusCodeMatcher.new(definition['status_codes'])
  @version         = version
  @schema          = Marshal.load(Marshal.dump 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_metadataObject (readonly)

Returns the value of attribute custom_metadata.



186
187
188
# File 'lib/interpol/endpoint.rb', line 186

def 
  @custom_metadata
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



186
187
188
# File 'lib/interpol/endpoint.rb', line 186

def endpoint
  @endpoint
end

#examplesObject (readonly)

Returns the value of attribute examples.



186
187
188
# File 'lib/interpol/endpoint.rb', line 186

def examples
  @examples
end

#message_typeObject (readonly)

Returns the value of attribute message_type.



186
187
188
# File 'lib/interpol/endpoint.rb', line 186

def message_type
  @message_type
end

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

Returns the value of attribute query_params.



186
187
188
# File 'lib/interpol/endpoint.rb', line 186

def query_params
  @query_params
end

#schemaObject (readonly)

Returns the value of attribute schema.



186
187
188
# File 'lib/interpol/endpoint.rb', line 186

def schema
  @schema
end

#versionObject (readonly)

Returns the value of attribute version.



186
187
188
# File 'lib/interpol/endpoint.rb', line 186

def version
  @version
end

Instance Method Details

#descriptionObject



227
228
229
230
231
# File 'lib/interpol/endpoint.rb', line 227

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

#endpoint_nameObject



214
215
216
# File 'lib/interpol/endpoint.rb', line 214

def endpoint_name
  @endpoint.name
end

#example_status_codeObject



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

Returns:

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

Returns:

  • (Boolean)


206
207
208
# File 'lib/interpol/endpoint.rb', line 206

def request?
  message_type == "request"
end

#response?Boolean

Returns:

  • (Boolean)


210
211
212
# File 'lib/interpol/endpoint.rb', line 210

def response?
  message_type == "response"
end

#status_codesObject



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

Raises:



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, :version => :draft3)
    raise ValidationError.new(errors, schema, description) if errors.any?
  end
  errors = ::JSON::Validator.fully_validate(schema, data, :version => :draft3)
  raise ValidationError.new(errors, data, description) if errors.any?
end