Module: Html2rss::Selectors::SchemaDoc

Defined in:
lib/html2rss/selectors/schema_doc.rb

Overview

Builds JSON Schema fragments from extractor / post-processor class constants.

Each registry class owns DESCRIPTION, EXAMPLES, and optional OPTION_TYPES; this module is the export adapter only.

Constant Summary collapse

RUBY_TO_JSON_TYPE =

Maps Ruby option types to JSON Schema type strings.

{
  String => 'string',
  Integer => 'integer',
  Hash => 'object',
  Array => 'array'
}.freeze

Class Method Summary collapse

Class Method Details

.for_extractor(name:, klass:) ⇒ Hash{Symbol => Object}

Returns JSON Schema for one extractor name.

Parameters:

  • name (Symbol, String)

    registry key

  • klass (Class)

    extractor class

Returns:

  • (Hash{Symbol => Object})

    JSON Schema for one extractor name



42
43
44
45
46
47
48
49
50
51
# File 'lib/html2rss/selectors/schema_doc.rb', line 42

def for_extractor(name:, klass:)
  name = name.to_s
  {
    type: 'string',
    const: name,
    title: name,
    description: klass::DESCRIPTION,
    examples: klass::EXAMPLES
  }
end

.for_post_processor(name:, klass:) ⇒ Hash{Symbol => Object}

Returns JSON Schema object for one post-processor.

Parameters:

  • name (Symbol, String)

    registry key

  • klass (Class)

    post-processor class

Returns:

  • (Hash{Symbol => Object})

    JSON Schema object for one post-processor



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/html2rss/selectors/schema_doc.rb', line 25

def for_post_processor(name:, klass:)
  name = name.to_s
  {
    type: 'object',
    title: name,
    description: klass::DESCRIPTION,
    examples: klass::EXAMPLES,
    properties: post_processor_properties(name, klass),
    required: post_processor_required(klass),
    additionalProperties: true
  }
end

.json_type_for(ruby_type) ⇒ String

Parameters:

  • ruby_type (Class)

Returns:

  • (String)


91
92
93
94
95
# File 'lib/html2rss/selectors/schema_doc.rb', line 91

def json_type_for(ruby_type)
  RUBY_TO_JSON_TYPE.fetch(ruby_type) do
    raise ArgumentError, "unsupported OPTION_TYPES mapping for #{ruby_type}"
  end
end

.option_types_for(klass) ⇒ Hash{Symbol => Class}

Parameters:

  • klass (Class)

Returns:

  • (Hash{Symbol => Class})


80
81
82
83
84
85
# File 'lib/html2rss/selectors/schema_doc.rb', line 80

def option_types_for(klass)
  types = {}
  types.merge!(klass::OPTION_TYPES) if klass.const_defined?(:OPTION_TYPES)
  types.merge!(klass::OPTIONAL_OPTION_TYPES) if klass.const_defined?(:OPTIONAL_OPTION_TYPES)
  types
end

.post_processor_properties(name, klass) ⇒ Hash{Symbol => Hash}

Parameters:

  • name (String)

    registry key

  • klass (Class)

Returns:

  • (Hash{Symbol => Hash})


57
58
59
60
61
62
63
# File 'lib/html2rss/selectors/schema_doc.rb', line 57

def post_processor_properties(name, klass)
  properties = { name: { type: 'string', const: name } }
  option_types_for(klass).each do |field, ruby_type|
    properties[field] = { type: json_type_for(ruby_type) }
  end
  properties
end

.post_processor_required(klass) ⇒ Array<String>

Parameters:

  • klass (Class)

Returns:

  • (Array<String>)


69
70
71
72
73
74
# File 'lib/html2rss/selectors/schema_doc.rb', line 69

def post_processor_required(klass)
  required = ['name']
  return required unless klass.const_defined?(:OPTION_TYPES)

  required + klass::OPTION_TYPES.keys.map(&:to_s)
end