class Restspec::Schema::Types::SchemaIdType

Attributes

schema_name[RW]

Public Class Methods

new(schema_name, options = {}) click to toggle source
Calls superclass method Restspec::Schema::Types::BasicType::new
# File lib/restspec/schema/types/schema_id_type.rb, line 4
def initialize(schema_name, options = {})
  self.schema_name = schema_name
  super(options)
end

Public Instance Methods

example_for(attribute) click to toggle source
# File lib/restspec/schema/types/schema_id_type.rb, line 9
def example_for(attribute)
  return sample_item[id_property] if sample_item.present?

  if create_response.code == 201 && value = response_property_value(create_response, id_property)
    value
  else
    hardcoded_fallback
  end
rescue URI::InvalidURIError, Errno::ECONNREFUSED => e
  hardcoded_fallback
end
valid?(attribute, value) click to toggle source
# File lib/restspec/schema/types/schema_id_type.rb, line 21
def valid?(attribute, value)
  return true unless perform_validation?
  item_ids.any? { |item| item[id_property] == value }
end

Private Instance Methods

create_endpoint() click to toggle source
# File lib/restspec/schema/types/schema_id_type.rb, line 78
def create_endpoint
  @create_endpoint ||= get_create_endpoint
end
create_example() click to toggle source
# File lib/restspec/schema/types/schema_id_type.rb, line 82
def create_example
  Restspec.example_for(create_schema_name)
end
create_response() click to toggle source
# File lib/restspec/schema/types/schema_id_type.rb, line 64
def create_response
  if create_endpoint.present?
    create_endpoint.execute(body: create_example)
  end
end
create_schema_name() click to toggle source
# File lib/restspec/schema/types/schema_id_type.rb, line 86
def create_schema_name
  example_options.fetch(:create_schema) { create_endpoint.schema_for(:payload).name }
end
find_endpoint(name) click to toggle source
# File lib/restspec/schema/types/schema_id_type.rb, line 41
def find_endpoint(name)
  Restspec::EndpointStore.get(name)
end
get_create_endpoint() click to toggle source
# File lib/restspec/schema/types/schema_id_type.rb, line 70
def get_create_endpoint
  if schema_name.present?
    Restspec::EndpointStore.get_by_schema_name_and_role(schema_name, :create, :payload)
  else
    find_endpoint(example_options.fetch(:create_endpoint))
  end
end
get_index_endpoint() click to toggle source
# File lib/restspec/schema/types/schema_id_type.rb, line 49
def get_index_endpoint
  if schema_name.present?
    Restspec::EndpointStore.get_by_schema_name_and_role(schema_name, :index, :response)
  else
    find_endpoint(example_options.fetch(:fetch_endpoint))
  end
end
get_sample_item() click to toggle source
# File lib/restspec/schema/types/schema_id_type.rb, line 57
def get_sample_item
  fetch_endpoint = get_index_endpoint
  if fetch_endpoint.present?
    fetch_endpoint.execute.body.try(:sample)
  end
end
hardcoded_fallback() click to toggle source
# File lib/restspec/schema/types/schema_id_type.rb, line 103
def hardcoded_fallback
  example_options.fetch(:hardcoded_fallback, Faker::Number.digit)
end
id_property() click to toggle source
# File lib/restspec/schema/types/schema_id_type.rb, line 94
def id_property
  example_options.fetch(:id) { schema_options.fetch(:id, :id) }
end
item_ids() click to toggle source
# File lib/restspec/schema/types/schema_id_type.rb, line 98
def item_ids
  fetch_endpoint = get_index_endpoint
  fetch_endpoint.execute.body
end
perform_validation?() click to toggle source
# File lib/restspec/schema/types/schema_id_type.rb, line 90
def perform_validation?
  schema_options.fetch(:perform_validation, true)
end
response_property_value(response, property) click to toggle source
# File lib/restspec/schema/types/schema_id_type.rb, line 30
def response_property_value(response, property)
  schema = Restspec::SchemaStore.get(schema_name)
  body = response.body

  if schema.root?
    body[schema.root_name][id_property]
  else
    body[id_property]
  end
end
sample_item() click to toggle source
# File lib/restspec/schema/types/schema_id_type.rb, line 45
def sample_item
  @sample_item ||= get_sample_item
end