nominet-epp 0.0.8 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
data/History.md CHANGED
@@ -1,3 +1,15 @@
1
+
2
+ 0.0.10 / 2011-05-04
3
+ ===================
4
+
5
+ * Fix syntax error in helpers.rb
6
+
7
+ 0.0.9 / 2011-05-04
8
+ ==================
9
+
10
+ * Correct implementation of Update Operation for domain, account and contact to send
11
+ the correct sequence of XML elements
12
+
1
13
  0.0.8 / 2011-05-03
2
14
  ==================
3
15
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.8
1
+ 0.0.10
@@ -34,14 +34,14 @@ module NominetEPP
34
34
  when Hash
35
35
  host << XML::Node.new('hostName', nameserver[:name], ns)
36
36
  if nameserver[:v4]
37
- n = XML::Node.new('hostAddr', nameserver[:v4], ns)
38
- n['ip'] = 'v4'
39
- host << n
37
+ host << XML::Node.new('hostAddr', nameserver[:v4], ns).tap do |n|
38
+ n['ip'] = 'v4'
39
+ end
40
40
  end
41
41
  if nameserver[:v6]
42
- n = XML::Node.new('hostAddr', nameserver[:v6], ns)
43
- n['ip'] = 'v6'
44
- host << n
42
+ host << XML::Node.new('hostAddr', nameserver[:v6], ns).tap do |n|
43
+ n['ip'] = 'v6'
44
+ end
45
45
  end
46
46
  end
47
47
 
@@ -54,28 +54,130 @@ module NominetEPP
54
54
  # @param [XML::Node] node XML Node to add the attributes to
55
55
  # @param [XML::Namespace] ns XML Namespace to create the elements under
56
56
  def account_fields_xml(fields, node, ns)
57
- fields.each do |k,v|
57
+ [:trad_name, :type, :co_no, :opt_out, :addr, :contacts].each do |k|
58
+ next if fields[k].nil?
58
59
  case k
59
60
  when :contacts
60
- v.each do |c|
61
- node << (c_node = XML::Node.new('contact', nil, ns))
62
- c_node['order'] = c.delete(:order)
63
- c_node['type'] = 'admin'
64
-
65
- c_node << contact(c.delete(:action) || 'create') do |cn, cns|
66
- c.each do |l,w|
67
- cn << XML::Node.new(l, w, cns)
68
- end
69
- end
61
+ account_contacts_to_xml(fields[k], ns) do |n|
62
+ node << n
70
63
  end
71
64
  when :addr # Limitation, can only handle one addr at a time
72
- node << (a_node = XML::Node.new('addr', nil, ns))
73
- a_node['type'] = 'admin'
74
- v.each do |l,w|
75
- a_node << XML::Node.new(l, w, ns)
76
- end
65
+ node << addr_to_xml(fields[k], ns)
77
66
  else
