Module: NominetEPP::Helpers

Defined in:
lib/nominet-epp/helpers.rb

Overview

Helper methods

Instance Method Summary collapse

Instance Method Details

#account_contact_to_xml(contact, ns) ⇒ XML::Node

Creates an XML contact element

Parameters:

  • contact (Hash)

    Contact attributes

  • ns (XML::Namespace)

    XML Namespace to create the elements under

Returns:

  • (XML::Node)

    XML contact element

Raises:

  • (ArgumentError)


132
133
134
135
136
137
138
139
140
141
# File 'lib/nominet-epp/helpers.rb', line 132

def (contact, ns)
  raise ArgumentError, "contact must be a hash" unless contact.is_a?(Hash)
  raise ArgumentError, "ns must be an xml namespace" unless ns.is_a?(XML::Namespace)

  XML::Node.new('contact', nil, ns).tap do |node|
    node['order'] = contact.delete(:order).to_s if contact.has_key?(:order)

    node << contact_to_xml(contact)
  end
end

#account_contacts_to_xml(contacts, ns) {|node| ... } ⇒ Array

Creates and array of XML::Node objects for each contact passed.

Parameters:

  • contacts (Array)

    Array of contacts

  • ns (XML::Namespace)

    XML Namespace to create the elements under

Yields:

  • (node)

Yield Parameters:

  • node (XML::Node)

    XML contact element

Returns:

  • (Array)

    array of XML contact nodes

Raises:

  • (ArgumentError)


114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/nominet-epp/helpers.rb', line 114

def (contacts, ns)
  raise ArgumentError, "contacts must be an Array" unless contacts.is_a?(Array)
  raise ArgumentError, "ns must be an xml namespace" unless ns.is_a?(XML::Namespace)

  contacts = (contacts)

  contacts[0,3].map do |contact|
    (contact, ns).tap do |n|
      yield n if block_given?
    end
  end
end

#account_fields_xml(fields, node, ns) ⇒ Object

Adds the attributes from the fields to the node.

Parameters:

  • fields (Hash)

    Account attributes to marshal

  • node (XML::Node)

    XML Node to add the attributes to

  • ns (XML::Namespace)

    XML Namespace to create the elements under



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/nominet-epp/helpers.rb', line 56

def (fields, node, ns)
  [:trad_name, :type, :co_no, :opt_out, :addr, :contacts].each do |k|
    next if fields[k].nil?
    case k
    when :contacts
      (fields[k], ns) do |n|
        node << n
      end
    when :addr  # Limitation, can only handle one addr at a time
      node << addr_to_xml(fields[k], ns)
    else
      node << generic_field_to_xml(k, fields[k], ns) unless fields[k] == ''
    end
  end
end

#addr_to_xml(addr, ns) ⇒ XML::Node

Creates an XML addr element

Parameters:

  • addr (Hash)

    Address attributes

  • ns (XML::Namespace)

    XML Namespace to create the elements in

Returns:

  • (XML::Node)

    XML addr element

Raises:

  • (ArgumentError)


170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/nominet-epp/helpers.rb', line 170

def addr_to_xml(addr, ns)
  keys = [:street, :locality, :city, :county, :postcode, :country]
  raise ArgumentError, "addr must be a hash" unless addr.is_a?(Hash)
  raise ArgumentError, "ns must be an xml namespace" unless ns.is_a?(XML::Namespace)
  raise ArgumentError, "Address allowed keys are #{keys.join(', ')}" unless (addr.keys - keys).empty?

  XML::Node.new('addr', nil, ns).tap do |a|
    keys.each do |key|
      next if addr[key].nil? || addr[key] == ''
      a << XML::Node.new(key, addr[key], ns)
    end
  end
end

#contact_to_xml(contact) ⇒ XML::Node

Creates an XML contact:create or contact:update element.

The choice to create a create or update element is determined by the presence of a :roid key in the list of attributes.

Parameters:

  • contact (Hash)

    Contact attributes

Returns:

  • (XML::Node)

    XML contact:action element with the attributes

Raises:

  • (ArgumentError)


150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/nominet-epp/helpers.rb', line 150

