class Lurker::Json::Attribute

Constants

COLOR
DATE_TIME
FORMAT
TYPE_MAP
URI

Public Instance Methods

eql?(schema) click to toggle source
# File lib/lurker/json/schema/attribute.rb, line 41
def eql?(schema)
  @schema[Json::TYPE] == attributify(schema)[Json::TYPE]
end
merge!(schema) click to toggle source
# File lib/lurker/json/schema/attribute.rb, line 22
def merge!(schema)
  return replace!(schema) if @schema[Json::TYPE].blank?

  schema = attributify(schema)
  return if eql?(schema)

  replace_options = {root_schema: root_schema, parent_schema: parent_schema,
                     parent_property: parent_property}

  attributes_tuple = Lurker::Json::Tuple::AnyOf.new(
    [to_hash, schema], replace_options)

  parent_schema.replace!(parent_property, attributes_tuple)
end
replace!(schema) click to toggle source
# File lib/lurker/json/schema/attribute.rb, line 37
def replace!(schema)
  @schema.clear.merge!(attributify schema)
end

Private Instance Methods

attributify(schema) click to toggle source
# File lib/lurker/json/schema/attribute.rb, line 65
def attributify(schema)
  return schema if schema.is_a?(Hash) || schema.is_a?(Lurker::Json::Schema)

  attribute = {
    Json::DESCRIPTION => '',
    Json::TYPE => guess_type(schema),
    Json::EXAMPLE => serialize_example(schema)
  }

  if format = guess_format(schema)
    attribute[FORMAT] = format
  end

  attribute
end
guess_format(data) click to toggle source
# File lib/lurker/json/schema/attribute.rb, line 94
def guess_format(data)
  if data.is_a?(Time)
    DATE_TIME
  elsif data.is_a?(String)
    if data.start_with? 'http://'
      URI
    elsif data.match(/\#[0-9a-fA-F]{3}(?:[0-9a-fA-F]{3})?\b/)
      COLOR
    else
      begin
        DATE_TIME if Time.iso8601(data)
      rescue
        nil
      end
    end
  end
end
guess_type(data) click to toggle source
# File lib/lurker/json/schema/attribute.rb, line 89
def guess_type(data)
  data_type = data.class.to_s
  TYPE_MAP[data_type] || data_type.downcase
end
initialize_default_properties() click to toggle source
# File lib/lurker/json/schema/attribute.rb, line 47
def initialize_default_properties
  @schema[Json::DESCRIPTION] ||= ''
  @schema[Json::TYPE] ||= ''
  @schema[Json::EXAMPLE] = '' if @schema[Json::EXAMPLE].nil?
end
parse_schema(schema) click to toggle source
# File lib/lurker/json/schema/attribute.rb, line 53
def parse_schema(schema)
  @schema = {}

  if schema.is_a?(Hash)
    @schema.merge!(schema)
  else
    @schema = attributify(schema)
  end

  initialize_default_properties
end
serialize_example(data) click to toggle source
# File lib/lurker/json/schema/attribute.rb, line 81
def serialize_example(data)
  if data.is_a?(ActionDispatch::Http::UploadedFile)
    data.headers
  else
    data
  end
end