78
- node << XML::Node.new(k.to_s.gsub('_', '-'), v, ns)
67
+ node << generic_field_to_xml(k, fields[k], ns) unless fields[k] == ''
68
+ end
69
+ end
70
+ end
71
+
72
+ # Creates an XML node with the dashed form of the +name+.
73
+ #
74
+ # @param [String] name Element name, underscores will be converted to hyphens
75
+ # @param [String] value Element value
76
+ # @param [XML::Namespace] ns XML Namespace to create the element under
77
+ # @return [XML::Node]
78
+ def generic_field_to_xml(name, value, ns)
79
+ XML::Node.new(name.to_s.gsub('_', '-'), value, ns)
80
+ end
81
+
82
+ # Massage the contacts to ensure they have an :order parameter
83
+ #
84
+ # @param [Array<Hash>] contacts Array of contact attributes
85
+ # @return [Array<Hash>] Fixed array of contact attributes
86
+ def fixup_account_contacts(contacts)
87
+ if contacts.all? { |c| c.has_key? :order }
88
+ return contacts.sort { |a,b| a[:order].to_i <=> b[:order].to_i }
89
+ elsif contacts.any? { |c| c.has_key? :order }
90
+ unordered = contacts.map {|c| c if c[:order].nil? }.compact
91
+ ordered = Array.new
92
+ contacts.each do |c|
93
+ next if c[:order].nil?
94
+ ordered[c[:order].to_i - 1] = c
95
+ end
96
+
97
+ contacts = ordered.map do |i|
98
+ unless i.nil? then i
99
+ else unordered.shift
100
+ end
101
+ end + unordered
102
+ end
103
+
104
+ contacts.each_with_index { |c,i| c[:order] = (i+1).to_s }
105
+ end
106
+
107
+ # Creates and array of XML::Node objects for each contact passed.
108
+ #
109
+ # @param [Array] contacts Array of contacts
110
+ # @param [XML::Namespace] ns XML Namespace to create the elements under
111
+ # @yield [node]
112
+ # @yieldparam [XML::Node] node XML contact element
113
+ # @return [Array] array of XML contact nodes
114
+ def account_contacts_to_xml(contacts, ns)
115
+ raise ArgumentError, "contacts must be an Array" unless contacts.is_a?(Array)
116
+ raise ArgumentError, "ns must be an xml namespace" unless ns.is_a?(XML::Namespace)
117
+
118
+ contacts = fixup_account_contacts(contacts)
119
+
120
+ contacts[0,3].map do |contact|
121
+ account_contact_to_xml(contact, ns).tap do |n|
122
+ yield n if block_given?
123
+ end
124
+ end
125
+ end
126
+
127
+ # Creates an XML contact element
128
+ #
129
+ # @param [Hash] contact Contact attributes
130
+ # @param [XML::Namespace] ns XML Namespace to create the elements under
131
+ # @return [XML::Node] XML contact element
132
+ def account_contact_to_xml(contact, ns)
133
+ raise ArgumentError, "contact must be a hash" unless contact.is_a?(Hash)
134
+ raise ArgumentError, "ns must be an xml namespace" unless ns.is_a?(XML::Namespace)
135
+
136
+ XML::Node.new('contact', nil, ns).tap do |node|
137
+ node['order'] = contact.delete(:order).to_s if contact.has_key?(:order)
138
+ node['type'] = 'admin'
139
+
140
+ node << contact_to_xml(contact)
141
+ end
142
+ end
143
+
144
+ # Creates an XML +contact:create+ or +contact:update+ element.
145
+ #
146
+ # The choice to create a +create+ or +update+ element is determined by the presence of
147
+ # a +:roid+ key in the list of attributes.
148
+ #
149
+ # @param [Hash] contact Contact attributes
150
+ # @return [XML::Node] XML contact:+action+ element with the attributes
151
+ def contact_to_xml(contact)
152
+ keys = [:roid, :name, :phone, :mobile, :email]
153
+ raise ArgumentError, "contact must be a hash" unless contact.is_a?(Hash)
154
+ raise ArgumentError, "Contact allowed keys are #{keys.join(', ')}" unless (contact.keys - keys).empty?
155
+ raise ArgumentError, "Contact requires name and email keys" unless contact.has_key?(:name) && contact.has_key?(:email)
156
+
157
+ action = contact[:roid].nil? || contact[:roid] == '' ? 'create' : 'update'
158
+ contact(action) do |node, ns|
159
+ keys.each do |key|
160
+ next if contact[key].nil? || contact[key] == ''
161
+ node << XML::Node.new(key, contact[key], ns)
162
+ end
163
+ end
164
+ end
165
+
166
+ # Creates an XML addr element
167
+ #
168
+ # @param [Hash] addr Address attributes
169
+ # @param [XML::Namespace] ns XML Namespace to create the elements in
170
+ # @return [XML::Node] XML addr element
171
+ def addr_to_xml(addr, ns)
172
+ keys = [:street, :locality, :city, :county, :postcode, :country]
173
+ raise ArgumentError, "addr must be a hash" unless addr.is_a?(Hash)
174
+ raise ArgumentError, "ns must be an xml namespace" unless ns.is_a?(XML::Namespace)
175
+ raise ArgumentError, "Address allowed keys are #{keys.join(', ')}" unless (addr.keys - keys).empty?
176
+
177
+ XML::Node.new('addr', nil, ns).tap do |a|
178
+ keys.each do |key|
179
+ next if addr[key].nil? || addr[key] == ''
180
+ a << XML::Node.new(key, addr[key], ns)
79
181
  end
80
182
  end
81
183
  end
@@ -90,62 +90,13 @@ module NominetEPP
90
90
  elsif acct.kind_of?(Hash)
91
91
  account('create') do |node, ns|
92
92
  node << XML::Node.new('name', acct[:name], ns)
93
- node << XML::Node.new('trad-name', acct[:trad_name], ns) unless acct[:trad_name].nil? || acct[:trad_name] == ''
94
- node << XML::Node.new('type', acct[:type], ns)
95
- node << XML::Node.new('co-no', acct[:co_no], ns) unless acct[:co_no].nil? || acct[:co_no] == ''
96
- node << XML::Node.new('opt-out', acct[:opt_out], ns)
97
-
98
- node << create_account_address(acct[:addr], ns) unless acct[:addr].nil?
99
-
100
- acct[:contacts][0,3].each_with_index do |cont, i|
101
- c = XML::Node.new('contact', nil, ns)
102
- c['order'] = (i + 1).to_s # Enforce order 1-3
103
- node << (c << create_account_contact(cont))
104
- end
93
+ account_fields_xml(acct, node, ns)
105
94
  end
106
95
  else
107
96
  raise ArgumentError, "acct must be String or Hash"
108
97
  end
109
98
  end
110
99
 
