Class: Generator::Feed

Inherits:
Object
  • Object
show all
Includes:
FileWriter, SchemaGenerator, SchemaHelpers
Defined in:
lib/generator/feed.rb

Overview

Generates Ruby type classes for SP-API feed schemas from JSON Schema files

Constant Summary

Constants included from Formatter

Generator::Formatter::MAX_LINE_LENGTH

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SchemaGenerator

#generate, #generate_main_file!, #generate_supplementary_files, included, #main_template, #needs_money?, #output_file_path, #sorted_properties

Methods included from SchemaHelpers

#api_name_for_type_resolver, #attribute_name_for, #format_property_comment, #generate_nested_types!, #generate_rbs!, #generic_placeholder?, #needs_money?, #nested_type_files, #ruby_type_for, #type_resolver

Methods included from Formatter

#convert_doc_links_to_full_url, #convert_html_links_to_yard, #format_method_definition, #split_long_comment_line

Methods included from FileWriter

#format_files, #write_file

Constructor Details

#initialize(file_path) ⇒ Feed

Returns a new instance of Feed.



38
39
40
41
# File 'lib/generator/feed.rb', line 38

def initialize(file_path)
  @file_path = file_path
  @schema = JSON.parse(File.read(file_path))
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



25
26
27
# File 'lib/generator/feed.rb', line 25

def file_path
  @file_path
end

#schemaObject (readonly)

Returns the value of attribute schema.



25
26
27
# File 'lib/generator/feed.rb', line 25

def schema
  @schema
end

Class Method Details

.filter_schema_files(files) ⇒ Object

Override to filter out example files



33
34
35
# File 'lib/generator/feed.rb', line 33

def filter_schema_files(files)
  files.reject { |file| file.include?(".example.") }
end

.schema_typeObject



28
29
30
# File 'lib/generator/feed.rb', line 28

def schema_type
  "feeds"
end

Instance Method Details

#class_nameObject

Class name for the feed (e.g., "ListingsFeedSchema")



64
65
66
67
# File 'lib/generator/feed.rb', line 64

def class_name
  # Underscore first to ensure ActiveSupport::Inflector applies acronym rules correctly
  feed_name.underscore.camelize
end

#feed_nameObject

Extract feed name from filename Examples: "listings-feed-schema-v2.json" => "ListingsFeedSchema" "listings-feed-message-schema-v2.json" => "ListingsFeedMessageSchema" "listings-feed-processing-report-schema-v2.json" => "ListingsFeedProcessingReportSchema"



55
56
57
58
59
60
61
# File 'lib/generator/feed.rb', line 55

def feed_name
  base_name = File.basename(file_path, ".json")
  # Remove version suffix (e.g., "-v2")
  name = base_name.sub(/-v\d+$/, "")
  # Convert to PascalCase, treating "feed" and "schema" as separate words
  name.split("-").map(&:capitalize).join
end

#generate_schema_types(written_files, all_types) ⇒ Object



43
44
45
46
47
48
# File 'lib/generator/feed.rb', line 43

def generate_schema_types(written_files, all_types)
  # Generate main feed type
  feed_result = generate_feed_type!
  written_files << feed_result[:file]
  all_types << feed_result[:type]
end

#raw_descriptionObject

Class description from schema Raw description from schema (will be formatted by Type class)



82
83
84
85
86
87
88
89
# File 'lib/generator/feed.rb', line 82

def raw_description
  return unless schema["title"] || schema["description"]

  description = schema["title"] || schema["description"]
  return if generic_placeholder?(description)

  description
end

#root_propertiesObject

Root properties from schema



70
71
72
73
# File 'lib/generator/feed.rb', line 70

def root_properties
  props = schema["properties"] || {}
  sorted_properties(props)
end

#root_required_propertiesObject

Required properties from root level



76
77
78
# File 'lib/generator/feed.rb', line 76

def root_required_properties
  schema["required"] || []
end