Class: Generator::Notification

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

Overview

Generates Ruby type classes for SP-API notification payloads from JSON Schema files

Constant Summary

Constants included from Formatter

Formatter::MAX_LINE_LENGTH

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SchemaGenerator

#generate_main_file!, included, #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) ⇒ Notification

Returns a new instance of Notification.



35
36
37
38
39
40
# File 'lib/generator/notification.rb', line 35

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

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



27
28
29
# File 'lib/generator/notification.rb', line 27

def file_path
  @file_path
end

#schemaObject (readonly)

Returns the value of attribute schema.



27
28
29
# File 'lib/generator/notification.rb', line 27

def schema
  @schema
end

Class Method Details

.schema_typeObject



30
31
32
# File 'lib/generator/notification.rb', line 30

def schema_type
  "notifications"
end

Instance Method Details

#class_nameObject

Class name for the notification (e.g., "AnyOfferChanged")



117
118
119
120
# File 'lib/generator/notification.rb', line 117

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

#envelope_propertiesObject

Get envelope properties (notificationVersion, notificationType, eventTime, etc.)



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/generator/notification.rb', line 184

def envelope_properties
  props = {}
  ["notificationVersion", "NotificationVersion"].each do |key|
    props[key] = schema.dig("properties", key) if schema.dig("properties", key)
  end
  ["notificationType", "NotificationType"].each do |key|
    props[key] = schema.dig("properties", key) if schema.dig("properties", key)
  end
  ["payloadVersion", "PayloadVersion"].each do |key|
    props[key] = schema.dig("properties", key) if schema.dig("properties", key)
  end
  ["eventTime", "EventTime"].each do |key|
    props[key] = schema.dig("properties", key) if schema.dig("properties", key)
  end
  ["notificationMetadata", "NotificationMetadata"].each do |key|
    props[key] = schema.dig("properties", key) if schema.dig("properties", key)
  end
  props
end

#envelope_required_propertiesObject

Get required envelope properties



205
206
207
208
209
# File 'lib/generator/notification.rb', line 205

def envelope_required_properties
  required = schema["required"] || []
  envelope_keys = envelope_properties.keys
  required.select { |r| envelope_keys.include?(r) }
end

#generateObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/generator/notification.rb', line 42

def generate
  written_files = []
  all_types = []

  # Generate nested types first
  nested_results = generate_nested_types!
  written_files.concat(nested_results[:files])
  all_types.concat(nested_results[:types])

  # Generate Payload and Notification as types
  payload_results = generate_payload_types!
  written_files.concat(payload_results[:files])
  all_types.concat(payload_results[:types])

  notification_result = generate_notification_type!
  written_files << notification_result[:file]
  all_types << notification_result[:type]

  # Generate main convenience file
  written_files << generate_main_file!

  # Reload to pick up newly generated files for RBS introspection
  IntrospectionLoader.reload
  written_files << generate_rbs!(all_types)

  # Batch format all written files
  format_files(written_files)

  Generator.logger.info("Generated notification #{notification_name.underscore}")
end

#notification_nameObject

Extract notification name from filename (e.g., "AnyOfferChangedNotification.json" => "AnyOfferChanged")



74
75
76
77
78
79
# File 'lib/generator/notification.rb', line 74

def notification_name
  base_name = File.basename(file_path, ".json")
  # Remove "Notification" suffix but keep version suffix (e.g., "_2023-12-13")
  # Convert dashes in version suffix to underscores for valid Ruby identifiers
  base_name.sub(/Notification$/, "").tr("-", "_")
end

#notification_object_nameObject

Get the actual notification object name within payload (handles casing variations)



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/generator/notification.rb', line 94

def notification_object_name
  payload_schema = schema.dig("properties", "payload") || schema.dig("properties", "Payload")
  return unless payload_schema

  # If payload uses a $ref, resolve it
  if payload_schema["$ref"]
    ref_path = payload_schema["$ref"].sub("#/definitions/", "")
    payload_schema = schema.dig("definitions", ref_path)
    return unless payload_schema
  end

  # Try different casing conventions
  [
    "#{notification_name.camelize(:lower)}Notification", # feedProcessingFinishedNotification
    "#{notification_name.camelize}Notification", # FeedProcessingFinishedNotification
  ].each do |name|
    return name if payload_schema.dig("properties", name)
  end

  nil
end

#notification_typeObject

Detect if this is Type A (nested) or Type B (flat) notification



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/generator/notification.rb', line 160

def notification_type
  payload_schema = schema.dig("properties", "payload") || schema.dig("properties", "Payload")
  return :unknown unless payload_schema

  # If payload uses a $ref, resolve it
  if payload_schema["$ref"]
    ref_path = payload_schema["$ref"].sub("#/definitions/", "")
    payload_schema = schema.dig("definitions", ref_path)
    return :unknown unless payload_schema
  end

  # Type A: has nested notification object (e.g., feedProcessingFinishedNotification)
  [
    "#{notification_name.camelize(:lower)}Notification",
    "#{notification_name.camelize}Notification",
  ].each do |name|
    return :nested if payload_schema.dig("properties", name)
  end

  # Type B: flat payload
  :flat
end

#payload_key_nameObject

Get the actual payload key name from the schema (handles PascalCase vs camelCase)



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

def payload_key_name
  # Check if schema uses "payload" or "Payload"
  if schema.dig("properties", "payload")
    "payload"
  elsif schema.dig("properties", "Payload")
    "Payload"
  else
    "payload" # fallback
  end
