Module: PacerXml::XmlRoute

Defined in:
lib/pacer-xml/xml_route.rb

Defined Under Namespace

Modules: ImportHelp

Instance Method Summary collapse

Instance Method Details

#childrenObject



82
83
84
# File 'lib/pacer-xml/xml_route.rb', line 82

def children
  flat_map(element_type: :xml) { |x| x.children.to_a }
end

#elementsObject



94
95
96
# File 'lib/pacer-xml/xml_route.rb', line 94

def elements
  select &:element?
end

#fieldsObject



98
99
100
# File 'lib/pacer-xml/xml_route.rb', line 98

def fields
  elements.map element_type: :hash, &:fields
end

#help(section = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
72
73
74
75
76
77
78
79
80
# File 'lib/pacer-xml/xml_route.rb', line 3

def help(section = nil)
  case section
  when nil
    puts <<HELP
This is included via the pacer-xml gem plugin.

pacer-xml uses Nokogiri for its xml parsing. Each element in an xml route
is the first child element of the Nokogiri::XML::Document element. To get at
the document element, simply call #parent on the element.

An xml route can be created, transformed, filtered and otherwise
processed by all standard Pacer routes. For instance, if a graph element
has a property with xml data in it, we could process it as follows:

  g.v.map(element_type: :xml) { |v| Nokogiri(v[:xml]) }

Method help sections:
  :xml
  :import

HELP
  when :xml
    puts <<HELP



Turn an xml file into a stream of xml nodes. Scans the xml file
line-by-line and uses arguments defined in start_section and end_section
to extract sections from the file.

Pacer.xml(file, start_section = nil, end_section = nil)

file:          String | IO
String           path to an xml file to read
IO               an open resource that responds to #each_line
  start_section: String | Symbol | Regex | Proc  (optional)
String | Symbol  name of xml tag to use as the root node of each
                 section of xml. The end_section will automatically be
                 set to the closing tag.  This uses very simple regex
                 matching.
Regex            If it matches, start the section from this line
Proc             proc { |line| }
                 If it results in a truthy value, starts collecting
                 lines for the next section of xml.
  end_section:   Proc  (optional)
Regex            If it matches, end the section including this line
Proc             proc { |line, lines| }
                 - If it results in a truthy value to indicate that the
                   current line is the last line in a section.
                 - if it results in an Array, pass the result of
                   joining the array to Nokogiri for the next section.

HELP
  when :import
    puts <<HELP
Turn the tree of xml in each node in the stream

xml_route.import(graph, opts = {})

  graph: PacerGraph   The graph to load the data into.
  opts:  Hash
:cache  false | Hash
  false              disable caching
  stats: true        enable occasional dump of cache info
:rename Hash         map of { 'old-name' => 'new-name' }
:html   Array        set of tag names to treat as containing HTML
:skip   Array        set of tag or attribute names to skip

Produces a vertex route where each vertex is the root vertex for each xml tree.

Look at the source of lib/pacer-xml/sample.rb a good example.

HELP
  else
    super
  end
  description
end

#import(graph, opts = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/pacer-xml/xml_route.rb', line 102

def import(graph, opts = {})
  if opts[:cache] == false
    builder = BuildGraph.new(graph, opts)
  else
    builder = BuildGraphCached.new(graph, opts)
  end
  graph.vertex_name ||= proc { |v| v[:type] }
  to_route.map(route_name: 'import', graph: graph, element_type: :vertex, modules: [ImportHelp]) do |node|
    graph.transaction do
      builder.build(node)
    end
  end.route
end

#namesObject



86
87
88
# File 'lib/pacer-xml/xml_route.rb', line 86

def names
  map element_type: :string, &:name
end

#text_nodesObject



90
91
92
# File 'lib/pacer-xml/xml_route.rb', line 90

def text_nodes
  select &:text?
end