111
- # Create account contact payload
112
- #
113
- # @param [Hash] cont Contact fields
114
- # @raise [ArgumentError] invalid contact fields
115
- # @raise [ArgumentError] name or email key is missing
116
- # @return [XML::Node]
117
- def create_account_contact(cont)
118
- raise ArgumentError, "cont must be a hash" unless cont.is_a?(Hash)
119
- raise ArgumentError, "Contact allowed keys are name, email, phone and mobile" unless (cont.keys - [:name, :email, :phone, :mobile]).empty?
120
- raise ArgumentError, "Contact requires name and email keys" unless cont.has_key?(:name) && cont.has_key?(:email)
121
-
122
- contact('create') do |node, ns|
123
- [:name, :phone, :mobile, :email].each do |key|
124
- next if cont[key].nil? || cont[key] == ''
125
- node << XML::Node.new(key, cont[key], ns)
126
- end
127
- end
128
- end
129
-
130
- # Create contact address
131
- #
132
- # @param [Hash] addr Address fields
133
- # @param [XML::Namespace] ns XML Namespace
134
- # @raise [ArgumentError] invalid keys in addr
135
- # @return [XML::Node]
136
- def create_account_address(addr, ns)
137
- raise ArgumentError, "addr must be a hash" unless addr.is_a?(Hash)
138
- raise ArgumentError, "ns must be an xml namespace" unless ns.is_a?(XML::Namespace)
139
- raise ArgumentError, "Address allowed keys are street, locality, city, county, postcode, country" unless (addr.keys - [:street, :locality, :city, :county, :postcode, :country]).empty?
140
-
141
- XML::Node.new('addr', nil, ns).tap do |a|
142
- [:street, :locality, :city, :county, :postcode, :country].each do |key|
143
- next if addr[key].nil? || addr[key] == ''
144
- a << XML::Node.new(key, addr[key], ns)
145
- end
146
- end
147
- end
148
-
149
100
  # Collects created account information
150
101
  #
151
102
  # @param [XML::Node] creData XML
@@ -74,19 +74,23 @@ module NominetEPP
74
74
  # @param [Hash] fields Fields to update on the domain
75
75
  # @return [XML::Node] +domain:update+ payload
76
76
  def update_domain(name, fields)
77
+ keys = [:auto_period, :account, :ns, :first_bill, :recur_bill,
78
+ :auto_bill, :next_bill, :renew_not_required, :notes, :reseller]
79
+
77
80
  domain('update') do |node, ns|
78
81
  node << XML::Node.new('name', name, ns)
79
- fields.each do |k,v|
82
+ keys.each do |k|
83
+ next if fields[k].nil?
80
84
  case k
81
85
  when :account
82
86
  node << (d_acct = XML::Node.new('account', nil, ns))
83
87
  d_acct << account('update') do |acct, a_ns|
84
- account_update_xml(v, acct, a_ns)
88
+ account_update_xml(fields[k], acct, a_ns)
85
89
  end
86
90
  when :ns
87
- node << domain_ns_xml(v, ns)
91
+ node << domain_ns_xml(fields[k], ns)
88
92
  else
89
- node << XML::Node.new(k.gsub('_', '-'), v, ns)
93
+ node << generic_field_to_xml(k, fields[k], ns) unless fields[k] == ''
90
94
  end
91
95
  end
92
96
  end
@@ -107,16 +111,9 @@ module NominetEPP
107
111
  # @param [String] roid Contact ID
108
112
  # @param [Hash] fields Fields to update on the contact
109
113
  # @raise [ArgumentError] invalid keys
110
- # @return [XML::Node] +contact:update+ payload
114
+ # @return [XML::Node] XML +contact:update+ element
111
115
  def update_contact(roid, fields = {})
112
- raise ArgumentError, "fields allowed keys name, email, phone, mobile" unless (fields.keys - [:name, :email, :phone, :mobile]).empty?
113
-
114
- contact('update') do |node, ns|
115
- node << XML::Node.new('roid', roid, ns)
116
- fields.each do |k,v|
117
- node << XML::Node.new(k, v, ns)
118
- end
119
- end
116
+ contact_to_xml(fields.merge(:roid => roid))
120
117
  end
121
118
 
122
119
  # Generate +host:update+ payload to modify a nameserver entry
data/nominet-epp.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{nominet-epp}
8
- s.version = "0.0.8"
8
+ s.version = "0.0.10"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Geoff Garside"]
12
- s.date = %q{2011-05-03}
12
+ s.date = %q{2011-05-04}
13
13
  s.description = %q{Client for communicating with the Nominet EPP}
14
14
  s.email = %q{geoff@geoffgarside.co.uk}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nominet-epp
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 8
10
- version: 0.0.8
9
+ - 10
10
+ version: 0.0.10
11
11
  platform: ruby
12
12
  authors:
13
13
  - Geoff Garside
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-03 00:00:00 Z
18
+ date: 2011-05-04 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: shoulda