def contact_to_xml(contact)
  keys = [:roid, :name, :phone, :mobile, :email]
  raise ArgumentError, "contact must be a hash" unless contact.is_a?(Hash)
  raise ArgumentError, "Contact allowed keys are #{keys.join(', ')}" unless (contact.keys - keys).empty?
  raise ArgumentError, "Contact requires name and email keys" unless contact.has_key?(:name) && contact.has_key?(:email)

  action = contact[:roid].nil? || contact[:roid] == '' ? 'create' : 'update'
  contact(action) do |node, ns|
    keys.each do |key|
      next if contact[key].nil? || contact[key] == ''
      node << XML::Node.new(key, contact[key], ns)
    end
  end
end

#domain_host_xml(nameserver, ns) ⇒ XML::Node

Returns host element with hostName and optionally ip subelements.

Parameters:

  • nameserver (String, Hash)

    Nameserver host name or hash of :name and :v4 or :v6 address

  • ns (XML::Namespace)

    XML Namespace to create the elements with

Returns:

  • (XML::Node)

    host element with hostName and optionally ip subelements



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/nominet-epp/helpers.rb', line 28

def domain_host_xml(nameserver, ns)
  host = XML::Node.new('host', nil, ns)

  case nameserver
  when String
    host << XML::Node.new('hostName', nameserver, ns)
  when Hash
    host << XML::Node.new('hostName', nameserver[:name], ns)
    if nameserver[:v4]
      host << XML::Node.new('hostAddr', nameserver[:v4], ns).tap do |n|
        n['ip'] = 'v4'
      end
    end
    if nameserver[:v6]
      host << XML::Node.new('hostAddr', nameserver[:v6], ns).tap do |n|
        n['ip'] = 'v6'
      end
    end
  end

  host
end

#domain_ns_xml(nameservers, ns) ⇒ XML::Node

Returns ns element of host elements.

Parameters:

  • nameservers (String, Array)

    Nameserver host name or array of host names or hashes

  • ns (XML::Namespace)

    XML Namespace to create the elements with

Returns:

  • (XML::Node)

    ns element of host elements

See Also:



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/nominet-epp/helpers.rb', line 8

def domain_ns_xml(nameservers, ns)
  ns_el = XML::Node.new('ns', nil, ns)

  case nameservers
  when String
    ns_el << domain_host_xml(nameservers, ns)
  when Array
    nameservers.each do |nameserver|
      ns_el << domain_host_xml(nameserver, ns)
    end
  else
    raise ArgumentError, "nameservers must either be a string or array"
  end

  ns_el
end

#fixup_account_contacts(contacts) ⇒ Array<Hash>

Massage the contacts to ensure they have an :order parameter

Parameters:

  • contacts (Array<Hash>)

    Array of contact attributes

Returns:

  • (Array<Hash>)

    Fixed array of contact attributes



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/nominet-epp/helpers.rb', line 86

def (contacts)
  if contacts.all? { |c| c.has_key? :order }
    return contacts.sort { |a,b| a[:order].to_i <=> b[:order].to_i }
  elsif contacts.any? { |c| c.has_key? :order }
    unordered = contacts.map {|c| c if c[:order].nil? }.compact
    ordered = Array.new
    contacts.each do |c|
      next if c[:order].nil?
      ordered[c[:order].to_i - 1] = c
    end

    contacts = ordered.map do |i|
      unless i.nil? then i
      else unordered.shift
      end
    end + unordered
  end

  contacts.each_with_index { |c,i| c[:order] = (i+1).to_s }
end

#generic_field_to_xml(name, value, ns) ⇒ XML::Node

Creates an XML node with the dashed form of the name.

Parameters:

  • name (String)

    Element name, underscores will be converted to hyphens

  • value (String)

    Element value

  • ns (XML::Namespace)

    XML Namespace to create the element under

Returns:

  • (XML::Node)


78
79
80
# File 'lib/nominet-epp/helpers.rb', line 78

def generic_field_to_xml(name, value, ns)
  XML::Node.new(name.to_s.gsub('_', '-'), value, ns)
end