Class: Newsmlg2::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/newsmlg2/document.rb

Overview

The NewsML-G2 document entry point: parses XML into a typed item (or newsMessage), exposes it, and serializes back to XML. Each document owns its CatalogStore, built from inline catalogs and catalogRefs.

doc = Newsmlg2.parse(xml)      # or Newsmlg2.parse_file(path)
doc.item                       # => Newsmlg2::NewsItem
doc.item..headlines.first
doc.catalog_store.get_scheme_for_alias("ninat")
doc.to_xml                     # => declaration + item XML

Root dispatch and itemSet resolution go through the Configuration registry; all model parsing is lutaml-model's from_xml. The gem never references a specific XML parser — the adapter is the consumer's choice via Lutaml::Model::Config.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item = nil) ⇒ Document

Returns a new instance of Document.

Parameters:



69
70
71
72
73
# File 'lib/newsmlg2/document.rb', line 69

def initialize(item = nil)
  @item = item
  @catalog_store = CatalogStore.new
  load_catalogs
end

Instance Attribute Details

#itemObject

Returns the value of attribute item.



66
67
68
# File 'lib/newsmlg2/document.rb', line 66

def item
  @item
end

Class Method Details

.parse(xml) ⇒ Object

Parses NewsML-G2 XML into a Document.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/newsmlg2/document.rb', line 21

def parse(xml)
  with_resolved_adapter do
    root_name, klass = root_class(xml)
    unless klass
      raise UnknownRootElement,
            "'#{root_name}' is not a NewsML-G2 item or newsMessage root element"
    end

    new(klass.from_xml(xml))
  end
end

.parse_file(path) ⇒ Object

Parses a NewsML-G2 XML file into a Document.



34
35
36
# File 'lib/newsmlg2/document.rb', line 34

def parse_file(path)
  parse(File.read(path))
end

Instance Method Details

#catalog_storeObject

The catalogs known to this document (inline catalogs plus catalogRefs resolvable through the bundled IPTC catalog cache).



89
90
91
# File 'lib/newsmlg2/document.rb', line 89

def catalog_store
  @catalog_store ||= CatalogStore.new.tap { |store| load_catalogs_into(store) }
end

#to_xml(**options) ⇒ Object Also known as: to_xml_string

Serializes the document: XML declaration plus the item, validating that a guid is present (items only).



95
96
97
98
# File 'lib/newsmlg2/document.rb', line 95

def to_xml(**options)
  item.validate! if item.is_a?(AnyItem)
  item.to_xml({ declaration: true }.merge(options))
end