Method: Webhookdb::Replicator::TransistorEpisodeV1#_prepare_for_insert

Defined in:
lib/webhookdb/replicator/transistor_episode_v1.rb

#_prepare_for_insert(resource, event, request, enrichment) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/webhookdb/replicator/transistor_episode_v1.rb', line 68

def _prepare_for_insert(resource, event, request, enrichment)
  h = super
  # Transistor merged their summary and description fields so they're authored
  # as one big 'description' HTML blob in February 2023. Previous to that,
  # there were separate summary and description fields
  # (we call this api_format 1).
  #
  # If we have a nil summary, we know this is a 'new' format (api_format 2).
  # In that case, look for the first line of the HTML,
  # and treat that as the summary. Anything else in the HTML is treated as
  # the remaining description. Some care is paid to whitespace, too,
  # since <br> tags can be used within an element.
  summary = resource.fetch("attributes").fetch("summary", nil)
  description = resource.fetch("attributes").fetch("description", nil)
  if summary.nil?
    h[:api_format] = 2
    parsed_desc = Nokogiri::HTML5.fragment(description)

    extracted_summary = self._extract_first_html_line_as_text(parsed_desc)
    h[:logical_description] = nil
    if extracted_summary
      h[:logical_summary] = extracted_summary
      h[:logical_description] = parsed_desc.to_s.strip if parsed_desc.inner_text.present?
    else
      h[:logical_summary] = parsed_desc.to_s.strip
    end
  else
    h[:logical_summary] = summary
    h[:logical_description] = description
    h[:api_format] = 1
  end
  h.merge!(enrichment) if enrichment
  return h
end