Class: AdfBuilder::Builder

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



35
36
37
38
# File 'lib/adf_builder.rb', line 35

def initialize
  @doc = self.init_doc
  @base = Base.new(@doc)
end

Class Method Details

.update_node(parent_node, key, value, params = {}) ⇒ Object

we will either create a new node with the value or replace the one if it is available



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/adf_builder.rb', line 119

def self.update_node(parent_node, key, value, params={})
  key = key.to_s
  value = value.to_s
  if parent_node.locate(key).size > 0
    node = parent_node.locate(key).first
    node.replace_text(value)
  else
    node = (Ox::Element.new(key) << value)
    parent_node << node
  end

  AdfBuilder::Builder.update_params(node, key, params)
end

.update_params(node, key, params) ⇒ Object

update the params by first checking if they are valid params and then checking if the values are valid if necessary



134
135
136
137
138
139
140
141
142
143
# File 'lib/adf_builder.rb', line 134

def self.update_params(node, key, params)
  return true if params.empty?
  key = key.to_sym
  valid_values = params[:valid_values]
  valid_parameters = params[:valid_parameters]
  _params = AdfBuilder::Builder.whitelabel_params(params,valid_parameters, key)
  _params.each do |k,v|
    node[k] = v if valid_values[key][k] == true or valid_values[key][k].include? v.to_s
  end
end

.valid_child?(parent, tag_name, index) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
153
# File 'lib/adf_builder.rb', line 150

def self.valid_child?(parent, tag_name, index)
  child = parent.locate(tag_name)[index]
  return !child.nil?,child
end

.whitelabel_params(opts, valid_parameters, key) ⇒ Object

clear out the opts that don’t match valid keys



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

def self.whitelabel_params(opts, valid_parameters, key)
  opts.slice(*valid_parameters[key])
end

Instance Method Details

#init_docObject

all the files will start with this same header



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/adf_builder.rb', line 104

def init_doc
  doc = Ox::Document.new
  instruct = Ox::Instruct.new(:xml)
  instruct[:version] = '1.0'
  doc << instruct
  doc << Ox::Raw.new("")
  instruct = Ox::Instruct.new('ADF')
  instruct[:version] = '1.0'
  doc << instruct
  adf = Ox::Element.new("adf")
  doc << adf
  doc
end

#minimal_leadObject

def an example of minimal XML taken from ADF spec file adfxml.info/adf_spec.pdf



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/adf_builder.rb', line 50

def minimal_lead
  prospect = Ox::Element.new("prospect")

  request_date = Ox::Element.new("requestdate")
  request_date << '2000-03-30T15:30:20-08:00'

  vehicle = Ox::Element.new('vehicle')
  year = Ox::Element.new("year")
  year << '1999'

  make = Ox::Element.new("make")
  make << 'Chevrolet'

  model = Ox::Element.new("model")
  model << 'Blazer'

  vehicle << year << make << model

  customer = Ox::Element.new("customer")

  contact = Ox::Element.new("contact")

  name = Ox::Element.new("name")
  name[:part] = 'full'
  name << 'John Doe'

  phone = Ox::Element.new("phone")
  phone << '393-999-3922'

  contact << name << phone
  customer << contact

  vendor = Ox::Element.new("vendor")

  contact = Ox::Element.new("contact")
  name = Ox::Element.new("name")
  name[:part] = 'full'
  name << 'Acura of Bellevue'

  contact << name
  vendor << contact

  prospect << request_date << vehicle << customer << vendor
  @doc.remove_children_by_path("adf/prospect")
  @doc.adf << prospect
  Ox.dump(@doc, {})
end

#prospectObject



40
41
42
# File 'lib/adf_builder.rb', line 40

def prospect
  @base.prospect
end

#reset_docObject

go back to the initial structure



99
100
101
# File 'lib/adf_builder.rb', line 99

def reset_doc
  @doc.adf.prospect.remove_children_by_path("*")
end

#to_xmlObject

output the XML



45
46
47
# File 'lib/adf_builder.rb', line 45

def to_xml
  Ox.dump(@doc, {})
end