end

#payload_propertiesObject

Get payload properties based on notification type



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/generator/notification.rb', line 217

def payload_properties
  return {} unless payload_schema

  resolved_payload = payload_schema
  # Resolve $ref if present
  if resolved_payload&.dig("$ref")
    ref_path = resolved_payload["$ref"].sub("#/definitions/", "")
    resolved_payload = schema.dig("definitions", ref_path)
    return {} unless resolved_payload
  end

  if notification_type == :nested
    # Type A: extract the nested notification object properties
    [
      "#{notification_name.camelize(:lower)}Notification",
      "#{notification_name.camelize}Notification",
    ].each do |name|
      nested_obj = resolved_payload.dig("properties", name)
      next unless nested_obj

      # Resolve nested object $ref if present
      if nested_obj["$ref"]
        ref_path = nested_obj["$ref"].sub("#/definitions/", "")
        nested_obj = schema.dig("definitions", ref_path)
      end
      return sorted_properties(nested_obj["properties"] || {}) if nested_obj
    end
    {}
  else
    # Type B: use payload properties directly
    sorted_properties(resolved_payload["properties"] || {})
  end
end

#payload_required_propertiesObject

Get required payload properties based on notification type



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/generator/notification.rb', line 252

def payload_required_properties
  return [] unless payload_schema

  resolved_payload = payload_schema
  # Resolve $ref if present
  if resolved_payload&.dig("$ref")
    ref_path = resolved_payload["$ref"].sub("#/definitions/", "")
    resolved_payload = schema.dig("definitions", ref_path)
    return [] unless resolved_payload
  end

  if notification_type == :nested
    # Type A: get required from nested notification object
    [
      "#{notification_name.camelize(:lower)}Notification",
      "#{notification_name.camelize}Notification",
    ].each do |name|
      nested_obj = resolved_payload.dig("properties", name)
      next unless nested_obj

      # Resolve nested object $ref if present
      if nested_obj["$ref"]
        ref_path = nested_obj["$ref"].sub("#/definitions/", "")
        nested_obj = schema.dig("definitions", ref_path)
      end
      return nested_obj["required"] || [] if nested_obj
    end
    []
  else
    # Type B: use payload required directly
    resolved_payload["required"] || []
  end
end

#payload_schemaObject

Get payload schema (the full payload object)



212
213
214
# File 'lib/generator/notification.rb', line 212

def payload_schema
  schema.dig("properties", "payload") || schema.dig("properties", "Payload")
end

#payload_versionObject

Extract payload version from the schema examples or filename



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/generator/notification.rb', line 123

def payload_version
  # Try to get from examples first
  examples = schema.dig("examples")
  if examples&.first
    return examples.first["payloadVersion"] || examples.first["PayloadVersion"]
  end

  # Fall back to filename-based version (e.g., "_2023-12-13")
  if file_path =~ /_(\d{4}-\d{2}-\d{2})\.json$/
    matched = ::Regexp.last_match(1)
    return matched if matched
  end

  # Default to 1.0 if no version found
  "1.0"
end

#raw_descriptionObject

Class description from schema (no leading spaces - template handles indentation) Raw description from schema (will be formatted by Type class)



152
153
154
155
156
157
# File 'lib/generator/notification.rb', line 152

def raw_description
  return unless schema["description"]
  return if generic_placeholder?(schema["description"])

  schema["description"]
end

#replace_inline_objects_with_refs(properties, extracted_types) ⇒ Object

Replace inline object definitions with $ref pointers to extracted types



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/generator/notification.rb', line 287

def replace_inline_objects_with_refs(properties, extracted_types)
  result = {}
  properties.each do |prop_name, prop_def|
    # Check if this is a money-like object
    if MoneyDetector.money_like?(prop_def)
      result[prop_name] = {
        "$ref" => "#/definitions/Money",
        "description" => prop_def["description"],
      }.compact
      next
    end

    # Check if this property matches an extracted type name
    if prop_def["type"] == "object" && extracted_types.key?(prop_name)
      # Replace with $ref
      result[prop_name] = { "$ref" => "#/definitions/#{prop_name}" }
    elsif prop_def["type"] == "array" && prop_def["items"]
      # Handle array items - check if they're objects that match extracted types
      items = prop_def["items"]

      # Handle case where items is an array (non-standard format)
      items = items.first if items.is_a?(Array) && !items.empty?

      # Check if items are money-like objects
      if items.is_a?(Hash) && MoneyDetector.money_like?(items)
        result[prop_name] = prop_def.dup
        result[prop_name]["items"] = { "$ref" => "#/definitions/Money" }
        next
      end

      # Check if items are inline objects that match extracted types
      if items.is_a?(Hash) && (items["type"] == "object" || items["anyOf"])
        item_type_name = prop_name.singularize
        if extracted_types.key?(item_type_name)
          result[prop_name] = prop_def.dup
          result[prop_name]["items"] = { "$ref" => "#/definitions/#{item_type_name}" }
        else
          result[prop_name] = prop_def
        end
      else
        result[prop_name] = prop_def
      end
    else
      result[prop_name] = prop_def
    end
  end
  result
end

#wrapper_propertiesObject

Properties from the wrapper level (notificationVersion, notificationType, etc.)



141
142
143
# File 'lib/generator/notification.rb', line 141

def wrapper_properties
  sorted_properties(schema["properties"] || {})
end

#wrapper_required_propertiesObject

Required properties from the wrapper level



146
147
148
# File 'lib/generator/notification.rb', line 146

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