Class: GandiV5::Domain

Inherits:
Object
  • Object
show all
Includes:
Data
Defined in:
lib/gandi_v5/domain.rb,
lib/gandi_v5/domain/tld.rb,
lib/gandi_v5/domain/dates.rb,
lib/gandi_v5/domain/contact.rb,
lib/gandi_v5/domain/contract.rb,
lib/gandi_v5/domain/live_dns.rb,
lib/gandi_v5/domain/auto_renew.rb,
lib/gandi_v5/domain/availability.rb,
lib/gandi_v5/domain/sharing_space.rb,
lib/gandi_v5/domain/availability/tax.rb,
lib/gandi_v5/domain/renewal_information.rb,
lib/gandi_v5/domain/restore_information.rb,
lib/gandi_v5/domain/availability/product.rb,
lib/gandi_v5/domain/availability/product/price.rb,
lib/gandi_v5/domain/availability/product/period.rb

Overview

Gandi Domain Management API.

Defined Under Namespace

Classes: AutoRenew, Availability, Contact, Contract, Dates, LiveDNS, RenewalInformation, RestoreInformation, SharingSpace, TLD

Constant Summary collapse

SERVICES =
{
  gandidns: 'GandiDNS',
  gandilivedns: 'LiveDNS',
  dnssec: 'DNSSEC',
  certificate: 'SSL Certificate',
  paas: 'PAAS',
  redirection: 'Redirection',
  gandimail: 'GandiMail',
  packmail: 'PackMail',
  blog: 'Blog',
  hosting: 'Hosting',
  site: 'Site',
  mailboxv2: 'MailboxV2'
}.freeze
STATUSES =
{
  clientHold: 'clientHold',
  clientUpdateProhibited: 'clientUpdateProhibited',
  clientTransferProhibited: 'clientTransferProhibited',
  clientDeleteProhibited: 'clientDeleteProhibited',
  clientRenewProhibited: 'clientRenewProhibited',
  serverHold: 'serverHold',
  pendingTransfer: 'pendingTransfer',
  serverTransferProhibited: 'serverTransferProhibited'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Data

#from_gandi, included, #initialize, #to_gandi, #to_h, #values_at

Instance Attribute Details

#auth_infonil, String (readonly)

Returns:

  • (nil, String)


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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/gandi_v5/domain.rb', line 52

class Domain
  include GandiV5::Data

  SERVICES = {
    gandidns: 'GandiDNS',
    gandilivedns: 'LiveDNS',
    dnssec: 'DNSSEC',
    certificate: 'SSL Certificate',
    paas: 'PAAS',
    redirection: 'Redirection',
    gandimail: 'GandiMail',
    packmail: 'PackMail',
    blog: 'Blog',
    hosting: 'Hosting',
    site: 'Site',
    mailboxv2: 'MailboxV2'
  }.freeze

  STATUSES = {
    clientHold: 'clientHold',
    clientUpdateProhibited: 'clientUpdateProhibited',
    clientTransferProhibited: 'clientTransferProhibited',
    clientDeleteProhibited: 'clientDeleteProhibited',
    clientRenewProhibited: 'clientRenewProhibited',
    serverHold: 'serverHold',
    pendingTransfer: 'pendingTransfer',
    serverTransferProhibited: 'serverTransferProhibited'
  }.freeze

  CONTACTS_CONVERTER = lambda { |hash|
    break {} if hash.nil?

    hash = hash.transform_keys(&:to_sym)
               .transform_values { |value| GandiV5::Domain::Contact.from_gandi value }

    hash.define_singleton_method(:owner) { send :'[]', :owner }
    hash.define_singleton_method(:admin) { send :'[]', :admin }
    hash.define_singleton_method(:bill) { send :'[]', :bill }
    hash.define_singleton_method(:tech) { send :'[]', :tech }

    hash
  }
  private_constant :CONTACTS_CONVERTER

  members :fqdn, :fqdn_unicode, :tld, :can_tld_lock, :tags, :owner, :domain_owner
  member :sharing_uuid, gandi_key: 'sharing_id'
  member :name_servers, gandi_key: 'nameservers'
  member :auth_info, gandi_key: 'authinfo'
  member :organisation_owner, gandi_key: 'orga_owner'
  member :uuid, gandi_key: 'id'

  member(
    :dates,
    converter: GandiV5::Domain::Dates
  )
  member(
    :sharing_space,
    gandi_key: 'sharing_space',
    converter: GandiV5::Domain::SharingSpace
  )

  member(
    :auto_renew,
    gandi_key: 'autorenew',
    converter: GandiV5::Data::Converter.new(from_gandi: lambda { |hash|
      break nil if hash.nil?
      break nil if hash.eql?(true)
      break GandiV5::Domain::AutoRenew.from_gandi('enabled' => false) if hash.eql?(false)

      GandiV5::Domain::AutoRenew.from_gandi hash
    })
  )

  member(
    :contacts,
    converter: GandiV5::Data::Converter.new(from_gandi: CONTACTS_CONVERTER)
  )

  member(
    :name_server,
    gandi_key: 'nameserver',
    converter: GandiV5::Data::Converter.new(from_gandi: ->(hash) { hash&.[]('current')&.to_sym })
  )
  member :services, converter: GandiV5::Data::Converter::Symbol, array: true
  member :status, converter: GandiV5::Data::Converter::Symbol, array: true
  member :trustee_roles, converter: GandiV5::Data::Converter::Symbol, array: true

  alias domain_uuid uuid

  # rubocop:disable Style/AsciiComments
  # Returns the string representation of the domain.
  # @return [String] e.g. "example.com", "😀.com (xn--e28h.uk.com)"
  # rubocop:enable Style/AsciiComments
  def to_s
    string = fqdn_unicode
    string += " (#{fqdn})" unless fqdn == fqdn_unicode
    string
  end

  # Contacts for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def contacts
    @contacts ||= fetch_contacts
  end

  # Requery Gandi for the domain's contacts.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_contacts
    _response, data = GandiV5.get url('contacts')
    self.contacts = data.transform_keys(&:to_sym)
    CONTACTS_CONVERTER.call contacts
  end

  # Update some of the domain's contacts.
  # @see https://api.gandi.net/docs/domains#patch-v5-domain-domains-domain-contacts
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new administrative contact.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new billing contact.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new technical contact.
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_contacts(admin: nil, bill: nil, tech: nil)
    body = {
      admin: admin.respond_to?(:to_gandi) ? admin.to_gandi : admin,
      bill: bill.respond_to?(:to_gandi) ? bill.to_gandi : bill,
      tech: tech.respond_to?(:to_gandi) ? tech.to_gandi : tech
    }.reject { |_k, v| v.nil? }.to_json

    GandiV5.patch url('contacts'), body
    fetch_contacts
  end

  # Renewal information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renewal_information
    @renewal_information ||= fetch_renewal_information
  end

  # Requery Gandi for the domain's renewal information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_renewal_information
    _response, data = GandiV5.get url('renew')
    data = data['renew'].merge('contracts' => data['contracts'])
    @renewal_information = GandiV5::Domain::RenewalInformation.from_gandi data
  end

  # Renew domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-renew
  # @param duration [Integer, #to_s] how long to renew for (in years).
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renew_for(duration = 1)
    body = { duration: duration }.to_json
    _response, data = GandiV5.post url('renew'), body
    data['message']
  end

  # Restoration information for the domain.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore_information
    @restore_information ||= fetch_restore_information
  end

  # Requery Gandi for the domain's restore information.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_restore_information
    _response, data = GandiV5.get url('restore')
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi data
  rescue RestClient::NotFound
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi restorable: false
  end

  # Restore an expired domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-restore
  # @return [String] The confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore
    _response, data = GandiV5.post url('restore'), '{}'
    data['message']
  end

  # Requery Gandi fo this domain's information.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def refresh
    _response, data = GandiV5.get url
    from_gandi data
    auto_renew.domain = self
  end

  # Get the price for renewing this domain.
  # @param currency [String] the currency to get the price in (e.g. GBP)
  # @param period [Integer] the number of year(s) renewal to get the price for
  # @return [GandiV5::Domain::Availability::Product::Price]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def renewal_price(currency: 'GBP', period: 1)
    arguments = { processes: [:renew], currency: currency, period: period }
    GandiV5::Domain::Availability.fetch(fqdn, **arguments)
                                 .products.first
                                 .prices.first
  end

  # LiveDNS information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def livedns
    @livedns ||= fetch_livedns
  end

  # Requery Gandi for the domain's LiveDNS information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_livedns
    _response, data = GandiV5.get url('livedns')
    @livedns = GandiV5::Domain::LiveDNS.from_gandi data
    @name_server = @livedns.current
    @name_servers = @livedns.name_servers
    @livedns
  end

  # Enable LiveDNS for the domain.
  # If you want to disable LiveDNS, change the nameservers.
  # Please note that if the domain is on the classic Gandi DNS,
  # this will also perform a copy of all existing records immediately afterwards.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-livedns
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def enable_livedns
    _response, data = GandiV5.post url('livedns')
    data['message']
  end

  # Name servers for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def name_servers
    @name_servers ||= fetch_name_servers
  end

  # Requery Gandi for the domain's name servers.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_name_servers
    _response, data = GandiV5.get url('nameservers')
    @name_servers = data
  end

  # Update name servers in Gandi.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-nameservers
  # @param nameservers [Array<String>] the name servers to use.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_name_servers(nameservers)
    _response, data = GandiV5.put url('nameservers'), { 'nameservers' => nameservers }.to_json
    @name_servers = nameservers
    data['message']
  end

  # Glue records for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_records
    @glue_records ||= fetch_glue_records
  end

  # Requery Gandi for the domain's glue records.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_glue_records
    _response, data = GandiV5.get url('hosts')
    @glue_records = data.map { |record| record.values_at('name', 'ips') }.to_h
  end

  # Add a new glue record to the domain in Gandi.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def add_glue_record(name, *ips)
    _response, data = GandiV5.post url('hosts'), { 'name' => name, 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Get a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @return [nil] name was not found
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_record(name)
    records = @glue_records.key?(name) ? @glue_records : fetch_glue_records
    records.key?(name) ? records.select { |k, _v| k == name } : nil
  end

  # Update a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_glue_record(name, *ips)
    _response, data = GandiV5.put url('hosts', name), { 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Delete a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#delete-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def delete_glue_record(name)
    _response, data = GandiV5.delete url('hosts', name)
    @glue_records ||= {}
    @glue_records.delete(name)
    data['message']
  end

  # @see GandiV5::Email::Mailbox.list
  def mailboxes(**params)
    GandiV5::Email::Mailbox.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Slot.list
  def mailbox_slots(**params)
    GandiV5::Email::Slot.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Forward.list
  def email_forwards(**params)
    GandiV5::Email::Forward.list(**params, fqdn: fqdn)
  end

  # Create (register) a new domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains
  # @param fqdn [String, #to_s] the fully qualified domain name to create.
  # @param dry_run [Boolean]
  #   whether the details should be checked instead of actually creating the domain.
  # @param sharing_id [String] either:
  #   * nil (default) - nothing special happens
  #   * an organization ID - pay using another organization
  #     (you need to have billing permissions on the organization
  #     and use the same organization name for the domain name's owner).
  #     The invoice will be edited using this organization's information.
  #   * a reseller ID - buy a domain for a customer using a reseller account
  #     (you need to have billing permissions on the reseller organization
  #     and have your customer's information for the owner).
  #     The invoice will be edited using the reseller organization's information.
  # @param owner [GandiV5::Domain::Contact, #to_gandi, #to_h] (required)
  #   the owner of the new domain.
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the administrative contact for the new domain.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the billing contact for the new domain.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the technical contact for the new domain.
  # @param claims [String] (optional) unknown - not documented at Gandi.
  # @param currency ["EUR", "USD", "GBP", "TWD", "CNY"] (optional)
  #   the currency you wish to be charged in.
  # @param duration [Integer] (optional, default 1, minimum 1 maximum 10)
  #   how many years to register for.
  # @param enforce_premium [Boolean] (optional)
  #   must be set to true if the domain is a premium domain.
  # @param extra_parameters [Hash, #to_gandi, #to_json] (optional)
  #   unknown - not documented at Gandi.
  # @param lang [String] (optional)
  #   ISO-639-2 language code of the domain, required for some IDN domains.
  # @param nameserver_ips [Hash<String => Array<String>>, #to_gandi, #to_json] (optional)
  #   For glue records only - dictionnary associating a nameserver to a list of IP addresses.
  # @param nameservers [Array<String>, #to_gandi, #to_json] (optional)
  #   List of nameservers. Gandi's LiveDNS nameservers are used if omitted..
  # @param price [Numeric, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param resellee_id [String, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param smd [String, #to_gandi, #to_json] (optional)
  #   Contents of a Signed Mark Data file (used for newgtld sunrises, tld_period must be sunrise).
  # @param tld_period ["sunrise", "landrush", "eap1", "eap2", "eap3", "eap4", "eap5", "eap6",
  #   "eap7", "eap8", "eap9", "golive", #to_gandi, #to_json] (optional)
  #   @see https://docs.gandi.net/en/domain_names/register/new_gtld.html
  # @return [GandiV5::Domain] the created domain
  # @return [Hash] if doing a dry run, you get what Gandi returns
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def self.create(fqdn, dry_run: false, sharing_id: nil, **params)
    fail ArgumentError, 'missing keyword: owner' unless params.key?(:owner)

    body = params.merge(fqdn: fqdn)
                 .transform_values { |val| val.respond_to?(:to_gandi) ? val.to_gandi : val }
                 .to_json
    url_ = sharing_id ? "#{url}?sharing_id=#{sharing_id}" : url

    response, data = GandiV5.post(url_, body, 'Dry-Run': dry_run ? 1 : 0)
    dry_run ? data : fetch(response.headers[:location].split('/').last)
  end

  # Get information on a domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain
  # @param fqdn [String, #to_s] the fully qualified domain name to fetch.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.fetch(fqdn)
    _response, data = GandiV5.get url(fqdn)
    domain = from_gandi data
    domain.auto_renew.domain = fqdn
    domain
  end

  # List domains.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains
  # @param page [#each<Integer, #to_s>] the page(s) of results to retrieve.
  #   If page is not provided keep querying until an empty list is returned.
  #   If page responds to .each then iterate until an empty list is returned.
  # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page.
  # @param fqdn [String, #to_s] (optional)
  #   filters the list by domain name, with optional patterns.
  #   e.g. "example.net", "example.*", "*ample.com"
  # @param sort_by [String, #to_s] (optional default "fqdn") how to sort the list.
  # @param tld [String, #to_s] (optional) used to filter by just the top level domain.
  # @return [Array<GandiV5::Domain>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.list(page: (1..), per_page: 100, **params)
    page = [page.to_i] unless page.respond_to?(:each)

    domains = []
    page.each do |page_number|
      _resp, data = GandiV5.get url, params: params.merge(page: page_number, per_page: per_page)
      break if data.empty?

      domains += data.map { |domain| from_gandi domain }
      break if data.count < per_page
    end
    domains
  end

  private

  def url(*extra)
    "#{BASE}domain/domains/" +
      CGI.escape(fqdn) +
      (extra.empty? ? '' : "/#{extra.join('/')}")
  end

  def self.url(fqdn = nil)
    "#{BASE}domain/domains" +
      (fqdn ? "/#{CGI.escape fqdn}" : '')
  end
  private_class_method :url
end

#auto_renewGandiV5::Domain::AutoRenew (readonly)



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/gandi_v5/domain.rb', line 52

class Domain
  include GandiV5::Data

  SERVICES = {
    gandidns: 'GandiDNS',
    gandilivedns: 'LiveDNS',
    dnssec: 'DNSSEC',
    certificate: 'SSL Certificate',
    paas: 'PAAS',
    redirection: 'Redirection',
    gandimail: 'GandiMail',
    packmail: 'PackMail',
    blog: 'Blog',
    hosting: 'Hosting',
    site: 'Site',
    mailboxv2: 'MailboxV2'
  }.freeze

  STATUSES = {
    clientHold: 'clientHold',
    clientUpdateProhibited: 'clientUpdateProhibited',
    clientTransferProhibited: 'clientTransferProhibited',
    clientDeleteProhibited: 'clientDeleteProhibited',
    clientRenewProhibited: 'clientRenewProhibited',
    serverHold: 'serverHold',
    pendingTransfer: 'pendingTransfer',
    serverTransferProhibited: 'serverTransferProhibited'
  }.freeze

  CONTACTS_CONVERTER = lambda { |hash|
    break {} if hash.nil?

    hash = hash.transform_keys(&:to_sym)
               .transform_values { |value| GandiV5::Domain::Contact.from_gandi value }

    hash.define_singleton_method(:owner) { send :'[]', :owner }
    hash.define_singleton_method(:admin) { send :'[]', :admin }
    hash.define_singleton_method(:bill) { send :'[]', :bill }
    hash.define_singleton_method(:tech) { send :'[]', :tech }

    hash
  }
  private_constant :CONTACTS_CONVERTER

  members :fqdn, :fqdn_unicode, :tld, :can_tld_lock, :tags, :owner, :domain_owner
  member :sharing_uuid, gandi_key: 'sharing_id'
  member :name_servers, gandi_key: 'nameservers'
  member :auth_info, gandi_key: 'authinfo'
  member :organisation_owner, gandi_key: 'orga_owner'
  member :uuid, gandi_key: 'id'

  member(
    :dates,
    converter: GandiV5::Domain::Dates
  )
  member(
    :sharing_space,
    gandi_key: 'sharing_space',
    converter: GandiV5::Domain::SharingSpace
  )

  member(
    :auto_renew,
    gandi_key: 'autorenew',
    converter: GandiV5::Data::Converter.new(from_gandi: lambda { |hash|
      break nil if hash.nil?
      break nil if hash.eql?(true)
      break GandiV5::Domain::AutoRenew.from_gandi('enabled' => false) if hash.eql?(false)

      GandiV5::Domain::AutoRenew.from_gandi hash
    })
  )

  member(
    :contacts,
    converter: GandiV5::Data::Converter.new(from_gandi: CONTACTS_CONVERTER)
  )

  member(
    :name_server,
    gandi_key: 'nameserver',
    converter: GandiV5::Data::Converter.new(from_gandi: ->(hash) { hash&.[]('current')&.to_sym })
  )
  member :services, converter: GandiV5::Data::Converter::Symbol, array: true
  member :status, converter: GandiV5::Data::Converter::Symbol, array: true
  member :trustee_roles, converter: GandiV5::Data::Converter::Symbol, array: true

  alias domain_uuid uuid

  # rubocop:disable Style/AsciiComments
  # Returns the string representation of the domain.
  # @return [String] e.g. "example.com", "😀.com (xn--e28h.uk.com)"
  # rubocop:enable Style/AsciiComments
  def to_s
    string = fqdn_unicode
    string += " (#{fqdn})" unless fqdn == fqdn_unicode
    string
  end

  # Contacts for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def contacts
    @contacts ||= fetch_contacts
  end

  # Requery Gandi for the domain's contacts.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_contacts
    _response, data = GandiV5.get url('contacts')
    self.contacts = data.transform_keys(&:to_sym)
    CONTACTS_CONVERTER.call contacts
  end

  # Update some of the domain's contacts.
  # @see https://api.gandi.net/docs/domains#patch-v5-domain-domains-domain-contacts
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new administrative contact.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new billing contact.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new technical contact.
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_contacts(admin: nil, bill: nil, tech: nil)
    body = {
      admin: admin.respond_to?(:to_gandi) ? admin.to_gandi : admin,
      bill: bill.respond_to?(:to_gandi) ? bill.to_gandi : bill,
      tech: tech.respond_to?(:to_gandi) ? tech.to_gandi : tech
    }.reject { |_k, v| v.nil? }.to_json

    GandiV5.patch url('contacts'), body
    fetch_contacts
  end

  # Renewal information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renewal_information
    @renewal_information ||= fetch_renewal_information
  end

  # Requery Gandi for the domain's renewal information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_renewal_information
    _response, data = GandiV5.get url('renew')
    data = data['renew'].merge('contracts' => data['contracts'])
    @renewal_information = GandiV5::Domain::RenewalInformation.from_gandi data
  end

  # Renew domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-renew
  # @param duration [Integer, #to_s] how long to renew for (in years).
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renew_for(duration = 1)
    body = { duration: duration }.to_json
    _response, data = GandiV5.post url('renew'), body
    data['message']
  end

  # Restoration information for the domain.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore_information
    @restore_information ||= fetch_restore_information
  end

  # Requery Gandi for the domain's restore information.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_restore_information
    _response, data = GandiV5.get url('restore')
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi data
  rescue RestClient::NotFound
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi restorable: false
  end

  # Restore an expired domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-restore
  # @return [String] The confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore
    _response, data = GandiV5.post url('restore'), '{}'
    data['message']
  end

  # Requery Gandi fo this domain's information.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def refresh
    _response, data = GandiV5.get url
    from_gandi data
    auto_renew.domain = self
  end

  # Get the price for renewing this domain.
  # @param currency [String] the currency to get the price in (e.g. GBP)
  # @param period [Integer] the number of year(s) renewal to get the price for
  # @return [GandiV5::Domain::Availability::Product::Price]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def renewal_price(currency: 'GBP', period: 1)
    arguments = { processes: [:renew], currency: currency, period: period }
    GandiV5::Domain::Availability.fetch(fqdn, **arguments)
                                 .products.first
                                 .prices.first
  end

  # LiveDNS information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def livedns
    @livedns ||= fetch_livedns
  end

  # Requery Gandi for the domain's LiveDNS information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_livedns
    _response, data = GandiV5.get url('livedns')
    @livedns = GandiV5::Domain::LiveDNS.from_gandi data
    @name_server = @livedns.current
    @name_servers = @livedns.name_servers
    @livedns
  end

  # Enable LiveDNS for the domain.
  # If you want to disable LiveDNS, change the nameservers.
  # Please note that if the domain is on the classic Gandi DNS,
  # this will also perform a copy of all existing records immediately afterwards.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-livedns
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def enable_livedns
    _response, data = GandiV5.post url('livedns')
    data['message']
  end

  # Name servers for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def name_servers
    @name_servers ||= fetch_name_servers
  end

  # Requery Gandi for the domain's name servers.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_name_servers
    _response, data = GandiV5.get url('nameservers')
    @name_servers = data
  end

  # Update name servers in Gandi.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-nameservers
  # @param nameservers [Array<String>] the name servers to use.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_name_servers(nameservers)
    _response, data = GandiV5.put url('nameservers'), { 'nameservers' => nameservers }.to_json
    @name_servers = nameservers
    data['message']
  end

  # Glue records for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_records
    @glue_records ||= fetch_glue_records
  end

  # Requery Gandi for the domain's glue records.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_glue_records
    _response, data = GandiV5.get url('hosts')
    @glue_records = data.map { |record| record.values_at('name', 'ips') }.to_h
  end

  # Add a new glue record to the domain in Gandi.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def add_glue_record(name, *ips)
    _response, data = GandiV5.post url('hosts'), { 'name' => name, 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Get a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @return [nil] name was not found
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_record(name)
    records = @glue_records.key?(name) ? @glue_records : fetch_glue_records
    records.key?(name) ? records.select { |k, _v| k == name } : nil
  end

  # Update a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_glue_record(name, *ips)
    _response, data = GandiV5.put url('hosts', name), { 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Delete a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#delete-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def delete_glue_record(name)
    _response, data = GandiV5.delete url('hosts', name)
    @glue_records ||= {}
    @glue_records.delete(name)
    data['message']
  end

  # @see GandiV5::Email::Mailbox.list
  def mailboxes(**params)
    GandiV5::Email::Mailbox.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Slot.list
  def mailbox_slots(**params)
    GandiV5::Email::Slot.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Forward.list
  def email_forwards(**params)
    GandiV5::Email::Forward.list(**params, fqdn: fqdn)
  end

  # Create (register) a new domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains
  # @param fqdn [String, #to_s] the fully qualified domain name to create.
  # @param dry_run [Boolean]
  #   whether the details should be checked instead of actually creating the domain.
  # @param sharing_id [String] either:
  #   * nil (default) - nothing special happens
  #   * an organization ID - pay using another organization
  #     (you need to have billing permissions on the organization
  #     and use the same organization name for the domain name's owner).
  #     The invoice will be edited using this organization's information.
  #   * a reseller ID - buy a domain for a customer using a reseller account
  #     (you need to have billing permissions on the reseller organization
  #     and have your customer's information for the owner).
  #     The invoice will be edited using the reseller organization's information.
  # @param owner [GandiV5::Domain::Contact, #to_gandi, #to_h] (required)
  #   the owner of the new domain.
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the administrative contact for the new domain.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the billing contact for the new domain.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the technical contact for the new domain.
  # @param claims [String] (optional) unknown - not documented at Gandi.
  # @param currency ["EUR", "USD", "GBP", "TWD", "CNY"] (optional)
  #   the currency you wish to be charged in.
  # @param duration [Integer] (optional, default 1, minimum 1 maximum 10)
  #   how many years to register for.
  # @param enforce_premium [Boolean] (optional)
  #   must be set to true if the domain is a premium domain.
  # @param extra_parameters [Hash, #to_gandi, #to_json] (optional)
  #   unknown - not documented at Gandi.
  # @param lang [String] (optional)
  #   ISO-639-2 language code of the domain, required for some IDN domains.
  # @param nameserver_ips [Hash<String => Array<String>>, #to_gandi, #to_json] (optional)
  #   For glue records only - dictionnary associating a nameserver to a list of IP addresses.
  # @param nameservers [Array<String>, #to_gandi, #to_json] (optional)
  #   List of nameservers. Gandi's LiveDNS nameservers are used if omitted..
  # @param price [Numeric, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param resellee_id [String, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param smd [String, #to_gandi, #to_json] (optional)
  #   Contents of a Signed Mark Data file (used for newgtld sunrises, tld_period must be sunrise).
  # @param tld_period ["sunrise", "landrush", "eap1", "eap2", "eap3", "eap4", "eap5", "eap6",
  #   "eap7", "eap8", "eap9", "golive", #to_gandi, #to_json] (optional)
  #   @see https://docs.gandi.net/en/domain_names/register/new_gtld.html
  # @return [GandiV5::Domain] the created domain
  # @return [Hash] if doing a dry run, you get what Gandi returns
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def self.create(fqdn, dry_run: false, sharing_id: nil, **params)
    fail ArgumentError, 'missing keyword: owner' unless params.key?(:owner)

    body = params.merge(fqdn: fqdn)
                 .transform_values { |val| val.respond_to?(:to_gandi) ? val.to_gandi : val }
                 .to_json
    url_ = sharing_id ? "#{url}?sharing_id=#{sharing_id}" : url

    response, data = GandiV5.post(url_, body, 'Dry-Run': dry_run ? 1 : 0)
    dry_run ? data : fetch(response.headers[:location].split('/').last)
  end

  # Get information on a domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain
  # @param fqdn [String, #to_s] the fully qualified domain name to fetch.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.fetch(fqdn)
    _response, data = GandiV5.get url(fqdn)
    domain = from_gandi data
    domain.auto_renew.domain = fqdn
    domain
  end

  # List domains.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains
  # @param page [#each<Integer, #to_s>] the page(s) of results to retrieve.
  #   If page is not provided keep querying until an empty list is returned.
  #   If page responds to .each then iterate until an empty list is returned.
  # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page.
  # @param fqdn [String, #to_s] (optional)
  #   filters the list by domain name, with optional patterns.
  #   e.g. "example.net", "example.*", "*ample.com"
  # @param sort_by [String, #to_s] (optional default "fqdn") how to sort the list.
  # @param tld [String, #to_s] (optional) used to filter by just the top level domain.
  # @return [Array<GandiV5::Domain>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.list(page: (1..), per_page: 100, **params)
    page = [page.to_i] unless page.respond_to?(:each)

    domains = []
    page.each do |page_number|
      _resp, data = GandiV5.get url, params: params.merge(page: page_number, per_page: per_page)
      break if data.empty?

      domains += data.map { |domain| from_gandi domain }
      break if data.count < per_page
    end
    domains
  end

  private

  def url(*extra)
    "#{BASE}domain/domains/" +
      CGI.escape(fqdn) +
      (extra.empty? ? '' : "/#{extra.join('/')}")
  end

  def self.url(fqdn = nil)
    "#{BASE}domain/domains" +
      (fqdn ? "/#{CGI.escape fqdn}" : '')
  end
  private_class_method :url
end

#can_tld_lockBoolean (readonly)

Returns:

  • (Boolean)


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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/gandi_v5/domain.rb', line 52

class Domain
  include GandiV5::Data

  SERVICES = {
    gandidns: 'GandiDNS',
    gandilivedns: 'LiveDNS',
    dnssec: 'DNSSEC',
    certificate: 'SSL Certificate',
    paas: 'PAAS',
    redirection: 'Redirection',
    gandimail: 'GandiMail',
    packmail: 'PackMail',
    blog: 'Blog',
    hosting: 'Hosting',
    site: 'Site',
    mailboxv2: 'MailboxV2'
  }.freeze

  STATUSES = {
    clientHold: 'clientHold',
    clientUpdateProhibited: 'clientUpdateProhibited',
    clientTransferProhibited: 'clientTransferProhibited',
    clientDeleteProhibited: 'clientDeleteProhibited',
    clientRenewProhibited: 'clientRenewProhibited',
    serverHold: 'serverHold',
    pendingTransfer: 'pendingTransfer',
    serverTransferProhibited: 'serverTransferProhibited'
  }.freeze

  CONTACTS_CONVERTER = lambda { |hash|
    break {} if hash.nil?

    hash = hash.transform_keys(&:to_sym)
               .transform_values { |value| GandiV5::Domain::Contact.from_gandi value }

    hash.define_singleton_method(:owner) { send :'[]', :owner }
    hash.define_singleton_method(:admin) { send :'[]', :admin }
    hash.define_singleton_method(:bill) { send :'[]', :bill }
    hash.define_singleton_method(:tech) { send :'[]', :tech }

    hash
  }
  private_constant :CONTACTS_CONVERTER

  members :fqdn, :fqdn_unicode, :tld, :can_tld_lock, :tags, :owner, :domain_owner
  member :sharing_uuid, gandi_key: 'sharing_id'
  member :name_servers, gandi_key: 'nameservers'
  member :auth_info, gandi_key: 'authinfo'
  member :organisation_owner, gandi_key: 'orga_owner'
  member :uuid, gandi_key: 'id'

  member(
    :dates,
    converter: GandiV5::Domain::Dates
  )
  member(
    :sharing_space,
    gandi_key: 'sharing_space',
    converter: GandiV5::Domain::SharingSpace
  )

  member(
    :auto_renew,
    gandi_key: 'autorenew',
    converter: GandiV5::Data::Converter.new(from_gandi: lambda { |hash|
      break nil if hash.nil?
      break nil if hash.eql?(true)
      break GandiV5::Domain::AutoRenew.from_gandi('enabled' => false) if hash.eql?(false)

      GandiV5::Domain::AutoRenew.from_gandi hash
    })
  )

  member(
    :contacts,
    converter: GandiV5::Data::Converter.new(from_gandi: CONTACTS_CONVERTER)
  )

  member(
    :name_server,
    gandi_key: 'nameserver',
    converter: GandiV5::Data::Converter.new(from_gandi: ->(hash) { hash&.[]('current')&.to_sym })
  )
  member :services, converter: GandiV5::Data::Converter::Symbol, array: true
  member :status, converter: GandiV5::Data::Converter::Symbol, array: true
  member :trustee_roles, converter: GandiV5::Data::Converter::Symbol, array: true

  alias domain_uuid uuid

  # rubocop:disable Style/AsciiComments
  # Returns the string representation of the domain.
  # @return [String] e.g. "example.com", "😀.com (xn--e28h.uk.com)"
  # rubocop:enable Style/AsciiComments
  def to_s
    string = fqdn_unicode
    string += " (#{fqdn})" unless fqdn == fqdn_unicode
    string
  end

  # Contacts for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def contacts
    @contacts ||= fetch_contacts
  end

  # Requery Gandi for the domain's contacts.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_contacts
    _response, data = GandiV5.get url('contacts')
    self.contacts = data.transform_keys(&:to_sym)
    CONTACTS_CONVERTER.call contacts
  end

  # Update some of the domain's contacts.
  # @see https://api.gandi.net/docs/domains#patch-v5-domain-domains-domain-contacts
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new administrative contact.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new billing contact.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new technical contact.
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_contacts(admin: nil, bill: nil, tech: nil)
    body = {
      admin: admin.respond_to?(:to_gandi) ? admin.to_gandi : admin,
      bill: bill.respond_to?(:to_gandi) ? bill.to_gandi : bill,
      tech: tech.respond_to?(:to_gandi) ? tech.to_gandi : tech
    }.reject { |_k, v| v.nil? }.to_json

    GandiV5.patch url('contacts'), body
    fetch_contacts
  end

  # Renewal information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renewal_information
    @renewal_information ||= fetch_renewal_information
  end

  # Requery Gandi for the domain's renewal information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_renewal_information
    _response, data = GandiV5.get url('renew')
    data = data['renew'].merge('contracts' => data['contracts'])
    @renewal_information = GandiV5::Domain::RenewalInformation.from_gandi data
  end

  # Renew domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-renew
  # @param duration [Integer, #to_s] how long to renew for (in years).
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renew_for(duration = 1)
    body = { duration: duration }.to_json
    _response, data = GandiV5.post url('renew'), body
    data['message']
  end

  # Restoration information for the domain.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore_information
    @restore_information ||= fetch_restore_information
  end

  # Requery Gandi for the domain's restore information.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_restore_information
    _response, data = GandiV5.get url('restore')
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi data
  rescue RestClient::NotFound
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi restorable: false
  end

  # Restore an expired domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-restore
  # @return [String] The confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore
    _response, data = GandiV5.post url('restore'), '{}'
    data['message']
  end

  # Requery Gandi fo this domain's information.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def refresh
    _response, data = GandiV5.get url
    from_gandi data
    auto_renew.domain = self
  end

  # Get the price for renewing this domain.
  # @param currency [String] the currency to get the price in (e.g. GBP)
  # @param period [Integer] the number of year(s) renewal to get the price for
  # @return [GandiV5::Domain::Availability::Product::Price]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def renewal_price(currency: 'GBP', period: 1)
    arguments = { processes: [:renew], currency: currency, period: period }
    GandiV5::Domain::Availability.fetch(fqdn, **arguments)
                                 .products.first
                                 .prices.first
  end

  # LiveDNS information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def livedns
    @livedns ||= fetch_livedns
  end

  # Requery Gandi for the domain's LiveDNS information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_livedns
    _response, data = GandiV5.get url('livedns')
    @livedns = GandiV5::Domain::LiveDNS.from_gandi data
    @name_server = @livedns.current
    @name_servers = @livedns.name_servers
    @livedns
  end

  # Enable LiveDNS for the domain.
  # If you want to disable LiveDNS, change the nameservers.
  # Please note that if the domain is on the classic Gandi DNS,
  # this will also perform a copy of all existing records immediately afterwards.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-livedns
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def enable_livedns
    _response, data = GandiV5.post url('livedns')
    data['message']
  end

  # Name servers for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def name_servers
    @name_servers ||= fetch_name_servers
  end

  # Requery Gandi for the domain's name servers.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_name_servers
    _response, data = GandiV5.get url('nameservers')
    @name_servers = data
  end

  # Update name servers in Gandi.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-nameservers
  # @param nameservers [Array<String>] the name servers to use.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_name_servers(nameservers)
    _response, data = GandiV5.put url('nameservers'), { 'nameservers' => nameservers }.to_json
    @name_servers = nameservers
    data['message']
  end

  # Glue records for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_records
    @glue_records ||= fetch_glue_records
  end

  # Requery Gandi for the domain's glue records.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_glue_records
    _response, data = GandiV5.get url('hosts')
    @glue_records = data.map { |record| record.values_at('name', 'ips') }.to_h
  end

  # Add a new glue record to the domain in Gandi.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def add_glue_record(name, *ips)
    _response, data = GandiV5.post url('hosts'), { 'name' => name, 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Get a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @return [nil] name was not found
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_record(name)
    records = @glue_records.key?(name) ? @glue_records : fetch_glue_records
    records.key?(name) ? records.select { |k, _v| k == name } : nil
  end

  # Update a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_glue_record(name, *ips)
    _response, data = GandiV5.put url('hosts', name), { 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Delete a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#delete-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def delete_glue_record(name)
    _response, data = GandiV5.delete url('hosts', name)
    @glue_records ||= {}
    @glue_records.delete(name)
    data['message']
  end

  # @see GandiV5::Email::Mailbox.list
  def mailboxes(**params)
    GandiV5::Email::Mailbox.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Slot.list
  def mailbox_slots(**params)
    GandiV5::Email::Slot.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Forward.list
  def email_forwards(**params)
    GandiV5::Email::Forward.list(**params, fqdn: fqdn)
  end

  # Create (register) a new domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains
  # @param fqdn [String, #to_s] the fully qualified domain name to create.
  # @param dry_run [Boolean]
  #   whether the details should be checked instead of actually creating the domain.
  # @param sharing_id [String] either:
  #   * nil (default) - nothing special happens
  #   * an organization ID - pay using another organization
  #     (you need to have billing permissions on the organization
  #     and use the same organization name for the domain name's owner).
  #     The invoice will be edited using this organization's information.
  #   * a reseller ID - buy a domain for a customer using a reseller account
  #     (you need to have billing permissions on the reseller organization
  #     and have your customer's information for the owner).
  #     The invoice will be edited using the reseller organization's information.
  # @param owner [GandiV5::Domain::Contact, #to_gandi, #to_h] (required)
  #   the owner of the new domain.
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the administrative contact for the new domain.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the billing contact for the new domain.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the technical contact for the new domain.
  # @param claims [String] (optional) unknown - not documented at Gandi.
  # @param currency ["EUR", "USD", "GBP", "TWD", "CNY"] (optional)
  #   the currency you wish to be charged in.
  # @param duration [Integer] (optional, default 1, minimum 1 maximum 10)
  #   how many years to register for.
  # @param enforce_premium [Boolean] (optional)
  #   must be set to true if the domain is a premium domain.
  # @param extra_parameters [Hash, #to_gandi, #to_json] (optional)
  #   unknown - not documented at Gandi.
  # @param lang [String] (optional)
  #   ISO-639-2 language code of the domain, required for some IDN domains.
  # @param nameserver_ips [Hash<String => Array<String>>, #to_gandi, #to_json] (optional)
  #   For glue records only - dictionnary associating a nameserver to a list of IP addresses.
  # @param nameservers [Array<String>, #to_gandi, #to_json] (optional)
  #   List of nameservers. Gandi's LiveDNS nameservers are used if omitted..
  # @param price [Numeric, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param resellee_id [String, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param smd [String, #to_gandi, #to_json] (optional)
  #   Contents of a Signed Mark Data file (used for newgtld sunrises, tld_period must be sunrise).
  # @param tld_period ["sunrise", "landrush", "eap1", "eap2", "eap3", "eap4", "eap5", "eap6",
  #   "eap7", "eap8", "eap9", "golive", #to_gandi, #to_json] (optional)
  #   @see https://docs.gandi.net/en/domain_names/register/new_gtld.html
  # @return [GandiV5::Domain] the created domain
  # @return [Hash] if doing a dry run, you get what Gandi returns
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def self.create(fqdn, dry_run: false, sharing_id: nil, **params)
    fail ArgumentError, 'missing keyword: owner' unless params.key?(:owner)

    body = params.merge(fqdn: fqdn)
                 .transform_values { |val| val.respond_to?(:to_gandi) ? val.to_gandi : val }
                 .to_json
    url_ = sharing_id ? "#{url}?sharing_id=#{sharing_id}" : url

    response, data = GandiV5.post(url_, body, 'Dry-Run': dry_run ? 1 : 0)
    dry_run ? data : fetch(response.headers[:location].split('/').last)
  end

  # Get information on a domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain
  # @param fqdn [String, #to_s] the fully qualified domain name to fetch.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.fetch(fqdn)
    _response, data = GandiV5.get url(fqdn)
    domain = from_gandi data
    domain.auto_renew.domain = fqdn
    domain
  end

  # List domains.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains
  # @param page [#each<Integer, #to_s>] the page(s) of results to retrieve.
  #   If page is not provided keep querying until an empty list is returned.
  #   If page responds to .each then iterate until an empty list is returned.
  # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page.
  # @param fqdn [String, #to_s] (optional)
  #   filters the list by domain name, with optional patterns.
  #   e.g. "example.net", "example.*", "*ample.com"
  # @param sort_by [String, #to_s] (optional default "fqdn") how to sort the list.
  # @param tld [String, #to_s] (optional) used to filter by just the top level domain.
  # @return [Array<GandiV5::Domain>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.list(page: (1..), per_page: 100, **params)
    page = [page.to_i] unless page.respond_to?(:each)

    domains = []
    page.each do |page_number|
      _resp, data = GandiV5.get url, params: params.merge(page: page_number, per_page: per_page)
      break if data.empty?

      domains += data.map { |domain| from_gandi domain }
      break if data.count < per_page
    end
    domains
  end

  private

  def url(*extra)
    "#{BASE}domain/domains/" +
      CGI.escape(fqdn) +
      (extra.empty? ? '' : "/#{extra.join('/')}")
  end

  def self.url(fqdn = nil)
    "#{BASE}domain/domains" +
      (fqdn ? "/#{CGI.escape fqdn}" : '')
  end
  private_class_method :url
end

#contactsHash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact} (readonly)

Contacts for the domain.

Returns:

Raises:

See Also:



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/gandi_v5/domain.rb', line 52

class Domain
  include GandiV5::Data

  SERVICES = {
    gandidns: 'GandiDNS',
    gandilivedns: 'LiveDNS',
    dnssec: 'DNSSEC',
    certificate: 'SSL Certificate',
    paas: 'PAAS',
    redirection: 'Redirection',
    gandimail: 'GandiMail',
    packmail: 'PackMail',
    blog: 'Blog',
    hosting: 'Hosting',
    site: 'Site',
    mailboxv2: 'MailboxV2'
  }.freeze

  STATUSES = {
    clientHold: 'clientHold',
    clientUpdateProhibited: 'clientUpdateProhibited',
    clientTransferProhibited: 'clientTransferProhibited',
    clientDeleteProhibited: 'clientDeleteProhibited',
    clientRenewProhibited: 'clientRenewProhibited',
    serverHold: 'serverHold',
    pendingTransfer: 'pendingTransfer',
    serverTransferProhibited: 'serverTransferProhibited'
  }.freeze

  CONTACTS_CONVERTER = lambda { |hash|
    break {} if hash.nil?

    hash = hash.transform_keys(&:to_sym)
               .transform_values { |value| GandiV5::Domain::Contact.from_gandi value }

    hash.define_singleton_method(:owner) { send :'[]', :owner }
    hash.define_singleton_method(:admin) { send :'[]', :admin }
    hash.define_singleton_method(:bill) { send :'[]', :bill }
    hash.define_singleton_method(:tech) { send :'[]', :tech }

    hash
  }
  private_constant :CONTACTS_CONVERTER

  members :fqdn, :fqdn_unicode, :tld, :can_tld_lock, :tags, :owner, :domain_owner
  member :sharing_uuid, gandi_key: 'sharing_id'
  member :name_servers, gandi_key: 'nameservers'
  member :auth_info, gandi_key: 'authinfo'
  member :organisation_owner, gandi_key: 'orga_owner'
  member :uuid, gandi_key: 'id'

  member(
    :dates,
    converter: GandiV5::Domain::Dates
  )
  member(
    :sharing_space,
    gandi_key: 'sharing_space',
    converter: GandiV5::Domain::SharingSpace
  )

  member(
    :auto_renew,
    gandi_key: 'autorenew',
    converter: GandiV5::Data::Converter.new(from_gandi: lambda { |hash|
      break nil if hash.nil?
      break nil if hash.eql?(true)
      break GandiV5::Domain::AutoRenew.from_gandi('enabled' => false) if hash.eql?(false)

      GandiV5::Domain::AutoRenew.from_gandi hash
    })
  )

  member(
    :contacts,
    converter: GandiV5::Data::Converter.new(from_gandi: CONTACTS_CONVERTER)
  )

  member(
    :name_server,
    gandi_key: 'nameserver',
    converter: GandiV5::Data::Converter.new(from_gandi: ->(hash) { hash&.[]('current')&.to_sym })
  )
  member :services, converter: GandiV5::Data::Converter::Symbol, array: true
  member :status, converter: GandiV5::Data::Converter::Symbol, array: true
  member :trustee_roles, converter: GandiV5::Data::Converter::Symbol, array: true

  alias domain_uuid uuid

  # rubocop:disable Style/AsciiComments
  # Returns the string representation of the domain.
  # @return [String] e.g. "example.com", "😀.com (xn--e28h.uk.com)"
  # rubocop:enable Style/AsciiComments
  def to_s
    string = fqdn_unicode
    string += " (#{fqdn})" unless fqdn == fqdn_unicode
    string
  end

  # Contacts for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def contacts
    @contacts ||= fetch_contacts
  end

  # Requery Gandi for the domain's contacts.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_contacts
    _response, data = GandiV5.get url('contacts')
    self.contacts = data.transform_keys(&:to_sym)
    CONTACTS_CONVERTER.call contacts
  end

  # Update some of the domain's contacts.
  # @see https://api.gandi.net/docs/domains#patch-v5-domain-domains-domain-contacts
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new administrative contact.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new billing contact.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new technical contact.
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_contacts(admin: nil, bill: nil, tech: nil)
    body = {
      admin: admin.respond_to?(:to_gandi) ? admin.to_gandi : admin,
      bill: bill.respond_to?(:to_gandi) ? bill.to_gandi : bill,
      tech: tech.respond_to?(:to_gandi) ? tech.to_gandi : tech
    }.reject { |_k, v| v.nil? }.to_json

    GandiV5.patch url('contacts'), body
    fetch_contacts
  end

  # Renewal information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renewal_information
    @renewal_information ||= fetch_renewal_information
  end

  # Requery Gandi for the domain's renewal information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_renewal_information
    _response, data = GandiV5.get url('renew')
    data = data['renew'].merge('contracts' => data['contracts'])
    @renewal_information = GandiV5::Domain::RenewalInformation.from_gandi data
  end

  # Renew domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-renew
  # @param duration [Integer, #to_s] how long to renew for (in years).
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renew_for(duration = 1)
    body = { duration: duration }.to_json
    _response, data = GandiV5.post url('renew'), body
    data['message']
  end

  # Restoration information for the domain.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore_information
    @restore_information ||= fetch_restore_information
  end

  # Requery Gandi for the domain's restore information.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_restore_information
    _response, data = GandiV5.get url('restore')
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi data
  rescue RestClient::NotFound
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi restorable: false
  end

  # Restore an expired domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-restore
  # @return [String] The confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore
    _response, data = GandiV5.post url('restore'), '{}'
    data['message']
  end

  # Requery Gandi fo this domain's information.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def refresh
    _response, data = GandiV5.get url
    from_gandi data
    auto_renew.domain = self
  end

  # Get the price for renewing this domain.
  # @param currency [String] the currency to get the price in (e.g. GBP)
  # @param period [Integer] the number of year(s) renewal to get the price for
  # @return [GandiV5::Domain::Availability::Product::Price]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def renewal_price(currency: 'GBP', period: 1)
    arguments = { processes: [:renew], currency: currency, period: period }
    GandiV5::Domain::Availability.fetch(fqdn, **arguments)
                                 .products.first
                                 .prices.first
  end

  # LiveDNS information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def livedns
    @livedns ||= fetch_livedns
  end

  # Requery Gandi for the domain's LiveDNS information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_livedns
    _response, data = GandiV5.get url('livedns')
    @livedns = GandiV5::Domain::LiveDNS.from_gandi data
    @name_server = @livedns.current
    @name_servers = @livedns.name_servers
    @livedns
  end

  # Enable LiveDNS for the domain.
  # If you want to disable LiveDNS, change the nameservers.
  # Please note that if the domain is on the classic Gandi DNS,
  # this will also perform a copy of all existing records immediately afterwards.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-livedns
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def enable_livedns
    _response, data = GandiV5.post url('livedns')
    data['message']
  end

  # Name servers for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def name_servers
    @name_servers ||= fetch_name_servers
  end

  # Requery Gandi for the domain's name servers.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_name_servers
    _response, data = GandiV5.get url('nameservers')
    @name_servers = data
  end

  # Update name servers in Gandi.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-nameservers
  # @param nameservers [Array<String>] the name servers to use.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_name_servers(nameservers)
    _response, data = GandiV5.put url('nameservers'), { 'nameservers' => nameservers }.to_json
    @name_servers = nameservers
    data['message']
  end

  # Glue records for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_records
    @glue_records ||= fetch_glue_records
  end

  # Requery Gandi for the domain's glue records.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_glue_records
    _response, data = GandiV5.get url('hosts')
    @glue_records = data.map { |record| record.values_at('name', 'ips') }.to_h
  end

  # Add a new glue record to the domain in Gandi.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def add_glue_record(name, *ips)
    _response, data = GandiV5.post url('hosts'), { 'name' => name, 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Get a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @return [nil] name was not found
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_record(name)
    records = @glue_records.key?(name) ? @glue_records : fetch_glue_records
    records.key?(name) ? records.select { |k, _v| k == name } : nil
  end

  # Update a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_glue_record(name, *ips)
    _response, data = GandiV5.put url('hosts', name), { 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Delete a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#delete-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def delete_glue_record(name)
    _response, data = GandiV5.delete url('hosts', name)
    @glue_records ||= {}
    @glue_records.delete(name)
    data['message']
  end

  # @see GandiV5::Email::Mailbox.list
  def mailboxes(**params)
    GandiV5::Email::Mailbox.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Slot.list
  def mailbox_slots(**params)
    GandiV5::Email::Slot.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Forward.list
  def email_forwards(**params)
    GandiV5::Email::Forward.list(**params, fqdn: fqdn)
  end

  # Create (register) a new domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains
  # @param fqdn [String, #to_s] the fully qualified domain name to create.
  # @param dry_run [Boolean]
  #   whether the details should be checked instead of actually creating the domain.
  # @param sharing_id [String] either:
  #   * nil (default) - nothing special happens
  #   * an organization ID - pay using another organization
  #     (you need to have billing permissions on the organization
  #     and use the same organization name for the domain name's owner).
  #     The invoice will be edited using this organization's information.
  #   * a reseller ID - buy a domain for a customer using a reseller account
  #     (you need to have billing permissions on the reseller organization
  #     and have your customer's information for the owner).
  #     The invoice will be edited using the reseller organization's information.
  # @param owner [GandiV5::Domain::Contact, #to_gandi, #to_h] (required)
  #   the owner of the new domain.
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the administrative contact for the new domain.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the billing contact for the new domain.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the technical contact for the new domain.
  # @param claims [String] (optional) unknown - not documented at Gandi.
  # @param currency ["EUR", "USD", "GBP", "TWD", "CNY"] (optional)
  #   the currency you wish to be charged in.
  # @param duration [Integer] (optional, default 1, minimum 1 maximum 10)
  #   how many years to register for.
  # @param enforce_premium [Boolean] (optional)
  #   must be set to true if the domain is a premium domain.
  # @param extra_parameters [Hash, #to_gandi, #to_json] (optional)
  #   unknown - not documented at Gandi.
  # @param lang [String] (optional)
  #   ISO-639-2 language code of the domain, required for some IDN domains.
  # @param nameserver_ips [Hash<String => Array<String>>, #to_gandi, #to_json] (optional)
  #   For glue records only - dictionnary associating a nameserver to a list of IP addresses.
  # @param nameservers [Array<String>, #to_gandi, #to_json] (optional)
  #   List of nameservers. Gandi's LiveDNS nameservers are used if omitted..
  # @param price [Numeric, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param resellee_id [String, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param smd [String, #to_gandi, #to_json] (optional)
  #   Contents of a Signed Mark Data file (used for newgtld sunrises, tld_period must be sunrise).
  # @param tld_period ["sunrise", "landrush", "eap1", "eap2", "eap3", "eap4", "eap5", "eap6",
  #   "eap7", "eap8", "eap9", "golive", #to_gandi, #to_json] (optional)
  #   @see https://docs.gandi.net/en/domain_names/register/new_gtld.html
  # @return [GandiV5::Domain] the created domain
  # @return [Hash] if doing a dry run, you get what Gandi returns
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def self.create(fqdn, dry_run: false, sharing_id: nil, **params)
    fail ArgumentError, 'missing keyword: owner' unless params.key?(:owner)

    body = params.merge(fqdn: fqdn)
                 .transform_values { |val| val.respond_to?(:to_gandi) ? val.to_gandi : val }
                 .to_json
    url_ = sharing_id ? "#{url}?sharing_id=#{sharing_id}" : url

    response, data = GandiV5.post(url_, body, 'Dry-Run': dry_run ? 1 : 0)
    dry_run ? data : fetch(response.headers[:location].split('/').last)
  end

  # Get information on a domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain
  # @param fqdn [String, #to_s] the fully qualified domain name to fetch.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.fetch(fqdn)
    _response, data = GandiV5.get url(fqdn)
    domain = from_gandi data
    domain.auto_renew.domain = fqdn
    domain
  end

  # List domains.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains
  # @param page [#each<Integer, #to_s>] the page(s) of results to retrieve.
  #   If page is not provided keep querying until an empty list is returned.
  #   If page responds to .each then iterate until an empty list is returned.
  # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page.
  # @param fqdn [String, #to_s] (optional)
  #   filters the list by domain name, with optional patterns.
  #   e.g. "example.net", "example.*", "*ample.com"
  # @param sort_by [String, #to_s] (optional default "fqdn") how to sort the list.
  # @param tld [String, #to_s] (optional) used to filter by just the top level domain.
  # @return [Array<GandiV5::Domain>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.list(page: (1..), per_page: 100, **params)
    page = [page.to_i] unless page.respond_to?(:each)

    domains = []
    page.each do |page_number|
      _resp, data = GandiV5.get url, params: params.merge(page: page_number, per_page: per_page)
      break if data.empty?

      domains += data.map { |domain| from_gandi domain }
      break if data.count < per_page
    end
    domains
  end

  private

  def url(*extra)
    "#{BASE}domain/domains/" +
      CGI.escape(fqdn) +
      (extra.empty? ? '' : "/#{extra.join('/')}")
  end

  def self.url(fqdn = nil)
    "#{BASE}domain/domains" +
      (fqdn ? "/#{CGI.escape fqdn}" : '')
  end
  private_class_method :url
end

#datesGandiV5::Domain::Dates (readonly)



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/gandi_v5/domain.rb', line 52

class Domain
  include GandiV5::Data

  SERVICES = {
    gandidns: 'GandiDNS',
    gandilivedns: 'LiveDNS',
    dnssec: 'DNSSEC',
    certificate: 'SSL Certificate',
    paas: 'PAAS',
    redirection: 'Redirection',
    gandimail: 'GandiMail',
    packmail: 'PackMail',
    blog: 'Blog',
    hosting: 'Hosting',
    site: 'Site',
    mailboxv2: 'MailboxV2'
  }.freeze

  STATUSES = {
    clientHold: 'clientHold',
    clientUpdateProhibited: 'clientUpdateProhibited',
    clientTransferProhibited: 'clientTransferProhibited',
    clientDeleteProhibited: 'clientDeleteProhibited',
    clientRenewProhibited: 'clientRenewProhibited',
    serverHold: 'serverHold',
    pendingTransfer: 'pendingTransfer',
    serverTransferProhibited: 'serverTransferProhibited'
  }.freeze

  CONTACTS_CONVERTER = lambda { |hash|
    break {} if hash.nil?

    hash = hash.transform_keys(&:to_sym)
               .transform_values { |value| GandiV5::Domain::Contact.from_gandi value }

    hash.define_singleton_method(:owner) { send :'[]', :owner }
    hash.define_singleton_method(:admin) { send :'[]', :admin }
    hash.define_singleton_method(:bill) { send :'[]', :bill }
    hash.define_singleton_method(:tech) { send :'[]', :tech }

    hash
  }
  private_constant :CONTACTS_CONVERTER

  members :fqdn, :fqdn_unicode, :tld, :can_tld_lock, :tags, :owner, :domain_owner
  member :sharing_uuid, gandi_key: 'sharing_id'
  member :name_servers, gandi_key: 'nameservers'
  member :auth_info, gandi_key: 'authinfo'
  member :organisation_owner, gandi_key: 'orga_owner'
  member :uuid, gandi_key: 'id'

  member(
    :dates,
    converter: GandiV5::Domain::Dates
  )
  member(
    :sharing_space,
    gandi_key: 'sharing_space',
    converter: GandiV5::Domain::SharingSpace
  )

  member(
    :auto_renew,
    gandi_key: 'autorenew',
    converter: GandiV5::Data::Converter.new(from_gandi: lambda { |hash|
      break nil if hash.nil?
      break nil if hash.eql?(true)
      break GandiV5::Domain::AutoRenew.from_gandi('enabled' => false) if hash.eql?(false)

      GandiV5::Domain::AutoRenew.from_gandi hash
    })
  )

  member(
    :contacts,
    converter: GandiV5::Data::Converter.new(from_gandi: CONTACTS_CONVERTER)
  )

  member(
    :name_server,
    gandi_key: 'nameserver',
    converter: GandiV5::Data::Converter.new(from_gandi: ->(hash) { hash&.[]('current')&.to_sym })
  )
  member :services, converter: GandiV5::Data::Converter::Symbol, array: true
  member :status, converter: GandiV5::Data::Converter::Symbol, array: true
  member :trustee_roles, converter: GandiV5::Data::Converter::Symbol, array: true

  alias domain_uuid uuid

  # rubocop:disable Style/AsciiComments
  # Returns the string representation of the domain.
  # @return [String] e.g. "example.com", "😀.com (xn--e28h.uk.com)"
  # rubocop:enable Style/AsciiComments
  def to_s
    string = fqdn_unicode
    string += " (#{fqdn})" unless fqdn == fqdn_unicode
    string
  end

  # Contacts for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def contacts
    @contacts ||= fetch_contacts
  end

  # Requery Gandi for the domain's contacts.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_contacts
    _response, data = GandiV5.get url('contacts')
    self.contacts = data.transform_keys(&:to_sym)
    CONTACTS_CONVERTER.call contacts
  end

  # Update some of the domain's contacts.
  # @see https://api.gandi.net/docs/domains#patch-v5-domain-domains-domain-contacts
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new administrative contact.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new billing contact.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new technical contact.
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_contacts(admin: nil, bill: nil, tech: nil)
    body = {
      admin: admin.respond_to?(:to_gandi) ? admin.to_gandi : admin,
      bill: bill.respond_to?(:to_gandi) ? bill.to_gandi : bill,
      tech: tech.respond_to?(:to_gandi) ? tech.to_gandi : tech
    }.reject { |_k, v| v.nil? }.to_json

    GandiV5.patch url('contacts'), body
    fetch_contacts
  end

  # Renewal information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renewal_information
    @renewal_information ||= fetch_renewal_information
  end

  # Requery Gandi for the domain's renewal information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_renewal_information
    _response, data = GandiV5.get url('renew')
    data = data['renew'].merge('contracts' => data['contracts'])
    @renewal_information = GandiV5::Domain::RenewalInformation.from_gandi data
  end

  # Renew domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-renew
  # @param duration [Integer, #to_s] how long to renew for (in years).
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renew_for(duration = 1)
    body = { duration: duration }.to_json
    _response, data = GandiV5.post url('renew'), body
    data['message']
  end

  # Restoration information for the domain.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore_information
    @restore_information ||= fetch_restore_information
  end

  # Requery Gandi for the domain's restore information.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_restore_information
    _response, data = GandiV5.get url('restore')
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi data
  rescue RestClient::NotFound
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi restorable: false
  end

  # Restore an expired domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-restore
  # @return [String] The confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore
    _response, data = GandiV5.post url('restore'), '{}'
    data['message']
  end

  # Requery Gandi fo this domain's information.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def refresh
    _response, data = GandiV5.get url
    from_gandi data
    auto_renew.domain = self
  end

  # Get the price for renewing this domain.
  # @param currency [String] the currency to get the price in (e.g. GBP)
  # @param period [Integer] the number of year(s) renewal to get the price for
  # @return [GandiV5::Domain::Availability::Product::Price]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def renewal_price(currency: 'GBP', period: 1)
    arguments = { processes: [:renew], currency: currency, period: period }
    GandiV5::Domain::Availability.fetch(fqdn, **arguments)
                                 .products.first
                                 .prices.first
  end

  # LiveDNS information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def livedns
    @livedns ||= fetch_livedns
  end

  # Requery Gandi for the domain's LiveDNS information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_livedns
    _response, data = GandiV5.get url('livedns')
    @livedns = GandiV5::Domain::LiveDNS.from_gandi data
    @name_server = @livedns.current
    @name_servers = @livedns.name_servers
    @livedns
  end

  # Enable LiveDNS for the domain.
  # If you want to disable LiveDNS, change the nameservers.
  # Please note that if the domain is on the classic Gandi DNS,
  # this will also perform a copy of all existing records immediately afterwards.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-livedns
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def enable_livedns
    _response, data = GandiV5.post url('livedns')
    data['message']
  end

  # Name servers for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def name_servers
    @name_servers ||= fetch_name_servers
  end

  # Requery Gandi for the domain's name servers.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_name_servers
    _response, data = GandiV5.get url('nameservers')
    @name_servers = data
  end

  # Update name servers in Gandi.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-nameservers
  # @param nameservers [Array<String>] the name servers to use.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_name_servers(nameservers)
    _response, data = GandiV5.put url('nameservers'), { 'nameservers' => nameservers }.to_json
    @name_servers = nameservers
    data['message']
  end

  # Glue records for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_records
    @glue_records ||= fetch_glue_records
  end

  # Requery Gandi for the domain's glue records.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_glue_records
    _response, data = GandiV5.get url('hosts')
    @glue_records = data.map { |record| record.values_at('name', 'ips') }.to_h
  end

  # Add a new glue record to the domain in Gandi.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def add_glue_record(name, *ips)
    _response, data = GandiV5.post url('hosts'), { 'name' => name, 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Get a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @return [nil] name was not found
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_record(name)
    records = @glue_records.key?(name) ? @glue_records : fetch_glue_records
    records.key?(name) ? records.select { |k, _v| k == name } : nil
  end

  # Update a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_glue_record(name, *ips)
    _response, data = GandiV5.put url('hosts', name), { 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Delete a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#delete-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def delete_glue_record(name)
    _response, data = GandiV5.delete url('hosts', name)
    @glue_records ||= {}
    @glue_records.delete(name)
    data['message']
  end

  # @see GandiV5::Email::Mailbox.list
  def mailboxes(**params)
    GandiV5::Email::Mailbox.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Slot.list
  def mailbox_slots(**params)
    GandiV5::Email::Slot.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Forward.list
  def email_forwards(**params)
    GandiV5::Email::Forward.list(**params, fqdn: fqdn)
  end

  # Create (register) a new domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains
  # @param fqdn [String, #to_s] the fully qualified domain name to create.
  # @param dry_run [Boolean]
  #   whether the details should be checked instead of actually creating the domain.
  # @param sharing_id [String] either:
  #   * nil (default) - nothing special happens
  #   * an organization ID - pay using another organization
  #     (you need to have billing permissions on the organization
  #     and use the same organization name for the domain name's owner).
  #     The invoice will be edited using this organization's information.
  #   * a reseller ID - buy a domain for a customer using a reseller account
  #     (you need to have billing permissions on the reseller organization
  #     and have your customer's information for the owner).
  #     The invoice will be edited using the reseller organization's information.
  # @param owner [GandiV5::Domain::Contact, #to_gandi, #to_h] (required)
  #   the owner of the new domain.
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the administrative contact for the new domain.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the billing contact for the new domain.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the technical contact for the new domain.
  # @param claims [String] (optional) unknown - not documented at Gandi.
  # @param currency ["EUR", "USD", "GBP", "TWD", "CNY"] (optional)
  #   the currency you wish to be charged in.
  # @param duration [Integer] (optional, default 1, minimum 1 maximum 10)
  #   how many years to register for.
  # @param enforce_premium [Boolean] (optional)
  #   must be set to true if the domain is a premium domain.
  # @param extra_parameters [Hash, #to_gandi, #to_json] (optional)
  #   unknown - not documented at Gandi.
  # @param lang [String] (optional)
  #   ISO-639-2 language code of the domain, required for some IDN domains.
  # @param nameserver_ips [Hash<String => Array<String>>, #to_gandi, #to_json] (optional)
  #   For glue records only - dictionnary associating a nameserver to a list of IP addresses.
  # @param nameservers [Array<String>, #to_gandi, #to_json] (optional)
  #   List of nameservers. Gandi's LiveDNS nameservers are used if omitted..
  # @param price [Numeric, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param resellee_id [String, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param smd [String, #to_gandi, #to_json] (optional)
  #   Contents of a Signed Mark Data file (used for newgtld sunrises, tld_period must be sunrise).
  # @param tld_period ["sunrise", "landrush", "eap1", "eap2", "eap3", "eap4", "eap5", "eap6",
  #   "eap7", "eap8", "eap9", "golive", #to_gandi, #to_json] (optional)
  #   @see https://docs.gandi.net/en/domain_names/register/new_gtld.html
  # @return [GandiV5::Domain] the created domain
  # @return [Hash] if doing a dry run, you get what Gandi returns
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def self.create(fqdn, dry_run: false, sharing_id: nil, **params)
    fail ArgumentError, 'missing keyword: owner' unless params.key?(:owner)

    body = params.merge(fqdn: fqdn)
                 .transform_values { |val| val.respond_to?(:to_gandi) ? val.to_gandi : val }
                 .to_json
    url_ = sharing_id ? "#{url}?sharing_id=#{sharing_id}" : url

    response, data = GandiV5.post(url_, body, 'Dry-Run': dry_run ? 1 : 0)
    dry_run ? data : fetch(response.headers[:location].split('/').last)
  end

  # Get information on a domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain
  # @param fqdn [String, #to_s] the fully qualified domain name to fetch.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.fetch(fqdn)
    _response, data = GandiV5.get url(fqdn)
    domain = from_gandi data
    domain.auto_renew.domain = fqdn
    domain
  end

  # List domains.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains
  # @param page [#each<Integer, #to_s>] the page(s) of results to retrieve.
  #   If page is not provided keep querying until an empty list is returned.
  #   If page responds to .each then iterate until an empty list is returned.
  # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page.
  # @param fqdn [String, #to_s] (optional)
  #   filters the list by domain name, with optional patterns.
  #   e.g. "example.net", "example.*", "*ample.com"
  # @param sort_by [String, #to_s] (optional default "fqdn") how to sort the list.
  # @param tld [String, #to_s] (optional) used to filter by just the top level domain.
  # @return [Array<GandiV5::Domain>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.list(page: (1..), per_page: 100, **params)
    page = [page.to_i] unless page.respond_to?(:each)

    domains = []
    page.each do |page_number|
      _resp, data = GandiV5.get url, params: params.merge(page: page_number, per_page: per_page)
      break if data.empty?

      domains += data.map { |domain| from_gandi domain }
      break if data.count < per_page
    end
    domains
  end

  private

  def url(*extra)
    "#{BASE}domain/domains/" +
      CGI.escape(fqdn) +
      (extra.empty? ? '' : "/#{extra.join('/')}")
  end

  def self.url(fqdn = nil)
    "#{BASE}domain/domains" +
      (fqdn ? "/#{CGI.escape fqdn}" : '')
  end
  private_class_method :url
end

#domain_ownerString (readonly)

Returns:

  • (String)


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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/gandi_v5/domain.rb', line 52

class Domain
  include GandiV5::Data

  SERVICES = {
    gandidns: 'GandiDNS',
    gandilivedns: 'LiveDNS',
    dnssec: 'DNSSEC',
    certificate: 'SSL Certificate',
    paas: 'PAAS',
    redirection: 'Redirection',
    gandimail: 'GandiMail',
    packmail: 'PackMail',
    blog: 'Blog',
    hosting: 'Hosting',
    site: 'Site',
    mailboxv2: 'MailboxV2'
  }.freeze

  STATUSES = {
    clientHold: 'clientHold',
    clientUpdateProhibited: 'clientUpdateProhibited',
    clientTransferProhibited: 'clientTransferProhibited',
    clientDeleteProhibited: 'clientDeleteProhibited',
    clientRenewProhibited: 'clientRenewProhibited',
    serverHold: 'serverHold',
    pendingTransfer: 'pendingTransfer',
    serverTransferProhibited: 'serverTransferProhibited'
  }.freeze

  CONTACTS_CONVERTER = lambda { |hash|
    break {} if hash.nil?

    hash = hash.transform_keys(&:to_sym)
               .transform_values { |value| GandiV5::Domain::Contact.from_gandi value }

    hash.define_singleton_method(:owner) { send :'[]', :owner }
    hash.define_singleton_method(:admin) { send :'[]', :admin }
    hash.define_singleton_method(:bill) { send :'[]', :bill }
    hash.define_singleton_method(:tech) { send :'[]', :tech }

    hash
  }
  private_constant :CONTACTS_CONVERTER

  members :fqdn, :fqdn_unicode, :tld, :can_tld_lock, :tags, :owner, :domain_owner
  member :sharing_uuid, gandi_key: 'sharing_id'
  member :name_servers, gandi_key: 'nameservers'
  member :auth_info, gandi_key: 'authinfo'
  member :organisation_owner, gandi_key: 'orga_owner'
  member :uuid, gandi_key: 'id'

  member(
    :dates,
    converter: GandiV5::Domain::Dates
  )
  member(
    :sharing_space,
    gandi_key: 'sharing_space',
    converter: GandiV5::Domain::SharingSpace
  )

  member(
    :auto_renew,
    gandi_key: 'autorenew',
    converter: GandiV5::Data::Converter.new(from_gandi: lambda { |hash|
      break nil if hash.nil?
      break nil if hash.eql?(true)
      break GandiV5::Domain::AutoRenew.from_gandi('enabled' => false) if hash.eql?(false)

      GandiV5::Domain::AutoRenew.from_gandi hash
    })
  )

  member(
    :contacts,
    converter: GandiV5::Data::Converter.new(from_gandi: CONTACTS_CONVERTER)
  )

  member(
    :name_server,
    gandi_key: 'nameserver',
    converter: GandiV5::Data::Converter.new(from_gandi: ->(hash) { hash&.[]('current')&.to_sym })
  )
  member :services, converter: GandiV5::Data::Converter::Symbol, array: true
  member :status, converter: GandiV5::Data::Converter::Symbol, array: true
  member :trustee_roles, converter: GandiV5::Data::Converter::Symbol, array: true

  alias domain_uuid uuid

  # rubocop:disable Style/AsciiComments
  # Returns the string representation of the domain.
  # @return [String] e.g. "example.com", "😀.com (xn--e28h.uk.com)"
  # rubocop:enable Style/AsciiComments
  def to_s
    string = fqdn_unicode
    string += " (#{fqdn})" unless fqdn == fqdn_unicode
    string
  end

  # Contacts for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def contacts
    @contacts ||= fetch_contacts
  end

  # Requery Gandi for the domain's contacts.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_contacts
    _response, data = GandiV5.get url('contacts')
    self.contacts = data.transform_keys(&:to_sym)
    CONTACTS_CONVERTER.call contacts
  end

  # Update some of the domain's contacts.
  # @see https://api.gandi.net/docs/domains#patch-v5-domain-domains-domain-contacts
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new administrative contact.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new billing contact.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new technical contact.
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_contacts(admin: nil, bill: nil, tech: nil)
    body = {
      admin: admin.respond_to?(:to_gandi) ? admin.to_gandi : admin,
      bill: bill.respond_to?(:to_gandi) ? bill.to_gandi : bill,
      tech: tech.respond_to?(:to_gandi) ? tech.to_gandi : tech
    }.reject { |_k, v| v.nil? }.to_json

    GandiV5.patch url('contacts'), body
    fetch_contacts
  end

  # Renewal information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renewal_information
    @renewal_information ||= fetch_renewal_information
  end

  # Requery Gandi for the domain's renewal information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_renewal_information
    _response, data = GandiV5.get url('renew')
    data = data['renew'].merge('contracts' => data['contracts'])
    @renewal_information = GandiV5::Domain::RenewalInformation.from_gandi data
  end

  # Renew domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-renew
  # @param duration [Integer, #to_s] how long to renew for (in years).
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renew_for(duration = 1)
    body = { duration: duration }.to_json
    _response, data = GandiV5.post url('renew'), body
    data['message']
  end

  # Restoration information for the domain.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore_information
    @restore_information ||= fetch_restore_information
  end

  # Requery Gandi for the domain's restore information.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_restore_information
    _response, data = GandiV5.get url('restore')
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi data
  rescue RestClient::NotFound
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi restorable: false
  end

  # Restore an expired domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-restore
  # @return [String] The confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore
    _response, data = GandiV5.post url('restore'), '{}'
    data['message']
  end

  # Requery Gandi fo this domain's information.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def refresh
    _response, data = GandiV5.get url
    from_gandi data
    auto_renew.domain = self
  end

  # Get the price for renewing this domain.
  # @param currency [String] the currency to get the price in (e.g. GBP)
  # @param period [Integer] the number of year(s) renewal to get the price for
  # @return [GandiV5::Domain::Availability::Product::Price]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def renewal_price(currency: 'GBP', period: 1)
    arguments = { processes: [:renew], currency: currency, period: period }
    GandiV5::Domain::Availability.fetch(fqdn, **arguments)
                                 .products.first
                                 .prices.first
  end

  # LiveDNS information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def livedns
    @livedns ||= fetch_livedns
  end

  # Requery Gandi for the domain's LiveDNS information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_livedns
    _response, data = GandiV5.get url('livedns')
    @livedns = GandiV5::Domain::LiveDNS.from_gandi data
    @name_server = @livedns.current
    @name_servers = @livedns.name_servers
    @livedns
  end

  # Enable LiveDNS for the domain.
  # If you want to disable LiveDNS, change the nameservers.
  # Please note that if the domain is on the classic Gandi DNS,
  # this will also perform a copy of all existing records immediately afterwards.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-livedns
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def enable_livedns
    _response, data = GandiV5.post url('livedns')
    data['message']
  end

  # Name servers for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def name_servers
    @name_servers ||= fetch_name_servers
  end

  # Requery Gandi for the domain's name servers.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_name_servers
    _response, data = GandiV5.get url('nameservers')
    @name_servers = data
  end

  # Update name servers in Gandi.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-nameservers
  # @param nameservers [Array<String>] the name servers to use.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_name_servers(nameservers)
    _response, data = GandiV5.put url('nameservers'), { 'nameservers' => nameservers }.to_json
    @name_servers = nameservers
    data['message']
  end

  # Glue records for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_records
    @glue_records ||= fetch_glue_records
  end

  # Requery Gandi for the domain's glue records.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_glue_records
    _response, data = GandiV5.get url('hosts')
    @glue_records = data.map { |record| record.values_at('name', 'ips') }.to_h
  end

  # Add a new glue record to the domain in Gandi.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def add_glue_record(name, *ips)
    _response, data = GandiV5.post url('hosts'), { 'name' => name, 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Get a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @return [nil] name was not found
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_record(name)
    records = @glue_records.key?(name) ? @glue_records : fetch_glue_records
    records.key?(name) ? records.select { |k, _v| k == name } : nil
  end

  # Update a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_glue_record(name, *ips)
    _response, data = GandiV5.put url('hosts', name), { 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Delete a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#delete-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def delete_glue_record(name)
    _response, data = GandiV5.delete url('hosts', name)
    @glue_records ||= {}
    @glue_records.delete(name)
    data['message']
  end

  # @see GandiV5::Email::Mailbox.list
  def mailboxes(**params)
    GandiV5::Email::Mailbox.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Slot.list
  def mailbox_slots(**params)
    GandiV5::Email::Slot.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Forward.list
  def email_forwards(**params)
    GandiV5::Email::Forward.list(**params, fqdn: fqdn)
  end

  # Create (register) a new domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains
  # @param fqdn [String, #to_s] the fully qualified domain name to create.
  # @param dry_run [Boolean]
  #   whether the details should be checked instead of actually creating the domain.
  # @param sharing_id [String] either:
  #   * nil (default) - nothing special happens
  #   * an organization ID - pay using another organization
  #     (you need to have billing permissions on the organization
  #     and use the same organization name for the domain name's owner).
  #     The invoice will be edited using this organization's information.
  #   * a reseller ID - buy a domain for a customer using a reseller account
  #     (you need to have billing permissions on the reseller organization
  #     and have your customer's information for the owner).
  #     The invoice will be edited using the reseller organization's information.
  # @param owner [GandiV5::Domain::Contact, #to_gandi, #to_h] (required)
  #   the owner of the new domain.
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the administrative contact for the new domain.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the billing contact for the new domain.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the technical contact for the new domain.
  # @param claims [String] (optional) unknown - not documented at Gandi.
  # @param currency ["EUR", "USD", "GBP", "TWD", "CNY"] (optional)
  #   the currency you wish to be charged in.
  # @param duration [Integer] (optional, default 1, minimum 1 maximum 10)
  #   how many years to register for.
  # @param enforce_premium [Boolean] (optional)
  #   must be set to true if the domain is a premium domain.
  # @param extra_parameters [Hash, #to_gandi, #to_json] (optional)
  #   unknown - not documented at Gandi.
  # @param lang [String] (optional)
  #   ISO-639-2 language code of the domain, required for some IDN domains.
  # @param nameserver_ips [Hash<String => Array<String>>, #to_gandi, #to_json] (optional)
  #   For glue records only - dictionnary associating a nameserver to a list of IP addresses.
  # @param nameservers [Array<String>, #to_gandi, #to_json] (optional)
  #   List of nameservers. Gandi's LiveDNS nameservers are used if omitted..
  # @param price [Numeric, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param resellee_id [String, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param smd [String, #to_gandi, #to_json] (optional)
  #   Contents of a Signed Mark Data file (used for newgtld sunrises, tld_period must be sunrise).
  # @param tld_period ["sunrise", "landrush", "eap1", "eap2", "eap3", "eap4", "eap5", "eap6",
  #   "eap7", "eap8", "eap9", "golive", #to_gandi, #to_json] (optional)
  #   @see https://docs.gandi.net/en/domain_names/register/new_gtld.html
  # @return [GandiV5::Domain] the created domain
  # @return [Hash] if doing a dry run, you get what Gandi returns
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def self.create(fqdn, dry_run: false, sharing_id: nil, **params)
    fail ArgumentError, 'missing keyword: owner' unless params.key?(:owner)

    body = params.merge(fqdn: fqdn)
                 .transform_values { |val| val.respond_to?(:to_gandi) ? val.to_gandi : val }
                 .to_json
    url_ = sharing_id ? "#{url}?sharing_id=#{sharing_id}" : url

    response, data = GandiV5.post(url_, body, 'Dry-Run': dry_run ? 1 : 0)
    dry_run ? data : fetch(response.headers[:location].split('/').last)
  end

  # Get information on a domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain
  # @param fqdn [String, #to_s] the fully qualified domain name to fetch.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.fetch(fqdn)
    _response, data = GandiV5.get url(fqdn)
    domain = from_gandi data
    domain.auto_renew.domain = fqdn
    domain
  end

  # List domains.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains
  # @param page [#each<Integer, #to_s>] the page(s) of results to retrieve.
  #   If page is not provided keep querying until an empty list is returned.
  #   If page responds to .each then iterate until an empty list is returned.
  # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page.
  # @param fqdn [String, #to_s] (optional)
  #   filters the list by domain name, with optional patterns.
  #   e.g. "example.net", "example.*", "*ample.com"
  # @param sort_by [String, #to_s] (optional default "fqdn") how to sort the list.
  # @param tld [String, #to_s] (optional) used to filter by just the top level domain.
  # @return [Array<GandiV5::Domain>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.list(page: (1..), per_page: 100, **params)
    page = [page.to_i] unless page.respond_to?(:each)

    domains = []
    page.each do |page_number|
      _resp, data = GandiV5.get url, params: params.merge(page: page_number, per_page: per_page)
      break if data.empty?

      domains += data.map { |domain| from_gandi domain }
      break if data.count < per_page
    end
    domains
  end

  private

  def url(*extra)
    "#{BASE}domain/domains/" +
      CGI.escape(fqdn) +
      (extra.empty? ? '' : "/#{extra.join('/')}")
  end

  def self.url(fqdn = nil)
    "#{BASE}domain/domains" +
      (fqdn ? "/#{CGI.escape fqdn}" : '')
  end
  private_class_method :url
end

#fqdnString (readonly)

Returns fully qualified domain name, written in its native alphabet (IDN).

Returns:

  • (String)

    fully qualified domain name, written in its native alphabet (IDN).



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/gandi_v5/domain.rb', line 52

class Domain
  include GandiV5::Data

  SERVICES = {
    gandidns: 'GandiDNS',
    gandilivedns: 'LiveDNS',
    dnssec: 'DNSSEC',
    certificate: 'SSL Certificate',
    paas: 'PAAS',
    redirection: 'Redirection',
    gandimail: 'GandiMail',
    packmail: 'PackMail',
    blog: 'Blog',
    hosting: 'Hosting',
    site: 'Site',
    mailboxv2: 'MailboxV2'
  }.freeze

  STATUSES = {
    clientHold: 'clientHold',
    clientUpdateProhibited: 'clientUpdateProhibited',
    clientTransferProhibited: 'clientTransferProhibited',
    clientDeleteProhibited: 'clientDeleteProhibited',
    clientRenewProhibited: 'clientRenewProhibited',
    serverHold: 'serverHold',
    pendingTransfer: 'pendingTransfer',
    serverTransferProhibited: 'serverTransferProhibited'
  }.freeze

  CONTACTS_CONVERTER = lambda { |hash|
    break {} if hash.nil?

    hash = hash.transform_keys(&:to_sym)
               .transform_values { |value| GandiV5::Domain::Contact.from_gandi value }

    hash.define_singleton_method(:owner) { send :'[]', :owner }
    hash.define_singleton_method(:admin) { send :'[]', :admin }
    hash.define_singleton_method(:bill) { send :'[]', :bill }
    hash.define_singleton_method(:tech) { send :'[]', :tech }

    hash
  }
  private_constant :CONTACTS_CONVERTER

  members :fqdn, :fqdn_unicode, :tld, :can_tld_lock, :tags, :owner, :domain_owner
  member :sharing_uuid, gandi_key: 'sharing_id'
  member :name_servers, gandi_key: 'nameservers'
  member :auth_info, gandi_key: 'authinfo'
  member :organisation_owner, gandi_key: 'orga_owner'
  member :uuid, gandi_key: 'id'

  member(
    :dates,
    converter: GandiV5::Domain::Dates
  )
  member(
    :sharing_space,
    gandi_key: 'sharing_space',
    converter: GandiV5::Domain::SharingSpace
  )

  member(
    :auto_renew,
    gandi_key: 'autorenew',
    converter: GandiV5::Data::Converter.new(from_gandi: lambda { |hash|
      break nil if hash.nil?
      break nil if hash.eql?(true)
      break GandiV5::Domain::AutoRenew.from_gandi('enabled' => false) if hash.eql?(false)

      GandiV5::Domain::AutoRenew.from_gandi hash
    })
  )

  member(
    :contacts,
    converter: GandiV5::Data::Converter.new(from_gandi: CONTACTS_CONVERTER)
  )

  member(
    :name_server,
    gandi_key: 'nameserver',
    converter: GandiV5::Data::Converter.new(from_gandi: ->(hash) { hash&.[]('current')&.to_sym })
  )
  member :services, converter: GandiV5::Data::Converter::Symbol, array: true
  member :status, converter: GandiV5::Data::Converter::Symbol, array: true
  member :trustee_roles, converter: GandiV5::Data::Converter::Symbol, array: true

  alias domain_uuid uuid

  # rubocop:disable Style/AsciiComments
  # Returns the string representation of the domain.
  # @return [String] e.g. "example.com", "😀.com (xn--e28h.uk.com)"
  # rubocop:enable Style/AsciiComments
  def to_s
    string = fqdn_unicode
    string += " (#{fqdn})" unless fqdn == fqdn_unicode
    string
  end

  # Contacts for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def contacts
    @contacts ||= fetch_contacts
  end

  # Requery Gandi for the domain's contacts.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_contacts
    _response, data = GandiV5.get url('contacts')
    self.contacts = data.transform_keys(&:to_sym)
    CONTACTS_CONVERTER.call contacts
  end

  # Update some of the domain's contacts.
  # @see https://api.gandi.net/docs/domains#patch-v5-domain-domains-domain-contacts
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new administrative contact.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new billing contact.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new technical contact.
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_contacts(admin: nil, bill: nil, tech: nil)
    body = {
      admin: admin.respond_to?(:to_gandi) ? admin.to_gandi : admin,
      bill: bill.respond_to?(:to_gandi) ? bill.to_gandi : bill,
      tech: tech.respond_to?(:to_gandi) ? tech.to_gandi : tech
    }.reject { |_k, v| v.nil? }.to_json

    GandiV5.patch url('contacts'), body
    fetch_contacts
  end

  # Renewal information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renewal_information
    @renewal_information ||= fetch_renewal_information
  end

  # Requery Gandi for the domain's renewal information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_renewal_information
    _response, data = GandiV5.get url('renew')
    data = data['renew'].merge('contracts' => data['contracts'])
    @renewal_information = GandiV5::Domain::RenewalInformation.from_gandi data
  end

  # Renew domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-renew
  # @param duration [Integer, #to_s] how long to renew for (in years).
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renew_for(duration = 1)
    body = { duration: duration }.to_json
    _response, data = GandiV5.post url('renew'), body
    data['message']
  end

  # Restoration information for the domain.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore_information
    @restore_information ||= fetch_restore_information
  end

  # Requery Gandi for the domain's restore information.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_restore_information
    _response, data = GandiV5.get url('restore')
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi data
  rescue RestClient::NotFound
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi restorable: false
  end

  # Restore an expired domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-restore
  # @return [String] The confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore
    _response, data = GandiV5.post url('restore'), '{}'
    data['message']
  end

  # Requery Gandi fo this domain's information.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def refresh
    _response, data = GandiV5.get url
    from_gandi data
    auto_renew.domain = self
  end

  # Get the price for renewing this domain.
  # @param currency [String] the currency to get the price in (e.g. GBP)
  # @param period [Integer] the number of year(s) renewal to get the price for
  # @return [GandiV5::Domain::Availability::Product::Price]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def renewal_price(currency: 'GBP', period: 1)
    arguments = { processes: [:renew], currency: currency, period: period }
    GandiV5::Domain::Availability.fetch(fqdn, **arguments)
                                 .products.first
                                 .prices.first
  end

  # LiveDNS information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def livedns
    @livedns ||= fetch_livedns
  end

  # Requery Gandi for the domain's LiveDNS information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_livedns
    _response, data = GandiV5.get url('livedns')
    @livedns = GandiV5::Domain::LiveDNS.from_gandi data
    @name_server = @livedns.current
    @name_servers = @livedns.name_servers
    @livedns
  end

  # Enable LiveDNS for the domain.
  # If you want to disable LiveDNS, change the nameservers.
  # Please note that if the domain is on the classic Gandi DNS,
  # this will also perform a copy of all existing records immediately afterwards.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-livedns
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def enable_livedns
    _response, data = GandiV5.post url('livedns')
    data['message']
  end

  # Name servers for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def name_servers
    @name_servers ||= fetch_name_servers
  end

  # Requery Gandi for the domain's name servers.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_name_servers
    _response, data = GandiV5.get url('nameservers')
    @name_servers = data
  end

  # Update name servers in Gandi.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-nameservers
  # @param nameservers [Array<String>] the name servers to use.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_name_servers(nameservers)
    _response, data = GandiV5.put url('nameservers'), { 'nameservers' => nameservers }.to_json
    @name_servers = nameservers
    data['message']
  end

  # Glue records for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_records
    @glue_records ||= fetch_glue_records
  end

  # Requery Gandi for the domain's glue records.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_glue_records
    _response, data = GandiV5.get url('hosts')
    @glue_records = data.map { |record| record.values_at('name', 'ips') }.to_h
  end

  # Add a new glue record to the domain in Gandi.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def add_glue_record(name, *ips)
    _response, data = GandiV5.post url('hosts'), { 'name' => name, 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Get a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @return [nil] name was not found
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_record(name)
    records = @glue_records.key?(name) ? @glue_records : fetch_glue_records
    records.key?(name) ? records.select { |k, _v| k == name } : nil
  end

  # Update a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_glue_record(name, *ips)
    _response, data = GandiV5.put url('hosts', name), { 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Delete a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#delete-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def delete_glue_record(name)
    _response, data = GandiV5.delete url('hosts', name)
    @glue_records ||= {}
    @glue_records.delete(name)
    data['message']
  end

  # @see GandiV5::Email::Mailbox.list
  def mailboxes(**params)
    GandiV5::Email::Mailbox.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Slot.list
  def mailbox_slots(**params)
    GandiV5::Email::Slot.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Forward.list
  def email_forwards(**params)
    GandiV5::Email::Forward.list(**params, fqdn: fqdn)
  end

  # Create (register) a new domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains
  # @param fqdn [String, #to_s] the fully qualified domain name to create.
  # @param dry_run [Boolean]
  #   whether the details should be checked instead of actually creating the domain.
  # @param sharing_id [String] either:
  #   * nil (default) - nothing special happens
  #   * an organization ID - pay using another organization
  #     (you need to have billing permissions on the organization
  #     and use the same organization name for the domain name's owner).
  #     The invoice will be edited using this organization's information.
  #   * a reseller ID - buy a domain for a customer using a reseller account
  #     (you need to have billing permissions on the reseller organization
  #     and have your customer's information for the owner).
  #     The invoice will be edited using the reseller organization's information.
  # @param owner [GandiV5::Domain::Contact, #to_gandi, #to_h] (required)
  #   the owner of the new domain.
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the administrative contact for the new domain.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the billing contact for the new domain.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the technical contact for the new domain.
  # @param claims [String] (optional) unknown - not documented at Gandi.
  # @param currency ["EUR", "USD", "GBP", "TWD", "CNY"] (optional)
  #   the currency you wish to be charged in.
  # @param duration [Integer] (optional, default 1, minimum 1 maximum 10)
  #   how many years to register for.
  # @param enforce_premium [Boolean] (optional)
  #   must be set to true if the domain is a premium domain.
  # @param extra_parameters [Hash, #to_gandi, #to_json] (optional)
  #   unknown - not documented at Gandi.
  # @param lang [String] (optional)
  #   ISO-639-2 language code of the domain, required for some IDN domains.
  # @param nameserver_ips [Hash<String => Array<String>>, #to_gandi, #to_json] (optional)
  #   For glue records only - dictionnary associating a nameserver to a list of IP addresses.
  # @param nameservers [Array<String>, #to_gandi, #to_json] (optional)
  #   List of nameservers. Gandi's LiveDNS nameservers are used if omitted..
  # @param price [Numeric, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param resellee_id [String, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param smd [String, #to_gandi, #to_json] (optional)
  #   Contents of a Signed Mark Data file (used for newgtld sunrises, tld_period must be sunrise).
  # @param tld_period ["sunrise", "landrush", "eap1", "eap2", "eap3", "eap4", "eap5", "eap6",
  #   "eap7", "eap8", "eap9", "golive", #to_gandi, #to_json] (optional)
  #   @see https://docs.gandi.net/en/domain_names/register/new_gtld.html
  # @return [GandiV5::Domain] the created domain
  # @return [Hash] if doing a dry run, you get what Gandi returns
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def self.create(fqdn, dry_run: false, sharing_id: nil, **params)
    fail ArgumentError, 'missing keyword: owner' unless params.key?(:owner)

    body = params.merge(fqdn: fqdn)
                 .transform_values { |val| val.respond_to?(:to_gandi) ? val.to_gandi : val }
                 .to_json
    url_ = sharing_id ? "#{url}?sharing_id=#{sharing_id}" : url

    response, data = GandiV5.post(url_, body, 'Dry-Run': dry_run ? 1 : 0)
    dry_run ? data : fetch(response.headers[:location].split('/').last)
  end

  # Get information on a domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain
  # @param fqdn [String, #to_s] the fully qualified domain name to fetch.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.fetch(fqdn)
    _response, data = GandiV5.get url(fqdn)
    domain = from_gandi data
    domain.auto_renew.domain = fqdn
    domain
  end

  # List domains.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains
  # @param page [#each<Integer, #to_s>] the page(s) of results to retrieve.
  #   If page is not provided keep querying until an empty list is returned.
  #   If page responds to .each then iterate until an empty list is returned.
  # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page.
  # @param fqdn [String, #to_s] (optional)
  #   filters the list by domain name, with optional patterns.
  #   e.g. "example.net", "example.*", "*ample.com"
  # @param sort_by [String, #to_s] (optional default "fqdn") how to sort the list.
  # @param tld [String, #to_s] (optional) used to filter by just the top level domain.
  # @return [Array<GandiV5::Domain>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.list(page: (1..), per_page: 100, **params)
    page = [page.to_i] unless page.respond_to?(:each)

    domains = []
    page.each do |page_number|
      _resp, data = GandiV5.get url, params: params.merge(page: page_number, per_page: per_page)
      break if data.empty?

      domains += data.map { |domain| from_gandi domain }
      break if data.count < per_page
    end
    domains
  end

  private

  def url(*extra)
    "#{BASE}domain/domains/" +
      CGI.escape(fqdn) +
      (extra.empty? ? '' : "/#{extra.join('/')}")
  end

  def self.url(fqdn = nil)
    "#{BASE}domain/domains" +
      (fqdn ? "/#{CGI.escape fqdn}" : '')
  end
  private_class_method :url
end

#fqdn_unicodeString (readonly)

Returns fully qualified domain name, written in unicode. @see docs.gandi.net/en/domain_names/register/idn.html.

Returns:



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/gandi_v5/domain.rb', line 52

class Domain
  include GandiV5::Data

  SERVICES = {
    gandidns: 'GandiDNS',
    gandilivedns: 'LiveDNS',
    dnssec: 'DNSSEC',
    certificate: 'SSL Certificate',
    paas: 'PAAS',
    redirection: 'Redirection',
    gandimail: 'GandiMail',
    packmail: 'PackMail',
    blog: 'Blog',
    hosting: 'Hosting',
    site: 'Site',
    mailboxv2: 'MailboxV2'
  }.freeze

  STATUSES = {
    clientHold: 'clientHold',
    clientUpdateProhibited: 'clientUpdateProhibited',
    clientTransferProhibited: 'clientTransferProhibited',
    clientDeleteProhibited: 'clientDeleteProhibited',
    clientRenewProhibited: 'clientRenewProhibited',
    serverHold: 'serverHold',
    pendingTransfer: 'pendingTransfer',
    serverTransferProhibited: 'serverTransferProhibited'
  }.freeze

  CONTACTS_CONVERTER = lambda { |hash|
    break {} if hash.nil?

    hash = hash.transform_keys(&:to_sym)
               .transform_values { |value| GandiV5::Domain::Contact.from_gandi value }

    hash.define_singleton_method(:owner) { send :'[]', :owner }
    hash.define_singleton_method(:admin) { send :'[]', :admin }
    hash.define_singleton_method(:bill) { send :'[]', :bill }
    hash.define_singleton_method(:tech) { send :'[]', :tech }

    hash
  }
  private_constant :CONTACTS_CONVERTER

  members :fqdn, :fqdn_unicode, :tld, :can_tld_lock, :tags, :owner, :domain_owner
  member :sharing_uuid, gandi_key: 'sharing_id'
  member :name_servers, gandi_key: 'nameservers'
  member :auth_info, gandi_key: 'authinfo'
  member :organisation_owner, gandi_key: 'orga_owner'
  member :uuid, gandi_key: 'id'

  member(
    :dates,
    converter: GandiV5::Domain::Dates
  )
  member(
    :sharing_space,
    gandi_key: 'sharing_space',
    converter: GandiV5::Domain::SharingSpace
  )

  member(
    :auto_renew,
    gandi_key: 'autorenew',
    converter: GandiV5::Data::Converter.new(from_gandi: lambda { |hash|
      break nil if hash.nil?
      break nil if hash.eql?(true)
      break GandiV5::Domain::AutoRenew.from_gandi('enabled' => false) if hash.eql?(false)

      GandiV5::Domain::AutoRenew.from_gandi hash
    })
  )

  member(
    :contacts,
    converter: GandiV5::Data::Converter.new(from_gandi: CONTACTS_CONVERTER)
  )

  member(
    :name_server,
    gandi_key: 'nameserver',
    converter: GandiV5::Data::Converter.new(from_gandi: ->(hash) { hash&.[]('current')&.to_sym })
  )
  member :services, converter: GandiV5::Data::Converter::Symbol, array: true
  member :status, converter: GandiV5::Data::Converter::Symbol, array: true
  member :trustee_roles, converter: GandiV5::Data::Converter::Symbol, array: true

  alias domain_uuid uuid

  # rubocop:disable Style/AsciiComments
  # Returns the string representation of the domain.
  # @return [String] e.g. "example.com", "😀.com (xn--e28h.uk.com)"
  # rubocop:enable Style/AsciiComments
  def to_s
    string = fqdn_unicode
    string += " (#{fqdn})" unless fqdn == fqdn_unicode
    string
  end

  # Contacts for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def contacts
    @contacts ||= fetch_contacts
  end

  # Requery Gandi for the domain's contacts.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_contacts
    _response, data = GandiV5.get url('contacts')
    self.contacts = data.transform_keys(&:to_sym)
    CONTACTS_CONVERTER.call contacts
  end

  # Update some of the domain's contacts.
  # @see https://api.gandi.net/docs/domains#patch-v5-domain-domains-domain-contacts
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new administrative contact.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new billing contact.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new technical contact.
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_contacts(admin: nil, bill: nil, tech: nil)
    body = {
      admin: admin.respond_to?(:to_gandi) ? admin.to_gandi : admin,
      bill: bill.respond_to?(:to_gandi) ? bill.to_gandi : bill,
      tech: tech.respond_to?(:to_gandi) ? tech.to_gandi : tech
    }.reject { |_k, v| v.nil? }.to_json

    GandiV5.patch url('contacts'), body
    fetch_contacts
  end

  # Renewal information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renewal_information
    @renewal_information ||= fetch_renewal_information
  end

  # Requery Gandi for the domain's renewal information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_renewal_information
    _response, data = GandiV5.get url('renew')
    data = data['renew'].merge('contracts' => data['contracts'])
    @renewal_information = GandiV5::Domain::RenewalInformation.from_gandi data
  end

  # Renew domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-renew
  # @param duration [Integer, #to_s] how long to renew for (in years).
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renew_for(duration = 1)
    body = { duration: duration }.to_json
    _response, data = GandiV5.post url('renew'), body
    data['message']
  end

  # Restoration information for the domain.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore_information
    @restore_information ||= fetch_restore_information
  end

  # Requery Gandi for the domain's restore information.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_restore_information
    _response, data = GandiV5.get url('restore')
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi data
  rescue RestClient::NotFound
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi restorable: false
  end

  # Restore an expired domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-restore
  # @return [String] The confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore
    _response, data = GandiV5.post url('restore'), '{}'
    data['message']
  end

  # Requery Gandi fo this domain's information.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def refresh
    _response, data = GandiV5.get url
    from_gandi data
    auto_renew.domain = self
  end

  # Get the price for renewing this domain.
  # @param currency [String] the currency to get the price in (e.g. GBP)
  # @param period [Integer] the number of year(s) renewal to get the price for
  # @return [GandiV5::Domain::Availability::Product::Price]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def renewal_price(currency: 'GBP', period: 1)
    arguments = { processes: [:renew], currency: currency, period: period }
    GandiV5::Domain::Availability.fetch(fqdn, **arguments)
                                 .products.first
                                 .prices.first
  end

  # LiveDNS information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def livedns
    @livedns ||= fetch_livedns
  end

  # Requery Gandi for the domain's LiveDNS information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_livedns
    _response, data = GandiV5.get url('livedns')
    @livedns = GandiV5::Domain::LiveDNS.from_gandi data
    @name_server = @livedns.current
    @name_servers = @livedns.name_servers
    @livedns
  end

  # Enable LiveDNS for the domain.
  # If you want to disable LiveDNS, change the nameservers.
  # Please note that if the domain is on the classic Gandi DNS,
  # this will also perform a copy of all existing records immediately afterwards.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-livedns
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def enable_livedns
    _response, data = GandiV5.post url('livedns')
    data['message']
  end

  # Name servers for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def name_servers
    @name_servers ||= fetch_name_servers
  end

  # Requery Gandi for the domain's name servers.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_name_servers
    _response, data = GandiV5.get url('nameservers')
    @name_servers = data
  end

  # Update name servers in Gandi.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-nameservers
  # @param nameservers [Array<String>] the name servers to use.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_name_servers(nameservers)
    _response, data = GandiV5.put url('nameservers'), { 'nameservers' => nameservers }.to_json
    @name_servers = nameservers
    data['message']
  end

  # Glue records for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_records
    @glue_records ||= fetch_glue_records
  end

  # Requery Gandi for the domain's glue records.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_glue_records
    _response, data = GandiV5.get url('hosts')
    @glue_records = data.map { |record| record.values_at('name', 'ips') }.to_h
  end

  # Add a new glue record to the domain in Gandi.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def add_glue_record(name, *ips)
    _response, data = GandiV5.post url('hosts'), { 'name' => name, 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Get a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @return [nil] name was not found
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_record(name)
    records = @glue_records.key?(name) ? @glue_records : fetch_glue_records
    records.key?(name) ? records.select { |k, _v| k == name } : nil
  end

  # Update a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_glue_record(name, *ips)
    _response, data = GandiV5.put url('hosts', name), { 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Delete a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#delete-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def delete_glue_record(name)
    _response, data = GandiV5.delete url('hosts', name)
    @glue_records ||= {}
    @glue_records.delete(name)
    data['message']
  end

  # @see GandiV5::Email::Mailbox.list
  def mailboxes(**params)
    GandiV5::Email::Mailbox.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Slot.list
  def mailbox_slots(**params)
    GandiV5::Email::Slot.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Forward.list
  def email_forwards(**params)
    GandiV5::Email::Forward.list(**params, fqdn: fqdn)
  end

  # Create (register) a new domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains
  # @param fqdn [String, #to_s] the fully qualified domain name to create.
  # @param dry_run [Boolean]
  #   whether the details should be checked instead of actually creating the domain.
  # @param sharing_id [String] either:
  #   * nil (default) - nothing special happens
  #   * an organization ID - pay using another organization
  #     (you need to have billing permissions on the organization
  #     and use the same organization name for the domain name's owner).
  #     The invoice will be edited using this organization's information.
  #   * a reseller ID - buy a domain for a customer using a reseller account
  #     (you need to have billing permissions on the reseller organization
  #     and have your customer's information for the owner).
  #     The invoice will be edited using the reseller organization's information.
  # @param owner [GandiV5::Domain::Contact, #to_gandi, #to_h] (required)
  #   the owner of the new domain.
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the administrative contact for the new domain.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the billing contact for the new domain.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the technical contact for the new domain.
  # @param claims [String] (optional) unknown - not documented at Gandi.
  # @param currency ["EUR", "USD", "GBP", "TWD", "CNY"] (optional)
  #   the currency you wish to be charged in.
  # @param duration [Integer] (optional, default 1, minimum 1 maximum 10)
  #   how many years to register for.
  # @param enforce_premium [Boolean] (optional)
  #   must be set to true if the domain is a premium domain.
  # @param extra_parameters [Hash, #to_gandi, #to_json] (optional)
  #   unknown - not documented at Gandi.
  # @param lang [String] (optional)
  #   ISO-639-2 language code of the domain, required for some IDN domains.
  # @param nameserver_ips [Hash<String => Array<String>>, #to_gandi, #to_json] (optional)
  #   For glue records only - dictionnary associating a nameserver to a list of IP addresses.
  # @param nameservers [Array<String>, #to_gandi, #to_json] (optional)
  #   List of nameservers. Gandi's LiveDNS nameservers are used if omitted..
  # @param price [Numeric, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param resellee_id [String, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param smd [String, #to_gandi, #to_json] (optional)
  #   Contents of a Signed Mark Data file (used for newgtld sunrises, tld_period must be sunrise).
  # @param tld_period ["sunrise", "landrush", "eap1", "eap2", "eap3", "eap4", "eap5", "eap6",
  #   "eap7", "eap8", "eap9", "golive", #to_gandi, #to_json] (optional)
  #   @see https://docs.gandi.net/en/domain_names/register/new_gtld.html
  # @return [GandiV5::Domain] the created domain
  # @return [Hash] if doing a dry run, you get what Gandi returns
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def self.create(fqdn, dry_run: false, sharing_id: nil, **params)
    fail ArgumentError, 'missing keyword: owner' unless params.key?(:owner)

    body = params.merge(fqdn: fqdn)
                 .transform_values { |val| val.respond_to?(:to_gandi) ? val.to_gandi : val }
                 .to_json
    url_ = sharing_id ? "#{url}?sharing_id=#{sharing_id}" : url

    response, data = GandiV5.post(url_, body, 'Dry-Run': dry_run ? 1 : 0)
    dry_run ? data : fetch(response.headers[:location].split('/').last)
  end

  # Get information on a domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain
  # @param fqdn [String, #to_s] the fully qualified domain name to fetch.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.fetch(fqdn)
    _response, data = GandiV5.get url(fqdn)
    domain = from_gandi data
    domain.auto_renew.domain = fqdn
    domain
  end

  # List domains.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains
  # @param page [#each<Integer, #to_s>] the page(s) of results to retrieve.
  #   If page is not provided keep querying until an empty list is returned.
  #   If page responds to .each then iterate until an empty list is returned.
  # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page.
  # @param fqdn [String, #to_s] (optional)
  #   filters the list by domain name, with optional patterns.
  #   e.g. "example.net", "example.*", "*ample.com"
  # @param sort_by [String, #to_s] (optional default "fqdn") how to sort the list.
  # @param tld [String, #to_s] (optional) used to filter by just the top level domain.
  # @return [Array<GandiV5::Domain>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.list(page: (1..), per_page: 100, **params)
    page = [page.to_i] unless page.respond_to?(:each)

    domains = []
    page.each do |page_number|
      _resp, data = GandiV5.get url, params: params.merge(page: page_number, per_page: per_page)
      break if data.empty?

      domains += data.map { |domain| from_gandi domain }
      break if data.count < per_page
    end
    domains
  end

  private

  def url(*extra)
    "#{BASE}domain/domains/" +
      CGI.escape(fqdn) +
      (extra.empty? ? '' : "/#{extra.join('/')}")
  end

  def self.url(fqdn = nil)
    "#{BASE}domain/domains" +
      (fqdn ? "/#{CGI.escape fqdn}" : '')
  end
  private_class_method :url
end

#name_serverSymbol (readonly)

Returns:

  • (Symbol)


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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/gandi_v5/domain.rb', line 52

class Domain
  include GandiV5::Data

  SERVICES = {
    gandidns: 'GandiDNS',
    gandilivedns: 'LiveDNS',
    dnssec: 'DNSSEC',
    certificate: 'SSL Certificate',
    paas: 'PAAS',
    redirection: 'Redirection',
    gandimail: 'GandiMail',
    packmail: 'PackMail',
    blog: 'Blog',
    hosting: 'Hosting',
    site: 'Site',
    mailboxv2: 'MailboxV2'
  }.freeze

  STATUSES = {
    clientHold: 'clientHold',
    clientUpdateProhibited: 'clientUpdateProhibited',
    clientTransferProhibited: 'clientTransferProhibited',
    clientDeleteProhibited: 'clientDeleteProhibited',
    clientRenewProhibited: 'clientRenewProhibited',
    serverHold: 'serverHold',
    pendingTransfer: 'pendingTransfer',
    serverTransferProhibited: 'serverTransferProhibited'
  }.freeze

  CONTACTS_CONVERTER = lambda { |hash|
    break {} if hash.nil?

    hash = hash.transform_keys(&:to_sym)
               .transform_values { |value| GandiV5::Domain::Contact.from_gandi value }

    hash.define_singleton_method(:owner) { send :'[]', :owner }
    hash.define_singleton_method(:admin) { send :'[]', :admin }
    hash.define_singleton_method(:bill) { send :'[]', :bill }
    hash.define_singleton_method(:tech) { send :'[]', :tech }

    hash
  }
  private_constant :CONTACTS_CONVERTER

  members :fqdn, :fqdn_unicode, :tld, :can_tld_lock, :tags, :owner, :domain_owner
  member :sharing_uuid, gandi_key: 'sharing_id'
  member :name_servers, gandi_key: 'nameservers'
  member :auth_info, gandi_key: 'authinfo'
  member :organisation_owner, gandi_key: 'orga_owner'
  member :uuid, gandi_key: 'id'

  member(
    :dates,
    converter: GandiV5::Domain::Dates
  )
  member(
    :sharing_space,
    gandi_key: 'sharing_space',
    converter: GandiV5::Domain::SharingSpace
  )

  member(
    :auto_renew,
    gandi_key: 'autorenew',
    converter: GandiV5::Data::Converter.new(from_gandi: lambda { |hash|
      break nil if hash.nil?
      break nil if hash.eql?(true)
      break GandiV5::Domain::AutoRenew.from_gandi('enabled' => false) if hash.eql?(false)

      GandiV5::Domain::AutoRenew.from_gandi hash
    })
  )

  member(
    :contacts,
    converter: GandiV5::Data::Converter.new(from_gandi: CONTACTS_CONVERTER)
  )

  member(
    :name_server,
    gandi_key: 'nameserver',
    converter: GandiV5::Data::Converter.new(from_gandi: ->(hash) { hash&.[]('current')&.to_sym })
  )
  member :services, converter: GandiV5::Data::Converter::Symbol, array: true
  member :status, converter: GandiV5::Data::Converter::Symbol, array: true
  member :trustee_roles, converter: GandiV5::Data::Converter::Symbol, array: true

  alias domain_uuid uuid

  # rubocop:disable Style/AsciiComments
  # Returns the string representation of the domain.
  # @return [String] e.g. "example.com", "😀.com (xn--e28h.uk.com)"
  # rubocop:enable Style/AsciiComments
  def to_s
    string = fqdn_unicode
    string += " (#{fqdn})" unless fqdn == fqdn_unicode
    string
  end

  # Contacts for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def contacts
    @contacts ||= fetch_contacts
  end

  # Requery Gandi for the domain's contacts.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_contacts
    _response, data = GandiV5.get url('contacts')
    self.contacts = data.transform_keys(&:to_sym)
    CONTACTS_CONVERTER.call contacts
  end

  # Update some of the domain's contacts.
  # @see https://api.gandi.net/docs/domains#patch-v5-domain-domains-domain-contacts
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new administrative contact.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new billing contact.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new technical contact.
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_contacts(admin: nil, bill: nil, tech: nil)
    body = {
      admin: admin.respond_to?(:to_gandi) ? admin.to_gandi : admin,
      bill: bill.respond_to?(:to_gandi) ? bill.to_gandi : bill,
      tech: tech.respond_to?(:to_gandi) ? tech.to_gandi : tech
    }.reject { |_k, v| v.nil? }.to_json

    GandiV5.patch url('contacts'), body
    fetch_contacts
  end

  # Renewal information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renewal_information
    @renewal_information ||= fetch_renewal_information
  end

  # Requery Gandi for the domain's renewal information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_renewal_information
    _response, data = GandiV5.get url('renew')
    data = data['renew'].merge('contracts' => data['contracts'])
    @renewal_information = GandiV5::Domain::RenewalInformation.from_gandi data
  end

  # Renew domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-renew
  # @param duration [Integer, #to_s] how long to renew for (in years).
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renew_for(duration = 1)
    body = { duration: duration }.to_json
    _response, data = GandiV5.post url('renew'), body
    data['message']
  end

  # Restoration information for the domain.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore_information
    @restore_information ||= fetch_restore_information
  end

  # Requery Gandi for the domain's restore information.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_restore_information
    _response, data = GandiV5.get url('restore')
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi data
  rescue RestClient::NotFound
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi restorable: false
  end

  # Restore an expired domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-restore
  # @return [String] The confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore
    _response, data = GandiV5.post url('restore'), '{}'
    data['message']
  end

  # Requery Gandi fo this domain's information.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def refresh
    _response, data = GandiV5.get url
    from_gandi data
    auto_renew.domain = self
  end

  # Get the price for renewing this domain.
  # @param currency [String] the currency to get the price in (e.g. GBP)
  # @param period [Integer] the number of year(s) renewal to get the price for
  # @return [GandiV5::Domain::Availability::Product::Price]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def renewal_price(currency: 'GBP', period: 1)
    arguments = { processes: [:renew], currency: currency, period: period }
    GandiV5::Domain::Availability.fetch(fqdn, **arguments)
                                 .products.first
                                 .prices.first
  end

  # LiveDNS information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def livedns
    @livedns ||= fetch_livedns
  end

  # Requery Gandi for the domain's LiveDNS information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_livedns
    _response, data = GandiV5.get url('livedns')
    @livedns = GandiV5::Domain::LiveDNS.from_gandi data
    @name_server = @livedns.current
    @name_servers = @livedns.name_servers
    @livedns
  end

  # Enable LiveDNS for the domain.
  # If you want to disable LiveDNS, change the nameservers.
  # Please note that if the domain is on the classic Gandi DNS,
  # this will also perform a copy of all existing records immediately afterwards.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-livedns
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def enable_livedns
    _response, data = GandiV5.post url('livedns')
    data['message']
  end

  # Name servers for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def name_servers
    @name_servers ||= fetch_name_servers
  end

  # Requery Gandi for the domain's name servers.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_name_servers
    _response, data = GandiV5.get url('nameservers')
    @name_servers = data
  end

  # Update name servers in Gandi.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-nameservers
  # @param nameservers [Array<String>] the name servers to use.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_name_servers(nameservers)
    _response, data = GandiV5.put url('nameservers'), { 'nameservers' => nameservers }.to_json
    @name_servers = nameservers
    data['message']
  end

  # Glue records for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_records
    @glue_records ||= fetch_glue_records
  end

  # Requery Gandi for the domain's glue records.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_glue_records
    _response, data = GandiV5.get url('hosts')
    @glue_records = data.map { |record| record.values_at('name', 'ips') }.to_h
  end

  # Add a new glue record to the domain in Gandi.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def add_glue_record(name, *ips)
    _response, data = GandiV5.post url('hosts'), { 'name' => name, 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Get a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @return [nil] name was not found
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_record(name)
    records = @glue_records.key?(name) ? @glue_records : fetch_glue_records
    records.key?(name) ? records.select { |k, _v| k == name } : nil
  end

  # Update a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_glue_record(name, *ips)
    _response, data = GandiV5.put url('hosts', name), { 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Delete a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#delete-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def delete_glue_record(name)
    _response, data = GandiV5.delete url('hosts', name)
    @glue_records ||= {}
    @glue_records.delete(name)
    data['message']
  end

  # @see GandiV5::Email::Mailbox.list
  def mailboxes(**params)
    GandiV5::Email::Mailbox.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Slot.list
  def mailbox_slots(**params)
    GandiV5::Email::Slot.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Forward.list
  def email_forwards(**params)
    GandiV5::Email::Forward.list(**params, fqdn: fqdn)
  end

  # Create (register) a new domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains
  # @param fqdn [String, #to_s] the fully qualified domain name to create.
  # @param dry_run [Boolean]
  #   whether the details should be checked instead of actually creating the domain.
  # @param sharing_id [String] either:
  #   * nil (default) - nothing special happens
  #   * an organization ID - pay using another organization
  #     (you need to have billing permissions on the organization
  #     and use the same organization name for the domain name's owner).
  #     The invoice will be edited using this organization's information.
  #   * a reseller ID - buy a domain for a customer using a reseller account
  #     (you need to have billing permissions on the reseller organization
  #     and have your customer's information for the owner).
  #     The invoice will be edited using the reseller organization's information.
  # @param owner [GandiV5::Domain::Contact, #to_gandi, #to_h] (required)
  #   the owner of the new domain.
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the administrative contact for the new domain.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the billing contact for the new domain.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the technical contact for the new domain.
  # @param claims [String] (optional) unknown - not documented at Gandi.
  # @param currency ["EUR", "USD", "GBP", "TWD", "CNY"] (optional)
  #   the currency you wish to be charged in.
  # @param duration [Integer] (optional, default 1, minimum 1 maximum 10)
  #   how many years to register for.
  # @param enforce_premium [Boolean] (optional)
  #   must be set to true if the domain is a premium domain.
  # @param extra_parameters [Hash, #to_gandi, #to_json] (optional)
  #   unknown - not documented at Gandi.
  # @param lang [String] (optional)
  #   ISO-639-2 language code of the domain, required for some IDN domains.
  # @param nameserver_ips [Hash<String => Array<String>>, #to_gandi, #to_json] (optional)
  #   For glue records only - dictionnary associating a nameserver to a list of IP addresses.
  # @param nameservers [Array<String>, #to_gandi, #to_json] (optional)
  #   List of nameservers. Gandi's LiveDNS nameservers are used if omitted..
  # @param price [Numeric, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param resellee_id [String, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param smd [String, #to_gandi, #to_json] (optional)
  #   Contents of a Signed Mark Data file (used for newgtld sunrises, tld_period must be sunrise).
  # @param tld_period ["sunrise", "landrush", "eap1", "eap2", "eap3", "eap4", "eap5", "eap6",
  #   "eap7", "eap8", "eap9", "golive", #to_gandi, #to_json] (optional)
  #   @see https://docs.gandi.net/en/domain_names/register/new_gtld.html
  # @return [GandiV5::Domain] the created domain
  # @return [Hash] if doing a dry run, you get what Gandi returns
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def self.create(fqdn, dry_run: false, sharing_id: nil, **params)
    fail ArgumentError, 'missing keyword: owner' unless params.key?(:owner)

    body = params.merge(fqdn: fqdn)
                 .transform_values { |val| val.respond_to?(:to_gandi) ? val.to_gandi : val }
                 .to_json
    url_ = sharing_id ? "#{url}?sharing_id=#{sharing_id}" : url

    response, data = GandiV5.post(url_, body, 'Dry-Run': dry_run ? 1 : 0)
    dry_run ? data : fetch(response.headers[:location].split('/').last)
  end

  # Get information on a domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain
  # @param fqdn [String, #to_s] the fully qualified domain name to fetch.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.fetch(fqdn)
    _response, data = GandiV5.get url(fqdn)
    domain = from_gandi data
    domain.auto_renew.domain = fqdn
    domain
  end

  # List domains.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains
  # @param page [#each<Integer, #to_s>] the page(s) of results to retrieve.
  #   If page is not provided keep querying until an empty list is returned.
  #   If page responds to .each then iterate until an empty list is returned.
  # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page.
  # @param fqdn [String, #to_s] (optional)
  #   filters the list by domain name, with optional patterns.
  #   e.g. "example.net", "example.*", "*ample.com"
  # @param sort_by [String, #to_s] (optional default "fqdn") how to sort the list.
  # @param tld [String, #to_s] (optional) used to filter by just the top level domain.
  # @return [Array<GandiV5::Domain>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.list(page: (1..), per_page: 100, **params)
    page = [page.to_i] unless page.respond_to?(:each)

    domains = []
    page.each do |page_number|
      _resp, data = GandiV5.get url, params: params.merge(page: page_number, per_page: per_page)
      break if data.empty?

      domains += data.map { |domain| from_gandi domain }
      break if data.count < per_page
    end
    domains
  end

  private

  def url(*extra)
    "#{BASE}domain/domains/" +
      CGI.escape(fqdn) +
      (extra.empty? ? '' : "/#{extra.join('/')}")
  end

  def self.url(fqdn = nil)
    "#{BASE}domain/domains" +
      (fqdn ? "/#{CGI.escape fqdn}" : '')
  end
  private_class_method :url
end

#name_serversArray<String> (readonly)

Name servers for the domain.

Returns:

  • (Array<String>)

Raises:

See Also:



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/gandi_v5/domain.rb', line 52

class Domain
  include GandiV5::Data

  SERVICES = {
    gandidns: 'GandiDNS',
    gandilivedns: 'LiveDNS',
    dnssec: 'DNSSEC',
    certificate: 'SSL Certificate',
    paas: 'PAAS',
    redirection: 'Redirection',
    gandimail: 'GandiMail',
    packmail: 'PackMail',
    blog: 'Blog',
    hosting: 'Hosting',
    site: 'Site',
    mailboxv2: 'MailboxV2'
  }.freeze

  STATUSES = {
    clientHold: 'clientHold',
    clientUpdateProhibited: 'clientUpdateProhibited',
    clientTransferProhibited: 'clientTransferProhibited',
    clientDeleteProhibited: 'clientDeleteProhibited',
    clientRenewProhibited: 'clientRenewProhibited',
    serverHold: 'serverHold',
    pendingTransfer: 'pendingTransfer',
    serverTransferProhibited: 'serverTransferProhibited'
  }.freeze

  CONTACTS_CONVERTER = lambda { |hash|
    break {} if hash.nil?

    hash = hash.transform_keys(&:to_sym)
               .transform_values { |value| GandiV5::Domain::Contact.from_gandi value }

    hash.define_singleton_method(:owner) { send :'[]', :owner }
    hash.define_singleton_method(:admin) { send :'[]', :admin }
    hash.define_singleton_method(:bill) { send :'[]', :bill }
    hash.define_singleton_method(:tech) { send :'[]', :tech }

    hash
  }
  private_constant :CONTACTS_CONVERTER

  members :fqdn, :fqdn_unicode, :tld, :can_tld_lock, :tags, :owner, :domain_owner
  member :sharing_uuid, gandi_key: 'sharing_id'
  member :name_servers, gandi_key: 'nameservers'
  member :auth_info, gandi_key: 'authinfo'
  member :organisation_owner, gandi_key: 'orga_owner'
  member :uuid, gandi_key: 'id'

  member(
    :dates,
    converter: GandiV5::Domain::Dates
  )
  member(
    :sharing_space,
    gandi_key: 'sharing_space',
    converter: GandiV5::Domain::SharingSpace
  )

  member(
    :auto_renew,
    gandi_key: 'autorenew',
    converter: GandiV5::Data::Converter.new(from_gandi: lambda { |hash|
      break nil if hash.nil?
      break nil if hash.eql?(true)
      break GandiV5::Domain::AutoRenew.from_gandi('enabled' => false) if hash.eql?(false)

      GandiV5::Domain::AutoRenew.from_gandi hash
    })
  )

  member(
    :contacts,
    converter: GandiV5::Data::Converter.new(from_gandi: CONTACTS_CONVERTER)
  )

  member(
    :name_server,
    gandi_key: 'nameserver',
    converter: GandiV5::Data::Converter.new(from_gandi: ->(hash) { hash&.[]('current')&.to_sym })
  )
  member :services, converter: GandiV5::Data::Converter::Symbol, array: true
  member :status, converter: GandiV5::Data::Converter::Symbol, array: true
  member :trustee_roles, converter: GandiV5::Data::Converter::Symbol, array: true

  alias domain_uuid uuid

  # rubocop:disable Style/AsciiComments
  # Returns the string representation of the domain.
  # @return [String] e.g. "example.com", "😀.com (xn--e28h.uk.com)"
  # rubocop:enable Style/AsciiComments
  def to_s
    string = fqdn_unicode
    string += " (#{fqdn})" unless fqdn == fqdn_unicode
    string
  end

  # Contacts for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def contacts
    @contacts ||= fetch_contacts
  end

  # Requery Gandi for the domain's contacts.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_contacts
    _response, data = GandiV5.get url('contacts')
    self.contacts = data.transform_keys(&:to_sym)
    CONTACTS_CONVERTER.call contacts
  end

  # Update some of the domain's contacts.
  # @see https://api.gandi.net/docs/domains#patch-v5-domain-domains-domain-contacts
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new administrative contact.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new billing contact.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new technical contact.
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_contacts(admin: nil, bill: nil, tech: nil)
    body = {
      admin: admin.respond_to?(:to_gandi) ? admin.to_gandi : admin,
      bill: bill.respond_to?(:to_gandi) ? bill.to_gandi : bill,
      tech: tech.respond_to?(:to_gandi) ? tech.to_gandi : tech
    }.reject { |_k, v| v.nil? }.to_json

    GandiV5.patch url('contacts'), body
    fetch_contacts
  end

  # Renewal information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renewal_information
    @renewal_information ||= fetch_renewal_information
  end

  # Requery Gandi for the domain's renewal information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_renewal_information
    _response, data = GandiV5.get url('renew')
    data = data['renew'].merge('contracts' => data['contracts'])
    @renewal_information = GandiV5::Domain::RenewalInformation.from_gandi data
  end

  # Renew domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-renew
  # @param duration [Integer, #to_s] how long to renew for (in years).
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renew_for(duration = 1)
    body = { duration: duration }.to_json
    _response, data = GandiV5.post url('renew'), body
    data['message']
  end

  # Restoration information for the domain.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore_information
    @restore_information ||= fetch_restore_information
  end

  # Requery Gandi for the domain's restore information.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_restore_information
    _response, data = GandiV5.get url('restore')
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi data
  rescue RestClient::NotFound
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi restorable: false
  end

  # Restore an expired domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-restore
  # @return [String] The confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore
    _response, data = GandiV5.post url('restore'), '{}'
    data['message']
  end

  # Requery Gandi fo this domain's information.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def refresh
    _response, data = GandiV5.get url
    from_gandi data
    auto_renew.domain = self
  end

  # Get the price for renewing this domain.
  # @param currency [String] the currency to get the price in (e.g. GBP)
  # @param period [Integer] the number of year(s) renewal to get the price for
  # @return [GandiV5::Domain::Availability::Product::Price]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def renewal_price(currency: 'GBP', period: 1)
    arguments = { processes: [:renew], currency: currency, period: period }
    GandiV5::Domain::Availability.fetch(fqdn, **arguments)
                                 .products.first
                                 .prices.first
  end

  # LiveDNS information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def livedns
    @livedns ||= fetch_livedns
  end

  # Requery Gandi for the domain's LiveDNS information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_livedns
    _response, data = GandiV5.get url('livedns')
    @livedns = GandiV5::Domain::LiveDNS.from_gandi data
    @name_server = @livedns.current
    @name_servers = @livedns.name_servers
    @livedns
  end

  # Enable LiveDNS for the domain.
  # If you want to disable LiveDNS, change the nameservers.
  # Please note that if the domain is on the classic Gandi DNS,
  # this will also perform a copy of all existing records immediately afterwards.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-livedns
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def enable_livedns
    _response, data = GandiV5.post url('livedns')
    data['message']
  end

  # Name servers for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def name_servers
    @name_servers ||= fetch_name_servers
  end

  # Requery Gandi for the domain's name servers.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_name_servers
    _response, data = GandiV5.get url('nameservers')
    @name_servers = data
  end

  # Update name servers in Gandi.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-nameservers
  # @param nameservers [Array<String>] the name servers to use.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_name_servers(nameservers)
    _response, data = GandiV5.put url('nameservers'), { 'nameservers' => nameservers }.to_json
    @name_servers = nameservers
    data['message']
  end

  # Glue records for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_records
    @glue_records ||= fetch_glue_records
  end

  # Requery Gandi for the domain's glue records.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_glue_records
    _response, data = GandiV5.get url('hosts')
    @glue_records = data.map { |record| record.values_at('name', 'ips') }.to_h
  end

  # Add a new glue record to the domain in Gandi.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def add_glue_record(name, *ips)
    _response, data = GandiV5.post url('hosts'), { 'name' => name, 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Get a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @return [nil] name was not found
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_record(name)
    records = @glue_records.key?(name) ? @glue_records : fetch_glue_records
    records.key?(name) ? records.select { |k, _v| k == name } : nil
  end

  # Update a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_glue_record(name, *ips)
    _response, data = GandiV5.put url('hosts', name), { 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Delete a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#delete-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def delete_glue_record(name)
    _response, data = GandiV5.delete url('hosts', name)
    @glue_records ||= {}
    @glue_records.delete(name)
    data['message']
  end

  # @see GandiV5::Email::Mailbox.list
  def mailboxes(**params)
    GandiV5::Email::Mailbox.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Slot.list
  def mailbox_slots(**params)
    GandiV5::Email::Slot.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Forward.list
  def email_forwards(**params)
    GandiV5::Email::Forward.list(**params, fqdn: fqdn)
  end

  # Create (register) a new domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains
  # @param fqdn [String, #to_s] the fully qualified domain name to create.
  # @param dry_run [Boolean]
  #   whether the details should be checked instead of actually creating the domain.
  # @param sharing_id [String] either:
  #   * nil (default) - nothing special happens
  #   * an organization ID - pay using another organization
  #     (you need to have billing permissions on the organization
  #     and use the same organization name for the domain name's owner).
  #     The invoice will be edited using this organization's information.
  #   * a reseller ID - buy a domain for a customer using a reseller account
  #     (you need to have billing permissions on the reseller organization
  #     and have your customer's information for the owner).
  #     The invoice will be edited using the reseller organization's information.
  # @param owner [GandiV5::Domain::Contact, #to_gandi, #to_h] (required)
  #   the owner of the new domain.
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the administrative contact for the new domain.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the billing contact for the new domain.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the technical contact for the new domain.
  # @param claims [String] (optional) unknown - not documented at Gandi.
  # @param currency ["EUR", "USD", "GBP", "TWD", "CNY"] (optional)
  #   the currency you wish to be charged in.
  # @param duration [Integer] (optional, default 1, minimum 1 maximum 10)
  #   how many years to register for.
  # @param enforce_premium [Boolean] (optional)
  #   must be set to true if the domain is a premium domain.
  # @param extra_parameters [Hash, #to_gandi, #to_json] (optional)
  #   unknown - not documented at Gandi.
  # @param lang [String] (optional)
  #   ISO-639-2 language code of the domain, required for some IDN domains.
  # @param nameserver_ips [Hash<String => Array<String>>, #to_gandi, #to_json] (optional)
  #   For glue records only - dictionnary associating a nameserver to a list of IP addresses.
  # @param nameservers [Array<String>, #to_gandi, #to_json] (optional)
  #   List of nameservers. Gandi's LiveDNS nameservers are used if omitted..
  # @param price [Numeric, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param resellee_id [String, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param smd [String, #to_gandi, #to_json] (optional)
  #   Contents of a Signed Mark Data file (used for newgtld sunrises, tld_period must be sunrise).
  # @param tld_period ["sunrise", "landrush", "eap1", "eap2", "eap3", "eap4", "eap5", "eap6",
  #   "eap7", "eap8", "eap9", "golive", #to_gandi, #to_json] (optional)
  #   @see https://docs.gandi.net/en/domain_names/register/new_gtld.html
  # @return [GandiV5::Domain] the created domain
  # @return [Hash] if doing a dry run, you get what Gandi returns
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def self.create(fqdn, dry_run: false, sharing_id: nil, **params)
    fail ArgumentError, 'missing keyword: owner' unless params.key?(:owner)

    body = params.merge(fqdn: fqdn)
                 .transform_values { |val| val.respond_to?(:to_gandi) ? val.to_gandi : val }
                 .to_json
    url_ = sharing_id ? "#{url}?sharing_id=#{sharing_id}" : url

    response, data = GandiV5.post(url_, body, 'Dry-Run': dry_run ? 1 : 0)
    dry_run ? data : fetch(response.headers[:location].split('/').last)
  end

  # Get information on a domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain
  # @param fqdn [String, #to_s] the fully qualified domain name to fetch.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.fetch(fqdn)
    _response, data = GandiV5.get url(fqdn)
    domain = from_gandi data
    domain.auto_renew.domain = fqdn
    domain
  end

  # List domains.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains
  # @param page [#each<Integer, #to_s>] the page(s) of results to retrieve.
  #   If page is not provided keep querying until an empty list is returned.
  #   If page responds to .each then iterate until an empty list is returned.
  # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page.
  # @param fqdn [String, #to_s] (optional)
  #   filters the list by domain name, with optional patterns.
  #   e.g. "example.net", "example.*", "*ample.com"
  # @param sort_by [String, #to_s] (optional default "fqdn") how to sort the list.
  # @param tld [String, #to_s] (optional) used to filter by just the top level domain.
  # @return [Array<GandiV5::Domain>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.list(page: (1..), per_page: 100, **params)
    page = [page.to_i] unless page.respond_to?(:each)

    domains = []
    page.each do |page_number|
      _resp, data = GandiV5.get url, params: params.merge(page: page_number, per_page: per_page)
      break if data.empty?

      domains += data.map { |domain| from_gandi domain }
      break if data.count < per_page
    end
    domains
  end

  private

  def url(*extra)
    "#{BASE}domain/domains/" +
      CGI.escape(fqdn) +
      (extra.empty? ? '' : "/#{extra.join('/')}")
  end

  def self.url(fqdn = nil)
    "#{BASE}domain/domains" +
      (fqdn ? "/#{CGI.escape fqdn}" : '')
  end
  private_class_method :url
end

#organisation_owner_String (readonly)

Returns _String.

Returns:

  • (_String)

    _String



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/gandi_v5/domain.rb', line 52

class Domain
  include GandiV5::Data

  SERVICES = {
    gandidns: 'GandiDNS',
    gandilivedns: 'LiveDNS',
    dnssec: 'DNSSEC',
    certificate: 'SSL Certificate',
    paas: 'PAAS',
    redirection: 'Redirection',
    gandimail: 'GandiMail',
    packmail: 'PackMail',
    blog: 'Blog',
    hosting: 'Hosting',
    site: 'Site',
    mailboxv2: 'MailboxV2'
  }.freeze

  STATUSES = {
    clientHold: 'clientHold',
    clientUpdateProhibited: 'clientUpdateProhibited',
    clientTransferProhibited: 'clientTransferProhibited',
    clientDeleteProhibited: 'clientDeleteProhibited',
    clientRenewProhibited: 'clientRenewProhibited',
    serverHold: 'serverHold',
    pendingTransfer: 'pendingTransfer',
    serverTransferProhibited: 'serverTransferProhibited'
  }.freeze

  CONTACTS_CONVERTER = lambda { |hash|
    break {} if hash.nil?

    hash = hash.transform_keys(&:to_sym)
               .transform_values { |value| GandiV5::Domain::Contact.from_gandi value }

    hash.define_singleton_method(:owner) { send :'[]', :owner }
    hash.define_singleton_method(:admin) { send :'[]', :admin }
    hash.define_singleton_method(:bill) { send :'[]', :bill }
    hash.define_singleton_method(:tech) { send :'[]', :tech }

    hash
  }
  private_constant :CONTACTS_CONVERTER

  members :fqdn, :fqdn_unicode, :tld, :can_tld_lock, :tags, :owner, :domain_owner
  member :sharing_uuid, gandi_key: 'sharing_id'
  member :name_servers, gandi_key: 'nameservers'
  member :auth_info, gandi_key: 'authinfo'
  member :organisation_owner, gandi_key: 'orga_owner'
  member :uuid, gandi_key: 'id'

  member(
    :dates,
    converter: GandiV5::Domain::Dates
  )
  member(
    :sharing_space,
    gandi_key: 'sharing_space',
    converter: GandiV5::Domain::SharingSpace
  )

  member(
    :auto_renew,
    gandi_key: 'autorenew',
    converter: GandiV5::Data::Converter.new(from_gandi: lambda { |hash|
      break nil if hash.nil?
      break nil if hash.eql?(true)
      break GandiV5::Domain::AutoRenew.from_gandi('enabled' => false) if hash.eql?(false)

      GandiV5::Domain::AutoRenew.from_gandi hash
    })
  )

  member(
    :contacts,
    converter: GandiV5::Data::Converter.new(from_gandi: CONTACTS_CONVERTER)
  )

  member(
    :name_server,
    gandi_key: 'nameserver',
    converter: GandiV5::Data::Converter.new(from_gandi: ->(hash) { hash&.[]('current')&.to_sym })
  )
  member :services, converter: GandiV5::Data::Converter::Symbol, array: true
  member :status, converter: GandiV5::Data::Converter::Symbol, array: true
  member :trustee_roles, converter: GandiV5::Data::Converter::Symbol, array: true

  alias domain_uuid uuid

  # rubocop:disable Style/AsciiComments
  # Returns the string representation of the domain.
  # @return [String] e.g. "example.com", "😀.com (xn--e28h.uk.com)"
  # rubocop:enable Style/AsciiComments
  def to_s
    string = fqdn_unicode
    string += " (#{fqdn})" unless fqdn == fqdn_unicode
    string
  end

  # Contacts for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def contacts
    @contacts ||= fetch_contacts
  end

  # Requery Gandi for the domain's contacts.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_contacts
    _response, data = GandiV5.get url('contacts')
    self.contacts = data.transform_keys(&:to_sym)
    CONTACTS_CONVERTER.call contacts
  end

  # Update some of the domain's contacts.
  # @see https://api.gandi.net/docs/domains#patch-v5-domain-domains-domain-contacts
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new administrative contact.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new billing contact.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new technical contact.
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_contacts(admin: nil, bill: nil, tech: nil)
    body = {
      admin: admin.respond_to?(:to_gandi) ? admin.to_gandi : admin,
      bill: bill.respond_to?(:to_gandi) ? bill.to_gandi : bill,
      tech: tech.respond_to?(:to_gandi) ? tech.to_gandi : tech
    }.reject { |_k, v| v.nil? }.to_json

    GandiV5.patch url('contacts'), body
    fetch_contacts
  end

  # Renewal information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renewal_information
    @renewal_information ||= fetch_renewal_information
  end

  # Requery Gandi for the domain's renewal information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_renewal_information
    _response, data = GandiV5.get url('renew')
    data = data['renew'].merge('contracts' => data['contracts'])
    @renewal_information = GandiV5::Domain::RenewalInformation.from_gandi data
  end

  # Renew domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-renew
  # @param duration [Integer, #to_s] how long to renew for (in years).
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renew_for(duration = 1)
    body = { duration: duration }.to_json
    _response, data = GandiV5.post url('renew'), body
    data['message']
  end

  # Restoration information for the domain.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore_information
    @restore_information ||= fetch_restore_information
  end

  # Requery Gandi for the domain's restore information.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_restore_information
    _response, data = GandiV5.get url('restore')
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi data
  rescue RestClient::NotFound
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi restorable: false
  end

  # Restore an expired domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-restore
  # @return [String] The confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore
    _response, data = GandiV5.post url('restore'), '{}'
    data['message']
  end

  # Requery Gandi fo this domain's information.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def refresh
    _response, data = GandiV5.get url
    from_gandi data
    auto_renew.domain = self
  end

  # Get the price for renewing this domain.
  # @param currency [String] the currency to get the price in (e.g. GBP)
  # @param period [Integer] the number of year(s) renewal to get the price for
  # @return [GandiV5::Domain::Availability::Product::Price]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def renewal_price(currency: 'GBP', period: 1)
    arguments = { processes: [:renew], currency: currency, period: period }
    GandiV5::Domain::Availability.fetch(fqdn, **arguments)
                                 .products.first
                                 .prices.first
  end

  # LiveDNS information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def livedns
    @livedns ||= fetch_livedns
  end

  # Requery Gandi for the domain's LiveDNS information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_livedns
    _response, data = GandiV5.get url('livedns')
    @livedns = GandiV5::Domain::LiveDNS.from_gandi data
    @name_server = @livedns.current
    @name_servers = @livedns.name_servers
    @livedns
  end

  # Enable LiveDNS for the domain.
  # If you want to disable LiveDNS, change the nameservers.
  # Please note that if the domain is on the classic Gandi DNS,
  # this will also perform a copy of all existing records immediately afterwards.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-livedns
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def enable_livedns
    _response, data = GandiV5.post url('livedns')
    data['message']
  end

  # Name servers for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def name_servers
    @name_servers ||= fetch_name_servers
  end

  # Requery Gandi for the domain's name servers.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_name_servers
    _response, data = GandiV5.get url('nameservers')
    @name_servers = data
  end

  # Update name servers in Gandi.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-nameservers
  # @param nameservers [Array<String>] the name servers to use.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_name_servers(nameservers)
    _response, data = GandiV5.put url('nameservers'), { 'nameservers' => nameservers }.to_json
    @name_servers = nameservers
    data['message']
  end

  # Glue records for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_records
    @glue_records ||= fetch_glue_records
  end

  # Requery Gandi for the domain's glue records.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_glue_records
    _response, data = GandiV5.get url('hosts')
    @glue_records = data.map { |record| record.values_at('name', 'ips') }.to_h
  end

  # Add a new glue record to the domain in Gandi.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def add_glue_record(name, *ips)
    _response, data = GandiV5.post url('hosts'), { 'name' => name, 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Get a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @return [nil] name was not found
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_record(name)
    records = @glue_records.key?(name) ? @glue_records : fetch_glue_records
    records.key?(name) ? records.select { |k, _v| k == name } : nil
  end

  # Update a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_glue_record(name, *ips)
    _response, data = GandiV5.put url('hosts', name), { 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Delete a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#delete-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def delete_glue_record(name)
    _response, data = GandiV5.delete url('hosts', name)
    @glue_records ||= {}
    @glue_records.delete(name)
    data['message']
  end

  # @see GandiV5::Email::Mailbox.list
  def mailboxes(**params)
    GandiV5::Email::Mailbox.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Slot.list
  def mailbox_slots(**params)
    GandiV5::Email::Slot.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Forward.list
  def email_forwards(**params)
    GandiV5::Email::Forward.list(**params, fqdn: fqdn)
  end

  # Create (register) a new domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains
  # @param fqdn [String, #to_s] the fully qualified domain name to create.
  # @param dry_run [Boolean]
  #   whether the details should be checked instead of actually creating the domain.
  # @param sharing_id [String] either:
  #   * nil (default) - nothing special happens
  #   * an organization ID - pay using another organization
  #     (you need to have billing permissions on the organization
  #     and use the same organization name for the domain name's owner).
  #     The invoice will be edited using this organization's information.
  #   * a reseller ID - buy a domain for a customer using a reseller account
  #     (you need to have billing permissions on the reseller organization
  #     and have your customer's information for the owner).
  #     The invoice will be edited using the reseller organization's information.
  # @param owner [GandiV5::Domain::Contact, #to_gandi, #to_h] (required)
  #   the owner of the new domain.
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the administrative contact for the new domain.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the billing contact for the new domain.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the technical contact for the new domain.
  # @param claims [String] (optional) unknown - not documented at Gandi.
  # @param currency ["EUR", "USD", "GBP", "TWD", "CNY"] (optional)
  #   the currency you wish to be charged in.
  # @param duration [Integer] (optional, default 1, minimum 1 maximum 10)
  #   how many years to register for.
  # @param enforce_premium [Boolean] (optional)
  #   must be set to true if the domain is a premium domain.
  # @param extra_parameters [Hash, #to_gandi, #to_json] (optional)
  #   unknown - not documented at Gandi.
  # @param lang [String] (optional)
  #   ISO-639-2 language code of the domain, required for some IDN domains.
  # @param nameserver_ips [Hash<String => Array<String>>, #to_gandi, #to_json] (optional)
  #   For glue records only - dictionnary associating a nameserver to a list of IP addresses.
  # @param nameservers [Array<String>, #to_gandi, #to_json] (optional)
  #   List of nameservers. Gandi's LiveDNS nameservers are used if omitted..
  # @param price [Numeric, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param resellee_id [String, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param smd [String, #to_gandi, #to_json] (optional)
  #   Contents of a Signed Mark Data file (used for newgtld sunrises, tld_period must be sunrise).
  # @param tld_period ["sunrise", "landrush", "eap1", "eap2", "eap3", "eap4", "eap5", "eap6",
  #   "eap7", "eap8", "eap9", "golive", #to_gandi, #to_json] (optional)
  #   @see https://docs.gandi.net/en/domain_names/register/new_gtld.html
  # @return [GandiV5::Domain] the created domain
  # @return [Hash] if doing a dry run, you get what Gandi returns
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def self.create(fqdn, dry_run: false, sharing_id: nil, **params)
    fail ArgumentError, 'missing keyword: owner' unless params.key?(:owner)

    body = params.merge(fqdn: fqdn)
                 .transform_values { |val| val.respond_to?(:to_gandi) ? val.to_gandi : val }
                 .to_json
    url_ = sharing_id ? "#{url}?sharing_id=#{sharing_id}" : url

    response, data = GandiV5.post(url_, body, 'Dry-Run': dry_run ? 1 : 0)
    dry_run ? data : fetch(response.headers[:location].split('/').last)
  end

  # Get information on a domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain
  # @param fqdn [String, #to_s] the fully qualified domain name to fetch.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.fetch(fqdn)
    _response, data = GandiV5.get url(fqdn)
    domain = from_gandi data
    domain.auto_renew.domain = fqdn
    domain
  end

  # List domains.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains
  # @param page [#each<Integer, #to_s>] the page(s) of results to retrieve.
  #   If page is not provided keep querying until an empty list is returned.
  #   If page responds to .each then iterate until an empty list is returned.
  # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page.
  # @param fqdn [String, #to_s] (optional)
  #   filters the list by domain name, with optional patterns.
  #   e.g. "example.net", "example.*", "*ample.com"
  # @param sort_by [String, #to_s] (optional default "fqdn") how to sort the list.
  # @param tld [String, #to_s] (optional) used to filter by just the top level domain.
  # @return [Array<GandiV5::Domain>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.list(page: (1..), per_page: 100, **params)
    page = [page.to_i] unless page.respond_to?(:each)

    domains = []
    page.each do |page_number|
      _resp, data = GandiV5.get url, params: params.merge(page: page_number, per_page: per_page)
      break if data.empty?

      domains += data.map { |domain| from_gandi domain }
      break if data.count < per_page
    end
    domains
  end

  private

  def url(*extra)
    "#{BASE}domain/domains/" +
      CGI.escape(fqdn) +
      (extra.empty? ? '' : "/#{extra.join('/')}")
  end

  def self.url(fqdn = nil)
    "#{BASE}domain/domains" +
      (fqdn ? "/#{CGI.escape fqdn}" : '')
  end
  private_class_method :url
end

#ownerString (readonly)

Returns:

  • (String)


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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/gandi_v5/domain.rb', line 52

class Domain
  include GandiV5::Data

  SERVICES = {
    gandidns: 'GandiDNS',
    gandilivedns: 'LiveDNS',
    dnssec: 'DNSSEC',
    certificate: 'SSL Certificate',
    paas: 'PAAS',
    redirection: 'Redirection',
    gandimail: 'GandiMail',
    packmail: 'PackMail',
    blog: 'Blog',
    hosting: 'Hosting',
    site: 'Site',
    mailboxv2: 'MailboxV2'
  }.freeze

  STATUSES = {
    clientHold: 'clientHold',
    clientUpdateProhibited: 'clientUpdateProhibited',
    clientTransferProhibited: 'clientTransferProhibited',
    clientDeleteProhibited: 'clientDeleteProhibited',
    clientRenewProhibited: 'clientRenewProhibited',
    serverHold: 'serverHold',
    pendingTransfer: 'pendingTransfer',
    serverTransferProhibited: 'serverTransferProhibited'
  }.freeze

  CONTACTS_CONVERTER = lambda { |hash|
    break {} if hash.nil?

    hash = hash.transform_keys(&:to_sym)
               .transform_values { |value| GandiV5::Domain::Contact.from_gandi value }

    hash.define_singleton_method(:owner) { send :'[]', :owner }
    hash.define_singleton_method(:admin) { send :'[]', :admin }
    hash.define_singleton_method(:bill) { send :'[]', :bill }
    hash.define_singleton_method(:tech) { send :'[]', :tech }

    hash
  }
  private_constant :CONTACTS_CONVERTER

  members :fqdn, :fqdn_unicode, :tld, :can_tld_lock, :tags, :owner, :domain_owner
  member :sharing_uuid, gandi_key: 'sharing_id'
  member :name_servers, gandi_key: 'nameservers'
  member :auth_info, gandi_key: 'authinfo'
  member :organisation_owner, gandi_key: 'orga_owner'
  member :uuid, gandi_key: 'id'

  member(
    :dates,
    converter: GandiV5::Domain::Dates
  )
  member(
    :sharing_space,
    gandi_key: 'sharing_space',
    converter: GandiV5::Domain::SharingSpace
  )

  member(
    :auto_renew,
    gandi_key: 'autorenew',
    converter: GandiV5::Data::Converter.new(from_gandi: lambda { |hash|
      break nil if hash.nil?
      break nil if hash.eql?(true)
      break GandiV5::Domain::AutoRenew.from_gandi('enabled' => false) if hash.eql?(false)

      GandiV5::Domain::AutoRenew.from_gandi hash
    })
  )

  member(
    :contacts,
    converter: GandiV5::Data::Converter.new(from_gandi: CONTACTS_CONVERTER)
  )

  member(
    :name_server,
    gandi_key: 'nameserver',
    converter: GandiV5::Data::Converter.new(from_gandi: ->(hash) { hash&.[]('current')&.to_sym })
  )
  member :services, converter: GandiV5::Data::Converter::Symbol, array: true
  member :status, converter: GandiV5::Data::Converter::Symbol, array: true
  member :trustee_roles, converter: GandiV5::Data::Converter::Symbol, array: true

  alias domain_uuid uuid

  # rubocop:disable Style/AsciiComments
  # Returns the string representation of the domain.
  # @return [String] e.g. "example.com", "😀.com (xn--e28h.uk.com)"
  # rubocop:enable Style/AsciiComments
  def to_s
    string = fqdn_unicode
    string += " (#{fqdn})" unless fqdn == fqdn_unicode
    string
  end

  # Contacts for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def contacts
    @contacts ||= fetch_contacts
  end

  # Requery Gandi for the domain's contacts.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_contacts
    _response, data = GandiV5.get url('contacts')
    self.contacts = data.transform_keys(&:to_sym)
    CONTACTS_CONVERTER.call contacts
  end

  # Update some of the domain's contacts.
  # @see https://api.gandi.net/docs/domains#patch-v5-domain-domains-domain-contacts
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new administrative contact.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new billing contact.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new technical contact.
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_contacts(admin: nil, bill: nil, tech: nil)
    body = {
      admin: admin.respond_to?(:to_gandi) ? admin.to_gandi : admin,
      bill: bill.respond_to?(:to_gandi) ? bill.to_gandi : bill,
      tech: tech.respond_to?(:to_gandi) ? tech.to_gandi : tech
    }.reject { |_k, v| v.nil? }.to_json

    GandiV5.patch url('contacts'), body
    fetch_contacts
  end

  # Renewal information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renewal_information
    @renewal_information ||= fetch_renewal_information
  end

  # Requery Gandi for the domain's renewal information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_renewal_information
    _response, data = GandiV5.get url('renew')
    data = data['renew'].merge('contracts' => data['contracts'])
    @renewal_information = GandiV5::Domain::RenewalInformation.from_gandi data
  end

  # Renew domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-renew
  # @param duration [Integer, #to_s] how long to renew for (in years).
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renew_for(duration = 1)
    body = { duration: duration }.to_json
    _response, data = GandiV5.post url('renew'), body
    data['message']
  end

  # Restoration information for the domain.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore_information
    @restore_information ||= fetch_restore_information
  end

  # Requery Gandi for the domain's restore information.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_restore_information
    _response, data = GandiV5.get url('restore')
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi data
  rescue RestClient::NotFound
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi restorable: false
  end

  # Restore an expired domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-restore
  # @return [String] The confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore
    _response, data = GandiV5.post url('restore'), '{}'
    data['message']
  end

  # Requery Gandi fo this domain's information.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def refresh
    _response, data = GandiV5.get url
    from_gandi data
    auto_renew.domain = self
  end

  # Get the price for renewing this domain.
  # @param currency [String] the currency to get the price in (e.g. GBP)
  # @param period [Integer] the number of year(s) renewal to get the price for
  # @return [GandiV5::Domain::Availability::Product::Price]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def renewal_price(currency: 'GBP', period: 1)
    arguments = { processes: [:renew], currency: currency, period: period }
    GandiV5::Domain::Availability.fetch(fqdn, **arguments)
                                 .products.first
                                 .prices.first
  end

  # LiveDNS information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def livedns
    @livedns ||= fetch_livedns
  end

  # Requery Gandi for the domain's LiveDNS information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_livedns
    _response, data = GandiV5.get url('livedns')
    @livedns = GandiV5::Domain::LiveDNS.from_gandi data
    @name_server = @livedns.current
    @name_servers = @livedns.name_servers
    @livedns
  end

  # Enable LiveDNS for the domain.
  # If you want to disable LiveDNS, change the nameservers.
  # Please note that if the domain is on the classic Gandi DNS,
  # this will also perform a copy of all existing records immediately afterwards.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-livedns
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def enable_livedns
    _response, data = GandiV5.post url('livedns')
    data['message']
  end

  # Name servers for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def name_servers
    @name_servers ||= fetch_name_servers
  end

  # Requery Gandi for the domain's name servers.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_name_servers
    _response, data = GandiV5.get url('nameservers')
    @name_servers = data
  end

  # Update name servers in Gandi.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-nameservers
  # @param nameservers [Array<String>] the name servers to use.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_name_servers(nameservers)
    _response, data = GandiV5.put url('nameservers'), { 'nameservers' => nameservers }.to_json
    @name_servers = nameservers
    data['message']
  end

  # Glue records for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_records
    @glue_records ||= fetch_glue_records
  end

  # Requery Gandi for the domain's glue records.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_glue_records
    _response, data = GandiV5.get url('hosts')
    @glue_records = data.map { |record| record.values_at('name', 'ips') }.to_h
  end

  # Add a new glue record to the domain in Gandi.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def add_glue_record(name, *ips)
    _response, data = GandiV5.post url('hosts'), { 'name' => name, 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Get a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @return [nil] name was not found
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_record(name)
    records = @glue_records.key?(name) ? @glue_records : fetch_glue_records
    records.key?(name) ? records.select { |k, _v| k == name } : nil
  end

  # Update a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_glue_record(name, *ips)
    _response, data = GandiV5.put url('hosts', name), { 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Delete a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#delete-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def delete_glue_record(name)
    _response, data = GandiV5.delete url('hosts', name)
    @glue_records ||= {}
    @glue_records.delete(name)
    data['message']
  end

  # @see GandiV5::Email::Mailbox.list
  def mailboxes(**params)
    GandiV5::Email::Mailbox.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Slot.list
  def mailbox_slots(**params)
    GandiV5::Email::Slot.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Forward.list
  def email_forwards(**params)
    GandiV5::Email::Forward.list(**params, fqdn: fqdn)
  end

  # Create (register) a new domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains
  # @param fqdn [String, #to_s] the fully qualified domain name to create.
  # @param dry_run [Boolean]
  #   whether the details should be checked instead of actually creating the domain.
  # @param sharing_id [String] either:
  #   * nil (default) - nothing special happens
  #   * an organization ID - pay using another organization
  #     (you need to have billing permissions on the organization
  #     and use the same organization name for the domain name's owner).
  #     The invoice will be edited using this organization's information.
  #   * a reseller ID - buy a domain for a customer using a reseller account
  #     (you need to have billing permissions on the reseller organization
  #     and have your customer's information for the owner).
  #     The invoice will be edited using the reseller organization's information.
  # @param owner [GandiV5::Domain::Contact, #to_gandi, #to_h] (required)
  #   the owner of the new domain.
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the administrative contact for the new domain.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the billing contact for the new domain.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the technical contact for the new domain.
  # @param claims [String] (optional) unknown - not documented at Gandi.
  # @param currency ["EUR", "USD", "GBP", "TWD", "CNY"] (optional)
  #   the currency you wish to be charged in.
  # @param duration [Integer] (optional, default 1, minimum 1 maximum 10)
  #   how many years to register for.
  # @param enforce_premium [Boolean] (optional)
  #   must be set to true if the domain is a premium domain.
  # @param extra_parameters [Hash, #to_gandi, #to_json] (optional)
  #   unknown - not documented at Gandi.
  # @param lang [String] (optional)
  #   ISO-639-2 language code of the domain, required for some IDN domains.
  # @param nameserver_ips [Hash<String => Array<String>>, #to_gandi, #to_json] (optional)
  #   For glue records only - dictionnary associating a nameserver to a list of IP addresses.
  # @param nameservers [Array<String>, #to_gandi, #to_json] (optional)
  #   List of nameservers. Gandi's LiveDNS nameservers are used if omitted..
  # @param price [Numeric, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param resellee_id [String, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param smd [String, #to_gandi, #to_json] (optional)
  #   Contents of a Signed Mark Data file (used for newgtld sunrises, tld_period must be sunrise).
  # @param tld_period ["sunrise", "landrush", "eap1", "eap2", "eap3", "eap4", "eap5", "eap6",
  #   "eap7", "eap8", "eap9", "golive", #to_gandi, #to_json] (optional)
  #   @see https://docs.gandi.net/en/domain_names/register/new_gtld.html
  # @return [GandiV5::Domain] the created domain
  # @return [Hash] if doing a dry run, you get what Gandi returns
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def self.create(fqdn, dry_run: false, sharing_id: nil, **params)
    fail ArgumentError, 'missing keyword: owner' unless params.key?(:owner)

    body = params.merge(fqdn: fqdn)
                 .transform_values { |val| val.respond_to?(:to_gandi) ? val.to_gandi : val }
                 .to_json
    url_ = sharing_id ? "#{url}?sharing_id=#{sharing_id}" : url

    response, data = GandiV5.post(url_, body, 'Dry-Run': dry_run ? 1 : 0)
    dry_run ? data : fetch(response.headers[:location].split('/').last)
  end

  # Get information on a domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain
  # @param fqdn [String, #to_s] the fully qualified domain name to fetch.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.fetch(fqdn)
    _response, data = GandiV5.get url(fqdn)
    domain = from_gandi data
    domain.auto_renew.domain = fqdn
    domain
  end

  # List domains.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains
  # @param page [#each<Integer, #to_s>] the page(s) of results to retrieve.
  #   If page is not provided keep querying until an empty list is returned.
  #   If page responds to .each then iterate until an empty list is returned.
  # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page.
  # @param fqdn [String, #to_s] (optional)
  #   filters the list by domain name, with optional patterns.
  #   e.g. "example.net", "example.*", "*ample.com"
  # @param sort_by [String, #to_s] (optional default "fqdn") how to sort the list.
  # @param tld [String, #to_s] (optional) used to filter by just the top level domain.
  # @return [Array<GandiV5::Domain>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.list(page: (1..), per_page: 100, **params)
    page = [page.to_i] unless page.respond_to?(:each)

    domains = []
    page.each do |page_number|
      _resp, data = GandiV5.get url, params: params.merge(page: page_number, per_page: per_page)
      break if data.empty?

      domains += data.map { |domain| from_gandi domain }
      break if data.count < per_page
    end
    domains
  end

  private

  def url(*extra)
    "#{BASE}domain/domains/" +
      CGI.escape(fqdn) +
      (extra.empty? ? '' : "/#{extra.join('/')}")
  end

  def self.url(fqdn = nil)
    "#{BASE}domain/domains" +
      (fqdn ? "/#{CGI.escape fqdn}" : '')
  end
  private_class_method :url
end

#servicesArray<Symbol> (readonly)

Returns list of Gandi services attached to this domain. gandidns, redirection, gandimail, packmail, dnssec, blog, hosting, paas, site, certificate, gandilivedns, mailboxv2.

Returns:

  • (Array<Symbol>)

    list of Gandi services attached to this domain. gandidns, redirection, gandimail, packmail, dnssec, blog, hosting, paas, site, certificate, gandilivedns, mailboxv2



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/gandi_v5/domain.rb', line 52

class Domain
  include GandiV5::Data

  SERVICES = {
    gandidns: 'GandiDNS',
    gandilivedns: 'LiveDNS',
    dnssec: 'DNSSEC',
    certificate: 'SSL Certificate',
    paas: 'PAAS',
    redirection: 'Redirection',
    gandimail: 'GandiMail',
    packmail: 'PackMail',
    blog: 'Blog',
    hosting: 'Hosting',
    site: 'Site',
    mailboxv2: 'MailboxV2'
  }.freeze

  STATUSES = {
    clientHold: 'clientHold',
    clientUpdateProhibited: 'clientUpdateProhibited',
    clientTransferProhibited: 'clientTransferProhibited',
    clientDeleteProhibited: 'clientDeleteProhibited',
    clientRenewProhibited: 'clientRenewProhibited',
    serverHold: 'serverHold',
    pendingTransfer: 'pendingTransfer',
    serverTransferProhibited: 'serverTransferProhibited'
  }.freeze

  CONTACTS_CONVERTER = lambda { |hash|
    break {} if hash.nil?

    hash = hash.transform_keys(&:to_sym)
               .transform_values { |value| GandiV5::Domain::Contact.from_gandi value }

    hash.define_singleton_method(:owner) { send :'[]', :owner }
    hash.define_singleton_method(:admin) { send :'[]', :admin }
    hash.define_singleton_method(:bill) { send :'[]', :bill }
    hash.define_singleton_method(:tech) { send :'[]', :tech }

    hash
  }
  private_constant :CONTACTS_CONVERTER

  members :fqdn, :fqdn_unicode, :tld, :can_tld_lock, :tags, :owner, :domain_owner
  member :sharing_uuid, gandi_key: 'sharing_id'
  member :name_servers, gandi_key: 'nameservers'
  member :auth_info, gandi_key: 'authinfo'
  member :organisation_owner, gandi_key: 'orga_owner'
  member :uuid, gandi_key: 'id'

  member(
    :dates,
    converter: GandiV5::Domain::Dates
  )
  member(
    :sharing_space,
    gandi_key: 'sharing_space',
    converter: GandiV5::Domain::SharingSpace
  )

  member(
    :auto_renew,
    gandi_key: 'autorenew',
    converter: GandiV5::Data::Converter.new(from_gandi: lambda { |hash|
      break nil if hash.nil?
      break nil if hash.eql?(true)
      break GandiV5::Domain::AutoRenew.from_gandi('enabled' => false) if hash.eql?(false)

      GandiV5::Domain::AutoRenew.from_gandi hash
    })
  )

  member(
    :contacts,
    converter: GandiV5::Data::Converter.new(from_gandi: CONTACTS_CONVERTER)
  )

  member(
    :name_server,
    gandi_key: 'nameserver',
    converter: GandiV5::Data::Converter.new(from_gandi: ->(hash) { hash&.[]('current')&.to_sym })
  )
  member :services, converter: GandiV5::Data::Converter::Symbol, array: true
  member :status, converter: GandiV5::Data::Converter::Symbol, array: true
  member :trustee_roles, converter: GandiV5::Data::Converter::Symbol, array: true

  alias domain_uuid uuid

  # rubocop:disable Style/AsciiComments
  # Returns the string representation of the domain.
  # @return [String] e.g. "example.com", "😀.com (xn--e28h.uk.com)"
  # rubocop:enable Style/AsciiComments
  def to_s
    string = fqdn_unicode
    string += " (#{fqdn})" unless fqdn == fqdn_unicode
    string
  end

  # Contacts for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def contacts
    @contacts ||= fetch_contacts
  end

  # Requery Gandi for the domain's contacts.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_contacts
    _response, data = GandiV5.get url('contacts')
    self.contacts = data.transform_keys(&:to_sym)
    CONTACTS_CONVERTER.call contacts
  end

  # Update some of the domain's contacts.
  # @see https://api.gandi.net/docs/domains#patch-v5-domain-domains-domain-contacts
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new administrative contact.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new billing contact.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new technical contact.
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_contacts(admin: nil, bill: nil, tech: nil)
    body = {
      admin: admin.respond_to?(:to_gandi) ? admin.to_gandi : admin,
      bill: bill.respond_to?(:to_gandi) ? bill.to_gandi : bill,
      tech: tech.respond_to?(:to_gandi) ? tech.to_gandi : tech
    }.reject { |_k, v| v.nil? }.to_json

    GandiV5.patch url('contacts'), body
    fetch_contacts
  end

  # Renewal information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renewal_information
    @renewal_information ||= fetch_renewal_information
  end

  # Requery Gandi for the domain's renewal information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_renewal_information
    _response, data = GandiV5.get url('renew')
    data = data['renew'].merge('contracts' => data['contracts'])
    @renewal_information = GandiV5::Domain::RenewalInformation.from_gandi data
  end

  # Renew domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-renew
  # @param duration [Integer, #to_s] how long to renew for (in years).
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renew_for(duration = 1)
    body = { duration: duration }.to_json
    _response, data = GandiV5.post url('renew'), body
    data['message']
  end

  # Restoration information for the domain.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore_information
    @restore_information ||= fetch_restore_information
  end

  # Requery Gandi for the domain's restore information.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_restore_information
    _response, data = GandiV5.get url('restore')
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi data
  rescue RestClient::NotFound
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi restorable: false
  end

  # Restore an expired domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-restore
  # @return [String] The confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore
    _response, data = GandiV5.post url('restore'), '{}'
    data['message']
  end

  # Requery Gandi fo this domain's information.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def refresh
    _response, data = GandiV5.get url
    from_gandi data
    auto_renew.domain = self
  end

  # Get the price for renewing this domain.
  # @param currency [String] the currency to get the price in (e.g. GBP)
  # @param period [Integer] the number of year(s) renewal to get the price for
  # @return [GandiV5::Domain::Availability::Product::Price]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def renewal_price(currency: 'GBP', period: 1)
    arguments = { processes: [:renew], currency: currency, period: period }
    GandiV5::Domain::Availability.fetch(fqdn, **arguments)
                                 .products.first
                                 .prices.first
  end

  # LiveDNS information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def livedns
    @livedns ||= fetch_livedns
  end

  # Requery Gandi for the domain's LiveDNS information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_livedns
    _response, data = GandiV5.get url('livedns')
    @livedns = GandiV5::Domain::LiveDNS.from_gandi data
    @name_server = @livedns.current
    @name_servers = @livedns.name_servers
    @livedns
  end

  # Enable LiveDNS for the domain.
  # If you want to disable LiveDNS, change the nameservers.
  # Please note that if the domain is on the classic Gandi DNS,
  # this will also perform a copy of all existing records immediately afterwards.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-livedns
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def enable_livedns
    _response, data = GandiV5.post url('livedns')
    data['message']
  end

  # Name servers for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def name_servers
    @name_servers ||= fetch_name_servers
  end

  # Requery Gandi for the domain's name servers.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_name_servers
    _response, data = GandiV5.get url('nameservers')
    @name_servers = data
  end

  # Update name servers in Gandi.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-nameservers
  # @param nameservers [Array<String>] the name servers to use.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_name_servers(nameservers)
    _response, data = GandiV5.put url('nameservers'), { 'nameservers' => nameservers }.to_json
    @name_servers = nameservers
    data['message']
  end

  # Glue records for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_records
    @glue_records ||= fetch_glue_records
  end

  # Requery Gandi for the domain's glue records.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_glue_records
    _response, data = GandiV5.get url('hosts')
    @glue_records = data.map { |record| record.values_at('name', 'ips') }.to_h
  end

  # Add a new glue record to the domain in Gandi.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def add_glue_record(name, *ips)
    _response, data = GandiV5.post url('hosts'), { 'name' => name, 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Get a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @return [nil] name was not found
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_record(name)
    records = @glue_records.key?(name) ? @glue_records : fetch_glue_records
    records.key?(name) ? records.select { |k, _v| k == name } : nil
  end

  # Update a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_glue_record(name, *ips)
    _response, data = GandiV5.put url('hosts', name), { 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Delete a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#delete-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def delete_glue_record(name)
    _response, data = GandiV5.delete url('hosts', name)
    @glue_records ||= {}
    @glue_records.delete(name)
    data['message']
  end

  # @see GandiV5::Email::Mailbox.list
  def mailboxes(**params)
    GandiV5::Email::Mailbox.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Slot.list
  def mailbox_slots(**params)
    GandiV5::Email::Slot.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Forward.list
  def email_forwards(**params)
    GandiV5::Email::Forward.list(**params, fqdn: fqdn)
  end

  # Create (register) a new domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains
  # @param fqdn [String, #to_s] the fully qualified domain name to create.
  # @param dry_run [Boolean]
  #   whether the details should be checked instead of actually creating the domain.
  # @param sharing_id [String] either:
  #   * nil (default) - nothing special happens
  #   * an organization ID - pay using another organization
  #     (you need to have billing permissions on the organization
  #     and use the same organization name for the domain name's owner).
  #     The invoice will be edited using this organization's information.
  #   * a reseller ID - buy a domain for a customer using a reseller account
  #     (you need to have billing permissions on the reseller organization
  #     and have your customer's information for the owner).
  #     The invoice will be edited using the reseller organization's information.
  # @param owner [GandiV5::Domain::Contact, #to_gandi, #to_h] (required)
  #   the owner of the new domain.
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the administrative contact for the new domain.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the billing contact for the new domain.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the technical contact for the new domain.
  # @param claims [String] (optional) unknown - not documented at Gandi.
  # @param currency ["EUR", "USD", "GBP", "TWD", "CNY"] (optional)
  #   the currency you wish to be charged in.
  # @param duration [Integer] (optional, default 1, minimum 1 maximum 10)
  #   how many years to register for.
  # @param enforce_premium [Boolean] (optional)
  #   must be set to true if the domain is a premium domain.
  # @param extra_parameters [Hash, #to_gandi, #to_json] (optional)
  #   unknown - not documented at Gandi.
  # @param lang [String] (optional)
  #   ISO-639-2 language code of the domain, required for some IDN domains.
  # @param nameserver_ips [Hash<String => Array<String>>, #to_gandi, #to_json] (optional)
  #   For glue records only - dictionnary associating a nameserver to a list of IP addresses.
  # @param nameservers [Array<String>, #to_gandi, #to_json] (optional)
  #   List of nameservers. Gandi's LiveDNS nameservers are used if omitted..
  # @param price [Numeric, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param resellee_id [String, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param smd [String, #to_gandi, #to_json] (optional)
  #   Contents of a Signed Mark Data file (used for newgtld sunrises, tld_period must be sunrise).
  # @param tld_period ["sunrise", "landrush", "eap1", "eap2", "eap3", "eap4", "eap5", "eap6",
  #   "eap7", "eap8", "eap9", "golive", #to_gandi, #to_json] (optional)
  #   @see https://docs.gandi.net/en/domain_names/register/new_gtld.html
  # @return [GandiV5::Domain] the created domain
  # @return [Hash] if doing a dry run, you get what Gandi returns
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def self.create(fqdn, dry_run: false, sharing_id: nil, **params)
    fail ArgumentError, 'missing keyword: owner' unless params.key?(:owner)

    body = params.merge(fqdn: fqdn)
                 .transform_values { |val| val.respond_to?(:to_gandi) ? val.to_gandi : val }
                 .to_json
    url_ = sharing_id ? "#{url}?sharing_id=#{sharing_id}" : url

    response, data = GandiV5.post(url_, body, 'Dry-Run': dry_run ? 1 : 0)
    dry_run ? data : fetch(response.headers[:location].split('/').last)
  end

  # Get information on a domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain
  # @param fqdn [String, #to_s] the fully qualified domain name to fetch.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.fetch(fqdn)
    _response, data = GandiV5.get url(fqdn)
    domain = from_gandi data
    domain.auto_renew.domain = fqdn
    domain
  end

  # List domains.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains
  # @param page [#each<Integer, #to_s>] the page(s) of results to retrieve.
  #   If page is not provided keep querying until an empty list is returned.
  #   If page responds to .each then iterate until an empty list is returned.
  # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page.
  # @param fqdn [String, #to_s] (optional)
  #   filters the list by domain name, with optional patterns.
  #   e.g. "example.net", "example.*", "*ample.com"
  # @param sort_by [String, #to_s] (optional default "fqdn") how to sort the list.
  # @param tld [String, #to_s] (optional) used to filter by just the top level domain.
  # @return [Array<GandiV5::Domain>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.list(page: (1..), per_page: 100, **params)
    page = [page.to_i] unless page.respond_to?(:each)

    domains = []
    page.each do |page_number|
      _resp, data = GandiV5.get url, params: params.merge(page: page_number, per_page: per_page)
      break if data.empty?

      domains += data.map { |domain| from_gandi domain }
      break if data.count < per_page
    end
    domains
  end

  private

  def url(*extra)
    "#{BASE}domain/domains/" +
      CGI.escape(fqdn) +
      (extra.empty? ? '' : "/#{extra.join('/')}")
  end

  def self.url(fqdn = nil)
    "#{BASE}domain/domains" +
      (fqdn ? "/#{CGI.escape fqdn}" : '')
  end
  private_class_method :url
end

#sharing_spaceGandiV5::Domain::SharingSpace (readonly)



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/gandi_v5/domain.rb', line 52

class Domain
  include GandiV5::Data

  SERVICES = {
    gandidns: 'GandiDNS',
    gandilivedns: 'LiveDNS',
    dnssec: 'DNSSEC',
    certificate: 'SSL Certificate',
    paas: 'PAAS',
    redirection: 'Redirection',
    gandimail: 'GandiMail',
    packmail: 'PackMail',
    blog: 'Blog',
    hosting: 'Hosting',
    site: 'Site',
    mailboxv2: 'MailboxV2'
  }.freeze

  STATUSES = {
    clientHold: 'clientHold',
    clientUpdateProhibited: 'clientUpdateProhibited',
    clientTransferProhibited: 'clientTransferProhibited',
    clientDeleteProhibited: 'clientDeleteProhibited',
    clientRenewProhibited: 'clientRenewProhibited',
    serverHold: 'serverHold',
    pendingTransfer: 'pendingTransfer',
    serverTransferProhibited: 'serverTransferProhibited'
  }.freeze

  CONTACTS_CONVERTER = lambda { |hash|
    break {} if hash.nil?

    hash = hash.transform_keys(&:to_sym)
               .transform_values { |value| GandiV5::Domain::Contact.from_gandi value }

    hash.define_singleton_method(:owner) { send :'[]', :owner }
    hash.define_singleton_method(:admin) { send :'[]', :admin }
    hash.define_singleton_method(:bill) { send :'[]', :bill }
    hash.define_singleton_method(:tech) { send :'[]', :tech }

    hash
  }
  private_constant :CONTACTS_CONVERTER

  members :fqdn, :fqdn_unicode, :tld, :can_tld_lock, :tags, :owner, :domain_owner
  member :sharing_uuid, gandi_key: 'sharing_id'
  member :name_servers, gandi_key: 'nameservers'
  member :auth_info, gandi_key: 'authinfo'
  member :organisation_owner, gandi_key: 'orga_owner'
  member :uuid, gandi_key: 'id'

  member(
    :dates,
    converter: GandiV5::Domain::Dates
  )
  member(
    :sharing_space,
    gandi_key: 'sharing_space',
    converter: GandiV5::Domain::SharingSpace
  )

  member(
    :auto_renew,
    gandi_key: 'autorenew',
    converter: GandiV5::Data::Converter.new(from_gandi: lambda { |hash|
      break nil if hash.nil?
      break nil if hash.eql?(true)
      break GandiV5::Domain::AutoRenew.from_gandi('enabled' => false) if hash.eql?(false)

      GandiV5::Domain::AutoRenew.from_gandi hash
    })
  )

  member(
    :contacts,
    converter: GandiV5::Data::Converter.new(from_gandi: CONTACTS_CONVERTER)
  )

  member(
    :name_server,
    gandi_key: 'nameserver',
    converter: GandiV5::Data::Converter.new(from_gandi: ->(hash) { hash&.[]('current')&.to_sym })
  )
  member :services, converter: GandiV5::Data::Converter::Symbol, array: true
  member :status, converter: GandiV5::Data::Converter::Symbol, array: true
  member :trustee_roles, converter: GandiV5::Data::Converter::Symbol, array: true

  alias domain_uuid uuid

  # rubocop:disable Style/AsciiComments
  # Returns the string representation of the domain.
  # @return [String] e.g. "example.com", "😀.com (xn--e28h.uk.com)"
  # rubocop:enable Style/AsciiComments
  def to_s
    string = fqdn_unicode
    string += " (#{fqdn})" unless fqdn == fqdn_unicode
    string
  end

  # Contacts for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def contacts
    @contacts ||= fetch_contacts
  end

  # Requery Gandi for the domain's contacts.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_contacts
    _response, data = GandiV5.get url('contacts')
    self.contacts = data.transform_keys(&:to_sym)
    CONTACTS_CONVERTER.call contacts
  end

  # Update some of the domain's contacts.
  # @see https://api.gandi.net/docs/domains#patch-v5-domain-domains-domain-contacts
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new administrative contact.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new billing contact.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new technical contact.
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_contacts(admin: nil, bill: nil, tech: nil)
    body = {
      admin: admin.respond_to?(:to_gandi) ? admin.to_gandi : admin,
      bill: bill.respond_to?(:to_gandi) ? bill.to_gandi : bill,
      tech: tech.respond_to?(:to_gandi) ? tech.to_gandi : tech
    }.reject { |_k, v| v.nil? }.to_json

    GandiV5.patch url('contacts'), body
    fetch_contacts
  end

  # Renewal information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renewal_information
    @renewal_information ||= fetch_renewal_information
  end

  # Requery Gandi for the domain's renewal information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_renewal_information
    _response, data = GandiV5.get url('renew')
    data = data['renew'].merge('contracts' => data['contracts'])
    @renewal_information = GandiV5::Domain::RenewalInformation.from_gandi data
  end

  # Renew domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-renew
  # @param duration [Integer, #to_s] how long to renew for (in years).
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renew_for(duration = 1)
    body = { duration: duration }.to_json
    _response, data = GandiV5.post url('renew'), body
    data['message']
  end

  # Restoration information for the domain.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore_information
    @restore_information ||= fetch_restore_information
  end

  # Requery Gandi for the domain's restore information.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_restore_information
    _response, data = GandiV5.get url('restore')
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi data
  rescue RestClient::NotFound
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi restorable: false
  end

  # Restore an expired domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-restore
  # @return [String] The confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore
    _response, data = GandiV5.post url('restore'), '{}'
    data['message']
  end

  # Requery Gandi fo this domain's information.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def refresh
    _response, data = GandiV5.get url
    from_gandi data
    auto_renew.domain = self
  end

  # Get the price for renewing this domain.
  # @param currency [String] the currency to get the price in (e.g. GBP)
  # @param period [Integer] the number of year(s) renewal to get the price for
  # @return [GandiV5::Domain::Availability::Product::Price]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def renewal_price(currency: 'GBP', period: 1)
    arguments = { processes: [:renew], currency: currency, period: period }
    GandiV5::Domain::Availability.fetch(fqdn, **arguments)
                                 .products.first
                                 .prices.first
  end

  # LiveDNS information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def livedns
    @livedns ||= fetch_livedns
  end

  # Requery Gandi for the domain's LiveDNS information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_livedns
    _response, data = GandiV5.get url('livedns')
    @livedns = GandiV5::Domain::LiveDNS.from_gandi data
    @name_server = @livedns.current
    @name_servers = @livedns.name_servers
    @livedns
  end

  # Enable LiveDNS for the domain.
  # If you want to disable LiveDNS, change the nameservers.
  # Please note that if the domain is on the classic Gandi DNS,
  # this will also perform a copy of all existing records immediately afterwards.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-livedns
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def enable_livedns
    _response, data = GandiV5.post url('livedns')
    data['message']
  end

  # Name servers for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def name_servers
    @name_servers ||= fetch_name_servers
  end

  # Requery Gandi for the domain's name servers.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_name_servers
    _response, data = GandiV5.get url('nameservers')
    @name_servers = data
  end

  # Update name servers in Gandi.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-nameservers
  # @param nameservers [Array<String>] the name servers to use.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_name_servers(nameservers)
    _response, data = GandiV5.put url('nameservers'), { 'nameservers' => nameservers }.to_json
    @name_servers = nameservers
    data['message']
  end

  # Glue records for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_records
    @glue_records ||= fetch_glue_records
  end

  # Requery Gandi for the domain's glue records.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_glue_records
    _response, data = GandiV5.get url('hosts')
    @glue_records = data.map { |record| record.values_at('name', 'ips') }.to_h
  end

  # Add a new glue record to the domain in Gandi.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def add_glue_record(name, *ips)
    _response, data = GandiV5.post url('hosts'), { 'name' => name, 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Get a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @return [nil] name was not found
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_record(name)
    records = @glue_records.key?(name) ? @glue_records : fetch_glue_records
    records.key?(name) ? records.select { |k, _v| k == name } : nil
  end

  # Update a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_glue_record(name, *ips)
    _response, data = GandiV5.put url('hosts', name), { 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Delete a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#delete-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def delete_glue_record(name)
    _response, data = GandiV5.delete url('hosts', name)
    @glue_records ||= {}
    @glue_records.delete(name)
    data['message']
  end

  # @see GandiV5::Email::Mailbox.list
  def mailboxes(**params)
    GandiV5::Email::Mailbox.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Slot.list
  def mailbox_slots(**params)
    GandiV5::Email::Slot.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Forward.list
  def email_forwards(**params)
    GandiV5::Email::Forward.list(**params, fqdn: fqdn)
  end

  # Create (register) a new domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains
  # @param fqdn [String, #to_s] the fully qualified domain name to create.
  # @param dry_run [Boolean]
  #   whether the details should be checked instead of actually creating the domain.
  # @param sharing_id [String] either:
  #   * nil (default) - nothing special happens
  #   * an organization ID - pay using another organization
  #     (you need to have billing permissions on the organization
  #     and use the same organization name for the domain name's owner).
  #     The invoice will be edited using this organization's information.
  #   * a reseller ID - buy a domain for a customer using a reseller account
  #     (you need to have billing permissions on the reseller organization
  #     and have your customer's information for the owner).
  #     The invoice will be edited using the reseller organization's information.
  # @param owner [GandiV5::Domain::Contact, #to_gandi, #to_h] (required)
  #   the owner of the new domain.
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the administrative contact for the new domain.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the billing contact for the new domain.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the technical contact for the new domain.
  # @param claims [String] (optional) unknown - not documented at Gandi.
  # @param currency ["EUR", "USD", "GBP", "TWD", "CNY"] (optional)
  #   the currency you wish to be charged in.
  # @param duration [Integer] (optional, default 1, minimum 1 maximum 10)
  #   how many years to register for.
  # @param enforce_premium [Boolean] (optional)
  #   must be set to true if the domain is a premium domain.
  # @param extra_parameters [Hash, #to_gandi, #to_json] (optional)
  #   unknown - not documented at Gandi.
  # @param lang [String] (optional)
  #   ISO-639-2 language code of the domain, required for some IDN domains.
  # @param nameserver_ips [Hash<String => Array<String>>, #to_gandi, #to_json] (optional)
  #   For glue records only - dictionnary associating a nameserver to a list of IP addresses.
  # @param nameservers [Array<String>, #to_gandi, #to_json] (optional)
  #   List of nameservers. Gandi's LiveDNS nameservers are used if omitted..
  # @param price [Numeric, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param resellee_id [String, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param smd [String, #to_gandi, #to_json] (optional)
  #   Contents of a Signed Mark Data file (used for newgtld sunrises, tld_period must be sunrise).
  # @param tld_period ["sunrise", "landrush", "eap1", "eap2", "eap3", "eap4", "eap5", "eap6",
  #   "eap7", "eap8", "eap9", "golive", #to_gandi, #to_json] (optional)
  #   @see https://docs.gandi.net/en/domain_names/register/new_gtld.html
  # @return [GandiV5::Domain] the created domain
  # @return [Hash] if doing a dry run, you get what Gandi returns
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def self.create(fqdn, dry_run: false, sharing_id: nil, **params)
    fail ArgumentError, 'missing keyword: owner' unless params.key?(:owner)

    body = params.merge(fqdn: fqdn)
                 .transform_values { |val| val.respond_to?(:to_gandi) ? val.to_gandi : val }
                 .to_json
    url_ = sharing_id ? "#{url}?sharing_id=#{sharing_id}" : url

    response, data = GandiV5.post(url_, body, 'Dry-Run': dry_run ? 1 : 0)
    dry_run ? data : fetch(response.headers[:location].split('/').last)
  end

  # Get information on a domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain
  # @param fqdn [String, #to_s] the fully qualified domain name to fetch.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.fetch(fqdn)
    _response, data = GandiV5.get url(fqdn)
    domain = from_gandi data
    domain.auto_renew.domain = fqdn
    domain
  end

  # List domains.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains
  # @param page [#each<Integer, #to_s>] the page(s) of results to retrieve.
  #   If page is not provided keep querying until an empty list is returned.
  #   If page responds to .each then iterate until an empty list is returned.
  # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page.
  # @param fqdn [String, #to_s] (optional)
  #   filters the list by domain name, with optional patterns.
  #   e.g. "example.net", "example.*", "*ample.com"
  # @param sort_by [String, #to_s] (optional default "fqdn") how to sort the list.
  # @param tld [String, #to_s] (optional) used to filter by just the top level domain.
  # @return [Array<GandiV5::Domain>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.list(page: (1..), per_page: 100, **params)
    page = [page.to_i] unless page.respond_to?(:each)

    domains = []
    page.each do |page_number|
      _resp, data = GandiV5.get url, params: params.merge(page: page_number, per_page: per_page)
      break if data.empty?

      domains += data.map { |domain| from_gandi domain }
      break if data.count < per_page
    end
    domains
  end

  private

  def url(*extra)
    "#{BASE}domain/domains/" +
      CGI.escape(fqdn) +
      (extra.empty? ? '' : "/#{extra.join('/')}")
  end

  def self.url(fqdn = nil)
    "#{BASE}domain/domains" +
      (fqdn ? "/#{CGI.escape fqdn}" : '')
  end
  private_class_method :url
end

#sharing_uuidnil, String (readonly)

Returns:

  • (nil, String)


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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/gandi_v5/domain.rb', line 52

class Domain
  include GandiV5::Data

  SERVICES = {
    gandidns: 'GandiDNS',
    gandilivedns: 'LiveDNS',
    dnssec: 'DNSSEC',
    certificate: 'SSL Certificate',
    paas: 'PAAS',
    redirection: 'Redirection',
    gandimail: 'GandiMail',
    packmail: 'PackMail',
    blog: 'Blog',
    hosting: 'Hosting',
    site: 'Site',
    mailboxv2: 'MailboxV2'
  }.freeze

  STATUSES = {
    clientHold: 'clientHold',
    clientUpdateProhibited: 'clientUpdateProhibited',
    clientTransferProhibited: 'clientTransferProhibited',
    clientDeleteProhibited: 'clientDeleteProhibited',
    clientRenewProhibited: 'clientRenewProhibited',
    serverHold: 'serverHold',
    pendingTransfer: 'pendingTransfer',
    serverTransferProhibited: 'serverTransferProhibited'
  }.freeze

  CONTACTS_CONVERTER = lambda { |hash|
    break {} if hash.nil?

    hash = hash.transform_keys(&:to_sym)
               .transform_values { |value| GandiV5::Domain::Contact.from_gandi value }

    hash.define_singleton_method(:owner) { send :'[]', :owner }
    hash.define_singleton_method(:admin) { send :'[]', :admin }
    hash.define_singleton_method(:bill) { send :'[]', :bill }
    hash.define_singleton_method(:tech) { send :'[]', :tech }

    hash
  }
  private_constant :CONTACTS_CONVERTER

  members :fqdn, :fqdn_unicode, :tld, :can_tld_lock, :tags, :owner, :domain_owner
  member :sharing_uuid, gandi_key: 'sharing_id'
  member :name_servers, gandi_key: 'nameservers'
  member :auth_info, gandi_key: 'authinfo'
  member :organisation_owner, gandi_key: 'orga_owner'
  member :uuid, gandi_key: 'id'

  member(
    :dates,
    converter: GandiV5::Domain::Dates
  )
  member(
    :sharing_space,
    gandi_key: 'sharing_space',
    converter: GandiV5::Domain::SharingSpace
  )

  member(
    :auto_renew,
    gandi_key: 'autorenew',
    converter: GandiV5::Data::Converter.new(from_gandi: lambda { |hash|
      break nil if hash.nil?
      break nil if hash.eql?(true)
      break GandiV5::Domain::AutoRenew.from_gandi('enabled' => false) if hash.eql?(false)

      GandiV5::Domain::AutoRenew.from_gandi hash
    })
  )

  member(
    :contacts,
    converter: GandiV5::Data::Converter.new(from_gandi: CONTACTS_CONVERTER)
  )

  member(
    :name_server,
    gandi_key: 'nameserver',
    converter: GandiV5::Data::Converter.new(from_gandi: ->(hash) { hash&.[]('current')&.to_sym })
  )
  member :services, converter: GandiV5::Data::Converter::Symbol, array: true
  member :status, converter: GandiV5::Data::Converter::Symbol, array: true
  member :trustee_roles, converter: GandiV5::Data::Converter::Symbol, array: true

  alias domain_uuid uuid

  # rubocop:disable Style/AsciiComments
  # Returns the string representation of the domain.
  # @return [String] e.g. "example.com", "😀.com (xn--e28h.uk.com)"
  # rubocop:enable Style/AsciiComments
  def to_s
    string = fqdn_unicode
    string += " (#{fqdn})" unless fqdn == fqdn_unicode
    string
  end

  # Contacts for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def contacts
    @contacts ||= fetch_contacts
  end

  # Requery Gandi for the domain's contacts.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_contacts
    _response, data = GandiV5.get url('contacts')
    self.contacts = data.transform_keys(&:to_sym)
    CONTACTS_CONVERTER.call contacts
  end

  # Update some of the domain's contacts.
  # @see https://api.gandi.net/docs/domains#patch-v5-domain-domains-domain-contacts
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new administrative contact.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new billing contact.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new technical contact.
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_contacts(admin: nil, bill: nil, tech: nil)
    body = {
      admin: admin.respond_to?(:to_gandi) ? admin.to_gandi : admin,
      bill: bill.respond_to?(:to_gandi) ? bill.to_gandi : bill,
      tech: tech.respond_to?(:to_gandi) ? tech.to_gandi : tech
    }.reject { |_k, v| v.nil? }.to_json

    GandiV5.patch url('contacts'), body
    fetch_contacts
  end

  # Renewal information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renewal_information
    @renewal_information ||= fetch_renewal_information
  end

  # Requery Gandi for the domain's renewal information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_renewal_information
    _response, data = GandiV5.get url('renew')
    data = data['renew'].merge('contracts' => data['contracts'])
    @renewal_information = GandiV5::Domain::RenewalInformation.from_gandi data
  end

  # Renew domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-renew
  # @param duration [Integer, #to_s] how long to renew for (in years).
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renew_for(duration = 1)
    body = { duration: duration }.to_json
    _response, data = GandiV5.post url('renew'), body
    data['message']
  end

  # Restoration information for the domain.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore_information
    @restore_information ||= fetch_restore_information
  end

  # Requery Gandi for the domain's restore information.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_restore_information
    _response, data = GandiV5.get url('restore')
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi data
  rescue RestClient::NotFound
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi restorable: false
  end

  # Restore an expired domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-restore
  # @return [String] The confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore
    _response, data = GandiV5.post url('restore'), '{}'
    data['message']
  end

  # Requery Gandi fo this domain's information.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def refresh
    _response, data = GandiV5.get url
    from_gandi data
    auto_renew.domain = self
  end

  # Get the price for renewing this domain.
  # @param currency [String] the currency to get the price in (e.g. GBP)
  # @param period [Integer] the number of year(s) renewal to get the price for
  # @return [GandiV5::Domain::Availability::Product::Price]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def renewal_price(currency: 'GBP', period: 1)
    arguments = { processes: [:renew], currency: currency, period: period }
    GandiV5::Domain::Availability.fetch(fqdn, **arguments)
                                 .products.first
                                 .prices.first
  end

  # LiveDNS information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def livedns
    @livedns ||= fetch_livedns
  end

  # Requery Gandi for the domain's LiveDNS information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_livedns
    _response, data = GandiV5.get url('livedns')
    @livedns = GandiV5::Domain::LiveDNS.from_gandi data
    @name_server = @livedns.current
    @name_servers = @livedns.name_servers
    @livedns
  end

  # Enable LiveDNS for the domain.
  # If you want to disable LiveDNS, change the nameservers.
  # Please note that if the domain is on the classic Gandi DNS,
  # this will also perform a copy of all existing records immediately afterwards.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-livedns
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def enable_livedns
    _response, data = GandiV5.post url('livedns')
    data['message']
  end

  # Name servers for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def name_servers
    @name_servers ||= fetch_name_servers
  end

  # Requery Gandi for the domain's name servers.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_name_servers
    _response, data = GandiV5.get url('nameservers')
    @name_servers = data
  end

  # Update name servers in Gandi.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-nameservers
  # @param nameservers [Array<String>] the name servers to use.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_name_servers(nameservers)
    _response, data = GandiV5.put url('nameservers'), { 'nameservers' => nameservers }.to_json
    @name_servers = nameservers
    data['message']
  end

  # Glue records for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_records
    @glue_records ||= fetch_glue_records
  end

  # Requery Gandi for the domain's glue records.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_glue_records
    _response, data = GandiV5.get url('hosts')
    @glue_records = data.map { |record| record.values_at('name', 'ips') }.to_h
  end

  # Add a new glue record to the domain in Gandi.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def add_glue_record(name, *ips)
    _response, data = GandiV5.post url('hosts'), { 'name' => name, 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Get a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @return [nil] name was not found
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_record(name)
    records = @glue_records.key?(name) ? @glue_records : fetch_glue_records
    records.key?(name) ? records.select { |k, _v| k == name } : nil
  end

  # Update a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_glue_record(name, *ips)
    _response, data = GandiV5.put url('hosts', name), { 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Delete a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#delete-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def delete_glue_record(name)
    _response, data = GandiV5.delete url('hosts', name)
    @glue_records ||= {}
    @glue_records.delete(name)
    data['message']
  end

  # @see GandiV5::Email::Mailbox.list
  def mailboxes(**params)
    GandiV5::Email::Mailbox.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Slot.list
  def mailbox_slots(**params)
    GandiV5::Email::Slot.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Forward.list
  def email_forwards(**params)
    GandiV5::Email::Forward.list(**params, fqdn: fqdn)
  end

  # Create (register) a new domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains
  # @param fqdn [String, #to_s] the fully qualified domain name to create.
  # @param dry_run [Boolean]
  #   whether the details should be checked instead of actually creating the domain.
  # @param sharing_id [String] either:
  #   * nil (default) - nothing special happens
  #   * an organization ID - pay using another organization
  #     (you need to have billing permissions on the organization
  #     and use the same organization name for the domain name's owner).
  #     The invoice will be edited using this organization's information.
  #   * a reseller ID - buy a domain for a customer using a reseller account
  #     (you need to have billing permissions on the reseller organization
  #     and have your customer's information for the owner).
  #     The invoice will be edited using the reseller organization's information.
  # @param owner [GandiV5::Domain::Contact, #to_gandi, #to_h] (required)
  #   the owner of the new domain.
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the administrative contact for the new domain.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the billing contact for the new domain.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the technical contact for the new domain.
  # @param claims [String] (optional) unknown - not documented at Gandi.
  # @param currency ["EUR", "USD", "GBP", "TWD", "CNY"] (optional)
  #   the currency you wish to be charged in.
  # @param duration [Integer] (optional, default 1, minimum 1 maximum 10)
  #   how many years to register for.
  # @param enforce_premium [Boolean] (optional)
  #   must be set to true if the domain is a premium domain.
  # @param extra_parameters [Hash, #to_gandi, #to_json] (optional)
  #   unknown - not documented at Gandi.
  # @param lang [String] (optional)
  #   ISO-639-2 language code of the domain, required for some IDN domains.
  # @param nameserver_ips [Hash<String => Array<String>>, #to_gandi, #to_json] (optional)
  #   For glue records only - dictionnary associating a nameserver to a list of IP addresses.
  # @param nameservers [Array<String>, #to_gandi, #to_json] (optional)
  #   List of nameservers. Gandi's LiveDNS nameservers are used if omitted..
  # @param price [Numeric, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param resellee_id [String, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param smd [String, #to_gandi, #to_json] (optional)
  #   Contents of a Signed Mark Data file (used for newgtld sunrises, tld_period must be sunrise).
  # @param tld_period ["sunrise", "landrush", "eap1", "eap2", "eap3", "eap4", "eap5", "eap6",
  #   "eap7", "eap8", "eap9", "golive", #to_gandi, #to_json] (optional)
  #   @see https://docs.gandi.net/en/domain_names/register/new_gtld.html
  # @return [GandiV5::Domain] the created domain
  # @return [Hash] if doing a dry run, you get what Gandi returns
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def self.create(fqdn, dry_run: false, sharing_id: nil, **params)
    fail ArgumentError, 'missing keyword: owner' unless params.key?(:owner)

    body = params.merge(fqdn: fqdn)
                 .transform_values { |val| val.respond_to?(:to_gandi) ? val.to_gandi : val }
                 .to_json
    url_ = sharing_id ? "#{url}?sharing_id=#{sharing_id}" : url

    response, data = GandiV5.post(url_, body, 'Dry-Run': dry_run ? 1 : 0)
    dry_run ? data : fetch(response.headers[:location].split('/').last)
  end

  # Get information on a domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain
  # @param fqdn [String, #to_s] the fully qualified domain name to fetch.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.fetch(fqdn)
    _response, data = GandiV5.get url(fqdn)
    domain = from_gandi data
    domain.auto_renew.domain = fqdn
    domain
  end

  # List domains.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains
  # @param page [#each<Integer, #to_s>] the page(s) of results to retrieve.
  #   If page is not provided keep querying until an empty list is returned.
  #   If page responds to .each then iterate until an empty list is returned.
  # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page.
  # @param fqdn [String, #to_s] (optional)
  #   filters the list by domain name, with optional patterns.
  #   e.g. "example.net", "example.*", "*ample.com"
  # @param sort_by [String, #to_s] (optional default "fqdn") how to sort the list.
  # @param tld [String, #to_s] (optional) used to filter by just the top level domain.
  # @return [Array<GandiV5::Domain>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.list(page: (1..), per_page: 100, **params)
    page = [page.to_i] unless page.respond_to?(:each)

    domains = []
    page.each do |page_number|
      _resp, data = GandiV5.get url, params: params.merge(page: page_number, per_page: per_page)
      break if data.empty?

      domains += data.map { |domain| from_gandi domain }
      break if data.count < per_page
    end
    domains
  end

  private

  def url(*extra)
    "#{BASE}domain/domains/" +
      CGI.escape(fqdn) +
      (extra.empty? ? '' : "/#{extra.join('/')}")
  end

  def self.url(fqdn = nil)
    "#{BASE}domain/domains" +
      (fqdn ? "/#{CGI.escape fqdn}" : '')
  end
  private_class_method :url
end

#statusString (readonly)

Returns one of: “clientHold”, “clientUpdateProhibited”, “clientTransferProhibited”, “clientDeleteProhibited”, “clientRenewProhibited”, “serverHold”, “pendingTransfer”, “serverTransferProhibited” @see docs.gandi.net/en/domain_names/faq/domain_statuses.html.

Returns:

  • (String)

    one of: “clientHold”, “clientUpdateProhibited”, “clientTransferProhibited”, “clientDeleteProhibited”, “clientRenewProhibited”, “serverHold”, “pendingTransfer”, “serverTransferProhibited” @see docs.gandi.net/en/domain_names/faq/domain_statuses.html



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/gandi_v5/domain.rb', line 52

class Domain
  include GandiV5::Data

  SERVICES = {
    gandidns: 'GandiDNS',
    gandilivedns: 'LiveDNS',
    dnssec: 'DNSSEC',
    certificate: 'SSL Certificate',
    paas: 'PAAS',
    redirection: 'Redirection',
    gandimail: 'GandiMail',
    packmail: 'PackMail',
    blog: 'Blog',
    hosting: 'Hosting',
    site: 'Site',
    mailboxv2: 'MailboxV2'
  }.freeze

  STATUSES = {
    clientHold: 'clientHold',
    clientUpdateProhibited: 'clientUpdateProhibited',
    clientTransferProhibited: 'clientTransferProhibited',
    clientDeleteProhibited: 'clientDeleteProhibited',
    clientRenewProhibited: 'clientRenewProhibited',
    serverHold: 'serverHold',
    pendingTransfer: 'pendingTransfer',
    serverTransferProhibited: 'serverTransferProhibited'
  }.freeze

  CONTACTS_CONVERTER = lambda { |hash|
    break {} if hash.nil?

    hash = hash.transform_keys(&:to_sym)
               .transform_values { |value| GandiV5::Domain::Contact.from_gandi value }

    hash.define_singleton_method(:owner) { send :'[]', :owner }
    hash.define_singleton_method(:admin) { send :'[]', :admin }
    hash.define_singleton_method(:bill) { send :'[]', :bill }
    hash.define_singleton_method(:tech) { send :'[]', :tech }

    hash
  }
  private_constant :CONTACTS_CONVERTER

  members :fqdn, :fqdn_unicode, :tld, :can_tld_lock, :tags, :owner, :domain_owner
  member :sharing_uuid, gandi_key: 'sharing_id'
  member :name_servers, gandi_key: 'nameservers'
  member :auth_info, gandi_key: 'authinfo'
  member :organisation_owner, gandi_key: 'orga_owner'
  member :uuid, gandi_key: 'id'

  member(
    :dates,
    converter: GandiV5::Domain::Dates
  )
  member(
    :sharing_space,
    gandi_key: 'sharing_space',
    converter: GandiV5::Domain::SharingSpace
  )

  member(
    :auto_renew,
    gandi_key: 'autorenew',
    converter: GandiV5::Data::Converter.new(from_gandi: lambda { |hash|
      break nil if hash.nil?
      break nil if hash.eql?(true)
      break GandiV5::Domain::AutoRenew.from_gandi('enabled' => false) if hash.eql?(false)

      GandiV5::Domain::AutoRenew.from_gandi hash
    })
  )

  member(
    :contacts,
    converter: GandiV5::Data::Converter.new(from_gandi: CONTACTS_CONVERTER)
  )

  member(
    :name_server,
    gandi_key: 'nameserver',
    converter: GandiV5::Data::Converter.new(from_gandi: ->(hash) { hash&.[]('current')&.to_sym })
  )
  member :services, converter: GandiV5::Data::Converter::Symbol, array: true
  member :status, converter: GandiV5::Data::Converter::Symbol, array: true
  member :trustee_roles, converter: GandiV5::Data::Converter::Symbol, array: true

  alias domain_uuid uuid

  # rubocop:disable Style/AsciiComments
  # Returns the string representation of the domain.
  # @return [String] e.g. "example.com", "😀.com (xn--e28h.uk.com)"
  # rubocop:enable Style/AsciiComments
  def to_s
    string = fqdn_unicode
    string += " (#{fqdn})" unless fqdn == fqdn_unicode
    string
  end

  # Contacts for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def contacts
    @contacts ||= fetch_contacts
  end

  # Requery Gandi for the domain's contacts.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_contacts
    _response, data = GandiV5.get url('contacts')
    self.contacts = data.transform_keys(&:to_sym)
    CONTACTS_CONVERTER.call contacts
  end

  # Update some of the domain's contacts.
  # @see https://api.gandi.net/docs/domains#patch-v5-domain-domains-domain-contacts
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new administrative contact.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new billing contact.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new technical contact.
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_contacts(admin: nil, bill: nil, tech: nil)
    body = {
      admin: admin.respond_to?(:to_gandi) ? admin.to_gandi : admin,
      bill: bill.respond_to?(:to_gandi) ? bill.to_gandi : bill,
      tech: tech.respond_to?(:to_gandi) ? tech.to_gandi : tech
    }.reject { |_k, v| v.nil? }.to_json

    GandiV5.patch url('contacts'), body
    fetch_contacts
  end

  # Renewal information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renewal_information
    @renewal_information ||= fetch_renewal_information
  end

  # Requery Gandi for the domain's renewal information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_renewal_information
    _response, data = GandiV5.get url('renew')
    data = data['renew'].merge('contracts' => data['contracts'])
    @renewal_information = GandiV5::Domain::RenewalInformation.from_gandi data
  end

  # Renew domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-renew
  # @param duration [Integer, #to_s] how long to renew for (in years).
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renew_for(duration = 1)
    body = { duration: duration }.to_json
    _response, data = GandiV5.post url('renew'), body
    data['message']
  end

  # Restoration information for the domain.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore_information
    @restore_information ||= fetch_restore_information
  end

  # Requery Gandi for the domain's restore information.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_restore_information
    _response, data = GandiV5.get url('restore')
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi data
  rescue RestClient::NotFound
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi restorable: false
  end

  # Restore an expired domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-restore
  # @return [String] The confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore
    _response, data = GandiV5.post url('restore'), '{}'
    data['message']
  end

  # Requery Gandi fo this domain's information.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def refresh
    _response, data = GandiV5.get url
    from_gandi data
    auto_renew.domain = self
  end

  # Get the price for renewing this domain.
  # @param currency [String] the currency to get the price in (e.g. GBP)
  # @param period [Integer] the number of year(s) renewal to get the price for
  # @return [GandiV5::Domain::Availability::Product::Price]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def renewal_price(currency: 'GBP', period: 1)
    arguments = { processes: [:renew], currency: currency, period: period }
    GandiV5::Domain::Availability.fetch(fqdn, **arguments)
                                 .products.first
                                 .prices.first
  end

  # LiveDNS information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def livedns
    @livedns ||= fetch_livedns
  end

  # Requery Gandi for the domain's LiveDNS information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_livedns
    _response, data = GandiV5.get url('livedns')
    @livedns = GandiV5::Domain::LiveDNS.from_gandi data
    @name_server = @livedns.current
    @name_servers = @livedns.name_servers
    @livedns
  end

  # Enable LiveDNS for the domain.
  # If you want to disable LiveDNS, change the nameservers.
  # Please note that if the domain is on the classic Gandi DNS,
  # this will also perform a copy of all existing records immediately afterwards.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-livedns
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def enable_livedns
    _response, data = GandiV5.post url('livedns')
    data['message']
  end

  # Name servers for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def name_servers
    @name_servers ||= fetch_name_servers
  end

  # Requery Gandi for the domain's name servers.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_name_servers
    _response, data = GandiV5.get url('nameservers')
    @name_servers = data
  end

  # Update name servers in Gandi.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-nameservers
  # @param nameservers [Array<String>] the name servers to use.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_name_servers(nameservers)
    _response, data = GandiV5.put url('nameservers'), { 'nameservers' => nameservers }.to_json
    @name_servers = nameservers
    data['message']
  end

  # Glue records for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_records
    @glue_records ||= fetch_glue_records
  end

  # Requery Gandi for the domain's glue records.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_glue_records
    _response, data = GandiV5.get url('hosts')
    @glue_records = data.map { |record| record.values_at('name', 'ips') }.to_h
  end

  # Add a new glue record to the domain in Gandi.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def add_glue_record(name, *ips)
    _response, data = GandiV5.post url('hosts'), { 'name' => name, 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Get a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @return [nil] name was not found
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_record(name)
    records = @glue_records.key?(name) ? @glue_records : fetch_glue_records
    records.key?(name) ? records.select { |k, _v| k == name } : nil
  end

  # Update a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_glue_record(name, *ips)
    _response, data = GandiV5.put url('hosts', name), { 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Delete a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#delete-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def delete_glue_record(name)
    _response, data = GandiV5.delete url('hosts', name)
    @glue_records ||= {}
    @glue_records.delete(name)
    data['message']
  end

  # @see GandiV5::Email::Mailbox.list
  def mailboxes(**params)
    GandiV5::Email::Mailbox.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Slot.list
  def mailbox_slots(**params)
    GandiV5::Email::Slot.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Forward.list
  def email_forwards(**params)
    GandiV5::Email::Forward.list(**params, fqdn: fqdn)
  end

  # Create (register) a new domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains
  # @param fqdn [String, #to_s] the fully qualified domain name to create.
  # @param dry_run [Boolean]
  #   whether the details should be checked instead of actually creating the domain.
  # @param sharing_id [String] either:
  #   * nil (default) - nothing special happens
  #   * an organization ID - pay using another organization
  #     (you need to have billing permissions on the organization
  #     and use the same organization name for the domain name's owner).
  #     The invoice will be edited using this organization's information.
  #   * a reseller ID - buy a domain for a customer using a reseller account
  #     (you need to have billing permissions on the reseller organization
  #     and have your customer's information for the owner).
  #     The invoice will be edited using the reseller organization's information.
  # @param owner [GandiV5::Domain::Contact, #to_gandi, #to_h] (required)
  #   the owner of the new domain.
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the administrative contact for the new domain.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the billing contact for the new domain.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the technical contact for the new domain.
  # @param claims [String] (optional) unknown - not documented at Gandi.
  # @param currency ["EUR", "USD", "GBP", "TWD", "CNY"] (optional)
  #   the currency you wish to be charged in.
  # @param duration [Integer] (optional, default 1, minimum 1 maximum 10)
  #   how many years to register for.
  # @param enforce_premium [Boolean] (optional)
  #   must be set to true if the domain is a premium domain.
  # @param extra_parameters [Hash, #to_gandi, #to_json] (optional)
  #   unknown - not documented at Gandi.
  # @param lang [String] (optional)
  #   ISO-639-2 language code of the domain, required for some IDN domains.
  # @param nameserver_ips [Hash<String => Array<String>>, #to_gandi, #to_json] (optional)
  #   For glue records only - dictionnary associating a nameserver to a list of IP addresses.
  # @param nameservers [Array<String>, #to_gandi, #to_json] (optional)
  #   List of nameservers. Gandi's LiveDNS nameservers are used if omitted..
  # @param price [Numeric, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param resellee_id [String, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param smd [String, #to_gandi, #to_json] (optional)
  #   Contents of a Signed Mark Data file (used for newgtld sunrises, tld_period must be sunrise).
  # @param tld_period ["sunrise", "landrush", "eap1", "eap2", "eap3", "eap4", "eap5", "eap6",
  #   "eap7", "eap8", "eap9", "golive", #to_gandi, #to_json] (optional)
  #   @see https://docs.gandi.net/en/domain_names/register/new_gtld.html
  # @return [GandiV5::Domain] the created domain
  # @return [Hash] if doing a dry run, you get what Gandi returns
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def self.create(fqdn, dry_run: false, sharing_id: nil, **params)
    fail ArgumentError, 'missing keyword: owner' unless params.key?(:owner)

    body = params.merge(fqdn: fqdn)
                 .transform_values { |val| val.respond_to?(:to_gandi) ? val.to_gandi : val }
                 .to_json
    url_ = sharing_id ? "#{url}?sharing_id=#{sharing_id}" : url

    response, data = GandiV5.post(url_, body, 'Dry-Run': dry_run ? 1 : 0)
    dry_run ? data : fetch(response.headers[:location].split('/').last)
  end

  # Get information on a domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain
  # @param fqdn [String, #to_s] the fully qualified domain name to fetch.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.fetch(fqdn)
    _response, data = GandiV5.get url(fqdn)
    domain = from_gandi data
    domain.auto_renew.domain = fqdn
    domain
  end

  # List domains.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains
  # @param page [#each<Integer, #to_s>] the page(s) of results to retrieve.
  #   If page is not provided keep querying until an empty list is returned.
  #   If page responds to .each then iterate until an empty list is returned.
  # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page.
  # @param fqdn [String, #to_s] (optional)
  #   filters the list by domain name, with optional patterns.
  #   e.g. "example.net", "example.*", "*ample.com"
  # @param sort_by [String, #to_s] (optional default "fqdn") how to sort the list.
  # @param tld [String, #to_s] (optional) used to filter by just the top level domain.
  # @return [Array<GandiV5::Domain>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.list(page: (1..), per_page: 100, **params)
    page = [page.to_i] unless page.respond_to?(:each)

    domains = []
    page.each do |page_number|
      _resp, data = GandiV5.get url, params: params.merge(page: page_number, per_page: per_page)
      break if data.empty?

      domains += data.map { |domain| from_gandi domain }
      break if data.count < per_page
    end
    domains
  end

  private

  def url(*extra)
    "#{BASE}domain/domains/" +
      CGI.escape(fqdn) +
      (extra.empty? ? '' : "/#{extra.join('/')}")
  end

  def self.url(fqdn = nil)
    "#{BASE}domain/domains" +
      (fqdn ? "/#{CGI.escape fqdn}" : '')
  end
  private_class_method :url
end

#tagsnil, Array<String> (readonly)

Returns list of tags that have been assigned to the domain.

Returns:

  • (nil, Array<String>)

    list of tags that have been assigned to the domain.



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/gandi_v5/domain.rb', line 52

class Domain
  include GandiV5::Data

  SERVICES = {
    gandidns: 'GandiDNS',
    gandilivedns: 'LiveDNS',
    dnssec: 'DNSSEC',
    certificate: 'SSL Certificate',
    paas: 'PAAS',
    redirection: 'Redirection',
    gandimail: 'GandiMail',
    packmail: 'PackMail',
    blog: 'Blog',
    hosting: 'Hosting',
    site: 'Site',
    mailboxv2: 'MailboxV2'
  }.freeze

  STATUSES = {
    clientHold: 'clientHold',
    clientUpdateProhibited: 'clientUpdateProhibited',
    clientTransferProhibited: 'clientTransferProhibited',
    clientDeleteProhibited: 'clientDeleteProhibited',
    clientRenewProhibited: 'clientRenewProhibited',
    serverHold: 'serverHold',
    pendingTransfer: 'pendingTransfer',
    serverTransferProhibited: 'serverTransferProhibited'
  }.freeze

  CONTACTS_CONVERTER = lambda { |hash|
    break {} if hash.nil?

    hash = hash.transform_keys(&:to_sym)
               .transform_values { |value| GandiV5::Domain::Contact.from_gandi value }

    hash.define_singleton_method(:owner) { send :'[]', :owner }
    hash.define_singleton_method(:admin) { send :'[]', :admin }
    hash.define_singleton_method(:bill) { send :'[]', :bill }
    hash.define_singleton_method(:tech) { send :'[]', :tech }

    hash
  }
  private_constant :CONTACTS_CONVERTER

  members :fqdn, :fqdn_unicode, :tld, :can_tld_lock, :tags, :owner, :domain_owner
  member :sharing_uuid, gandi_key: 'sharing_id'
  member :name_servers, gandi_key: 'nameservers'
  member :auth_info, gandi_key: 'authinfo'
  member :organisation_owner, gandi_key: 'orga_owner'
  member :uuid, gandi_key: 'id'

  member(
    :dates,
    converter: GandiV5::Domain::Dates
  )
  member(
    :sharing_space,
    gandi_key: 'sharing_space',
    converter: GandiV5::Domain::SharingSpace
  )

  member(
    :auto_renew,
    gandi_key: 'autorenew',
    converter: GandiV5::Data::Converter.new(from_gandi: lambda { |hash|
      break nil if hash.nil?
      break nil if hash.eql?(true)
      break GandiV5::Domain::AutoRenew.from_gandi('enabled' => false) if hash.eql?(false)

      GandiV5::Domain::AutoRenew.from_gandi hash
    })
  )

  member(
    :contacts,
    converter: GandiV5::Data::Converter.new(from_gandi: CONTACTS_CONVERTER)
  )

  member(
    :name_server,
    gandi_key: 'nameserver',
    converter: GandiV5::Data::Converter.new(from_gandi: ->(hash) { hash&.[]('current')&.to_sym })
  )
  member :services, converter: GandiV5::Data::Converter::Symbol, array: true
  member :status, converter: GandiV5::Data::Converter::Symbol, array: true
  member :trustee_roles, converter: GandiV5::Data::Converter::Symbol, array: true

  alias domain_uuid uuid

  # rubocop:disable Style/AsciiComments
  # Returns the string representation of the domain.
  # @return [String] e.g. "example.com", "😀.com (xn--e28h.uk.com)"
  # rubocop:enable Style/AsciiComments
  def to_s
    string = fqdn_unicode
    string += " (#{fqdn})" unless fqdn == fqdn_unicode
    string
  end

  # Contacts for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def contacts
    @contacts ||= fetch_contacts
  end

  # Requery Gandi for the domain's contacts.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_contacts
    _response, data = GandiV5.get url('contacts')
    self.contacts = data.transform_keys(&:to_sym)
    CONTACTS_CONVERTER.call contacts
  end

  # Update some of the domain's contacts.
  # @see https://api.gandi.net/docs/domains#patch-v5-domain-domains-domain-contacts
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new administrative contact.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new billing contact.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new technical contact.
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_contacts(admin: nil, bill: nil, tech: nil)
    body = {
      admin: admin.respond_to?(:to_gandi) ? admin.to_gandi : admin,
      bill: bill.respond_to?(:to_gandi) ? bill.to_gandi : bill,
      tech: tech.respond_to?(:to_gandi) ? tech.to_gandi : tech
    }.reject { |_k, v| v.nil? }.to_json

    GandiV5.patch url('contacts'), body
    fetch_contacts
  end

  # Renewal information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renewal_information
    @renewal_information ||= fetch_renewal_information
  end

  # Requery Gandi for the domain's renewal information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_renewal_information
    _response, data = GandiV5.get url('renew')
    data = data['renew'].merge('contracts' => data['contracts'])
    @renewal_information = GandiV5::Domain::RenewalInformation.from_gandi data
  end

  # Renew domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-renew
  # @param duration [Integer, #to_s] how long to renew for (in years).
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renew_for(duration = 1)
    body = { duration: duration }.to_json
    _response, data = GandiV5.post url('renew'), body
    data['message']
  end

  # Restoration information for the domain.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore_information
    @restore_information ||= fetch_restore_information
  end

  # Requery Gandi for the domain's restore information.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_restore_information
    _response, data = GandiV5.get url('restore')
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi data
  rescue RestClient::NotFound
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi restorable: false
  end

  # Restore an expired domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-restore
  # @return [String] The confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore
    _response, data = GandiV5.post url('restore'), '{}'
    data['message']
  end

  # Requery Gandi fo this domain's information.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def refresh
    _response, data = GandiV5.get url
    from_gandi data
    auto_renew.domain = self
  end

  # Get the price for renewing this domain.
  # @param currency [String] the currency to get the price in (e.g. GBP)
  # @param period [Integer] the number of year(s) renewal to get the price for
  # @return [GandiV5::Domain::Availability::Product::Price]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def renewal_price(currency: 'GBP', period: 1)
    arguments = { processes: [:renew], currency: currency, period: period }
    GandiV5::Domain::Availability.fetch(fqdn, **arguments)
                                 .products.first
                                 .prices.first
  end

  # LiveDNS information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def livedns
    @livedns ||= fetch_livedns
  end

  # Requery Gandi for the domain's LiveDNS information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_livedns
    _response, data = GandiV5.get url('livedns')
    @livedns = GandiV5::Domain::LiveDNS.from_gandi data
    @name_server = @livedns.current
    @name_servers = @livedns.name_servers
    @livedns
  end

  # Enable LiveDNS for the domain.
  # If you want to disable LiveDNS, change the nameservers.
  # Please note that if the domain is on the classic Gandi DNS,
  # this will also perform a copy of all existing records immediately afterwards.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-livedns
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def enable_livedns
    _response, data = GandiV5.post url('livedns')
    data['message']
  end

  # Name servers for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def name_servers
    @name_servers ||= fetch_name_servers
  end

  # Requery Gandi for the domain's name servers.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_name_servers
    _response, data = GandiV5.get url('nameservers')
    @name_servers = data
  end

  # Update name servers in Gandi.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-nameservers
  # @param nameservers [Array<String>] the name servers to use.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_name_servers(nameservers)
    _response, data = GandiV5.put url('nameservers'), { 'nameservers' => nameservers }.to_json
    @name_servers = nameservers
    data['message']
  end

  # Glue records for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_records
    @glue_records ||= fetch_glue_records
  end

  # Requery Gandi for the domain's glue records.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_glue_records
    _response, data = GandiV5.get url('hosts')
    @glue_records = data.map { |record| record.values_at('name', 'ips') }.to_h
  end

  # Add a new glue record to the domain in Gandi.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def add_glue_record(name, *ips)
    _response, data = GandiV5.post url('hosts'), { 'name' => name, 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Get a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @return [nil] name was not found
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_record(name)
    records = @glue_records.key?(name) ? @glue_records : fetch_glue_records
    records.key?(name) ? records.select { |k, _v| k == name } : nil
  end

  # Update a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_glue_record(name, *ips)
    _response, data = GandiV5.put url('hosts', name), { 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Delete a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#delete-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def delete_glue_record(name)
    _response, data = GandiV5.delete url('hosts', name)
    @glue_records ||= {}
    @glue_records.delete(name)
    data['message']
  end

  # @see GandiV5::Email::Mailbox.list
  def mailboxes(**params)
    GandiV5::Email::Mailbox.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Slot.list
  def mailbox_slots(**params)
    GandiV5::Email::Slot.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Forward.list
  def email_forwards(**params)
    GandiV5::Email::Forward.list(**params, fqdn: fqdn)
  end

  # Create (register) a new domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains
  # @param fqdn [String, #to_s] the fully qualified domain name to create.
  # @param dry_run [Boolean]
  #   whether the details should be checked instead of actually creating the domain.
  # @param sharing_id [String] either:
  #   * nil (default) - nothing special happens
  #   * an organization ID - pay using another organization
  #     (you need to have billing permissions on the organization
  #     and use the same organization name for the domain name's owner).
  #     The invoice will be edited using this organization's information.
  #   * a reseller ID - buy a domain for a customer using a reseller account
  #     (you need to have billing permissions on the reseller organization
  #     and have your customer's information for the owner).
  #     The invoice will be edited using the reseller organization's information.
  # @param owner [GandiV5::Domain::Contact, #to_gandi, #to_h] (required)
  #   the owner of the new domain.
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the administrative contact for the new domain.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the billing contact for the new domain.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the technical contact for the new domain.
  # @param claims [String] (optional) unknown - not documented at Gandi.
  # @param currency ["EUR", "USD", "GBP", "TWD", "CNY"] (optional)
  #   the currency you wish to be charged in.
  # @param duration [Integer] (optional, default 1, minimum 1 maximum 10)
  #   how many years to register for.
  # @param enforce_premium [Boolean] (optional)
  #   must be set to true if the domain is a premium domain.
  # @param extra_parameters [Hash, #to_gandi, #to_json] (optional)
  #   unknown - not documented at Gandi.
  # @param lang [String] (optional)
  #   ISO-639-2 language code of the domain, required for some IDN domains.
  # @param nameserver_ips [Hash<String => Array<String>>, #to_gandi, #to_json] (optional)
  #   For glue records only - dictionnary associating a nameserver to a list of IP addresses.
  # @param nameservers [Array<String>, #to_gandi, #to_json] (optional)
  #   List of nameservers. Gandi's LiveDNS nameservers are used if omitted..
  # @param price [Numeric, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param resellee_id [String, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param smd [String, #to_gandi, #to_json] (optional)
  #   Contents of a Signed Mark Data file (used for newgtld sunrises, tld_period must be sunrise).
  # @param tld_period ["sunrise", "landrush", "eap1", "eap2", "eap3", "eap4", "eap5", "eap6",
  #   "eap7", "eap8", "eap9", "golive", #to_gandi, #to_json] (optional)
  #   @see https://docs.gandi.net/en/domain_names/register/new_gtld.html
  # @return [GandiV5::Domain] the created domain
  # @return [Hash] if doing a dry run, you get what Gandi returns
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def self.create(fqdn, dry_run: false, sharing_id: nil, **params)
    fail ArgumentError, 'missing keyword: owner' unless params.key?(:owner)

    body = params.merge(fqdn: fqdn)
                 .transform_values { |val| val.respond_to?(:to_gandi) ? val.to_gandi : val }
                 .to_json
    url_ = sharing_id ? "#{url}?sharing_id=#{sharing_id}" : url

    response, data = GandiV5.post(url_, body, 'Dry-Run': dry_run ? 1 : 0)
    dry_run ? data : fetch(response.headers[:location].split('/').last)
  end

  # Get information on a domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain
  # @param fqdn [String, #to_s] the fully qualified domain name to fetch.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.fetch(fqdn)
    _response, data = GandiV5.get url(fqdn)
    domain = from_gandi data
    domain.auto_renew.domain = fqdn
    domain
  end

  # List domains.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains
  # @param page [#each<Integer, #to_s>] the page(s) of results to retrieve.
  #   If page is not provided keep querying until an empty list is returned.
  #   If page responds to .each then iterate until an empty list is returned.
  # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page.
  # @param fqdn [String, #to_s] (optional)
  #   filters the list by domain name, with optional patterns.
  #   e.g. "example.net", "example.*", "*ample.com"
  # @param sort_by [String, #to_s] (optional default "fqdn") how to sort the list.
  # @param tld [String, #to_s] (optional) used to filter by just the top level domain.
  # @return [Array<GandiV5::Domain>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.list(page: (1..), per_page: 100, **params)
    page = [page.to_i] unless page.respond_to?(:each)

    domains = []
    page.each do |page_number|
      _resp, data = GandiV5.get url, params: params.merge(page: page_number, per_page: per_page)
      break if data.empty?

      domains += data.map { |domain| from_gandi domain }
      break if data.count < per_page
    end
    domains
  end

  private

  def url(*extra)
    "#{BASE}domain/domains/" +
      CGI.escape(fqdn) +
      (extra.empty? ? '' : "/#{extra.join('/')}")
  end

  def self.url(fqdn = nil)
    "#{BASE}domain/domains" +
      (fqdn ? "/#{CGI.escape fqdn}" : '')
  end
  private_class_method :url
end

#tldString (readonly)

Returns:

  • (String)


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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/gandi_v5/domain.rb', line 52

class Domain
  include GandiV5::Data

  SERVICES = {
    gandidns: 'GandiDNS',
    gandilivedns: 'LiveDNS',
    dnssec: 'DNSSEC',
    certificate: 'SSL Certificate',
    paas: 'PAAS',
    redirection: 'Redirection',
    gandimail: 'GandiMail',
    packmail: 'PackMail',
    blog: 'Blog',
    hosting: 'Hosting',
    site: 'Site',
    mailboxv2: 'MailboxV2'
  }.freeze

  STATUSES = {
    clientHold: 'clientHold',
    clientUpdateProhibited: 'clientUpdateProhibited',
    clientTransferProhibited: 'clientTransferProhibited',
    clientDeleteProhibited: 'clientDeleteProhibited',
    clientRenewProhibited: 'clientRenewProhibited',
    serverHold: 'serverHold',
    pendingTransfer: 'pendingTransfer',
    serverTransferProhibited: 'serverTransferProhibited'
  }.freeze

  CONTACTS_CONVERTER = lambda { |hash|
    break {} if hash.nil?

    hash = hash.transform_keys(&:to_sym)
               .transform_values { |value| GandiV5::Domain::Contact.from_gandi value }

    hash.define_singleton_method(:owner) { send :'[]', :owner }
    hash.define_singleton_method(:admin) { send :'[]', :admin }
    hash.define_singleton_method(:bill) { send :'[]', :bill }
    hash.define_singleton_method(:tech) { send :'[]', :tech }

    hash
  }
  private_constant :CONTACTS_CONVERTER

  members :fqdn, :fqdn_unicode, :tld, :can_tld_lock, :tags, :owner, :domain_owner
  member :sharing_uuid, gandi_key: 'sharing_id'
  member :name_servers, gandi_key: 'nameservers'
  member :auth_info, gandi_key: 'authinfo'
  member :organisation_owner, gandi_key: 'orga_owner'
  member :uuid, gandi_key: 'id'

  member(
    :dates,
    converter: GandiV5::Domain::Dates
  )
  member(
    :sharing_space,
    gandi_key: 'sharing_space',
    converter: GandiV5::Domain::SharingSpace
  )

  member(
    :auto_renew,
    gandi_key: 'autorenew',
    converter: GandiV5::Data::Converter.new(from_gandi: lambda { |hash|
      break nil if hash.nil?
      break nil if hash.eql?(true)
      break GandiV5::Domain::AutoRenew.from_gandi('enabled' => false) if hash.eql?(false)

      GandiV5::Domain::AutoRenew.from_gandi hash
    })
  )

  member(
    :contacts,
    converter: GandiV5::Data::Converter.new(from_gandi: CONTACTS_CONVERTER)
  )

  member(
    :name_server,
    gandi_key: 'nameserver',
    converter: GandiV5::Data::Converter.new(from_gandi: ->(hash) { hash&.[]('current')&.to_sym })
  )
  member :services, converter: GandiV5::Data::Converter::Symbol, array: true
  member :status, converter: GandiV5::Data::Converter::Symbol, array: true
  member :trustee_roles, converter: GandiV5::Data::Converter::Symbol, array: true

  alias domain_uuid uuid

  # rubocop:disable Style/AsciiComments
  # Returns the string representation of the domain.
  # @return [String] e.g. "example.com", "😀.com (xn--e28h.uk.com)"
  # rubocop:enable Style/AsciiComments
  def to_s
    string = fqdn_unicode
    string += " (#{fqdn})" unless fqdn == fqdn_unicode
    string
  end

  # Contacts for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def contacts
    @contacts ||= fetch_contacts
  end

  # Requery Gandi for the domain's contacts.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_contacts
    _response, data = GandiV5.get url('contacts')
    self.contacts = data.transform_keys(&:to_sym)
    CONTACTS_CONVERTER.call contacts
  end

  # Update some of the domain's contacts.
  # @see https://api.gandi.net/docs/domains#patch-v5-domain-domains-domain-contacts
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new administrative contact.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new billing contact.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new technical contact.
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_contacts(admin: nil, bill: nil, tech: nil)
    body = {
      admin: admin.respond_to?(:to_gandi) ? admin.to_gandi : admin,
      bill: bill.respond_to?(:to_gandi) ? bill.to_gandi : bill,
      tech: tech.respond_to?(:to_gandi) ? tech.to_gandi : tech
    }.reject { |_k, v| v.nil? }.to_json

    GandiV5.patch url('contacts'), body
    fetch_contacts
  end

  # Renewal information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renewal_information
    @renewal_information ||= fetch_renewal_information
  end

  # Requery Gandi for the domain's renewal information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_renewal_information
    _response, data = GandiV5.get url('renew')
    data = data['renew'].merge('contracts' => data['contracts'])
    @renewal_information = GandiV5::Domain::RenewalInformation.from_gandi data
  end

  # Renew domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-renew
  # @param duration [Integer, #to_s] how long to renew for (in years).
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renew_for(duration = 1)
    body = { duration: duration }.to_json
    _response, data = GandiV5.post url('renew'), body
    data['message']
  end

  # Restoration information for the domain.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore_information
    @restore_information ||= fetch_restore_information
  end

  # Requery Gandi for the domain's restore information.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_restore_information
    _response, data = GandiV5.get url('restore')
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi data
  rescue RestClient::NotFound
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi restorable: false
  end

  # Restore an expired domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-restore
  # @return [String] The confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore
    _response, data = GandiV5.post url('restore'), '{}'
    data['message']
  end

  # Requery Gandi fo this domain's information.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def refresh
    _response, data = GandiV5.get url
    from_gandi data
    auto_renew.domain = self
  end

  # Get the price for renewing this domain.
  # @param currency [String] the currency to get the price in (e.g. GBP)
  # @param period [Integer] the number of year(s) renewal to get the price for
  # @return [GandiV5::Domain::Availability::Product::Price]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def renewal_price(currency: 'GBP', period: 1)
    arguments = { processes: [:renew], currency: currency, period: period }
    GandiV5::Domain::Availability.fetch(fqdn, **arguments)
                                 .products.first
                                 .prices.first
  end

  # LiveDNS information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def livedns
    @livedns ||= fetch_livedns
  end

  # Requery Gandi for the domain's LiveDNS information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_livedns
    _response, data = GandiV5.get url('livedns')
    @livedns = GandiV5::Domain::LiveDNS.from_gandi data
    @name_server = @livedns.current
    @name_servers = @livedns.name_servers
    @livedns
  end

  # Enable LiveDNS for the domain.
  # If you want to disable LiveDNS, change the nameservers.
  # Please note that if the domain is on the classic Gandi DNS,
  # this will also perform a copy of all existing records immediately afterwards.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-livedns
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def enable_livedns
    _response, data = GandiV5.post url('livedns')
    data['message']
  end

  # Name servers for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def name_servers
    @name_servers ||= fetch_name_servers
  end

  # Requery Gandi for the domain's name servers.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_name_servers
    _response, data = GandiV5.get url('nameservers')
    @name_servers = data
  end

  # Update name servers in Gandi.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-nameservers
  # @param nameservers [Array<String>] the name servers to use.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_name_servers(nameservers)
    _response, data = GandiV5.put url('nameservers'), { 'nameservers' => nameservers }.to_json
    @name_servers = nameservers
    data['message']
  end

  # Glue records for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_records
    @glue_records ||= fetch_glue_records
  end

  # Requery Gandi for the domain's glue records.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_glue_records
    _response, data = GandiV5.get url('hosts')
    @glue_records = data.map { |record| record.values_at('name', 'ips') }.to_h
  end

  # Add a new glue record to the domain in Gandi.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def add_glue_record(name, *ips)
    _response, data = GandiV5.post url('hosts'), { 'name' => name, 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Get a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @return [nil] name was not found
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_record(name)
    records = @glue_records.key?(name) ? @glue_records : fetch_glue_records
    records.key?(name) ? records.select { |k, _v| k == name } : nil
  end

  # Update a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_glue_record(name, *ips)
    _response, data = GandiV5.put url('hosts', name), { 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Delete a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#delete-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def delete_glue_record(name)
    _response, data = GandiV5.delete url('hosts', name)
    @glue_records ||= {}
    @glue_records.delete(name)
    data['message']
  end

  # @see GandiV5::Email::Mailbox.list
  def mailboxes(**params)
    GandiV5::Email::Mailbox.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Slot.list
  def mailbox_slots(**params)
    GandiV5::Email::Slot.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Forward.list
  def email_forwards(**params)
    GandiV5::Email::Forward.list(**params, fqdn: fqdn)
  end

  # Create (register) a new domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains
  # @param fqdn [String, #to_s] the fully qualified domain name to create.
  # @param dry_run [Boolean]
  #   whether the details should be checked instead of actually creating the domain.
  # @param sharing_id [String] either:
  #   * nil (default) - nothing special happens
  #   * an organization ID - pay using another organization
  #     (you need to have billing permissions on the organization
  #     and use the same organization name for the domain name's owner).
  #     The invoice will be edited using this organization's information.
  #   * a reseller ID - buy a domain for a customer using a reseller account
  #     (you need to have billing permissions on the reseller organization
  #     and have your customer's information for the owner).
  #     The invoice will be edited using the reseller organization's information.
  # @param owner [GandiV5::Domain::Contact, #to_gandi, #to_h] (required)
  #   the owner of the new domain.
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the administrative contact for the new domain.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the billing contact for the new domain.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the technical contact for the new domain.
  # @param claims [String] (optional) unknown - not documented at Gandi.
  # @param currency ["EUR", "USD", "GBP", "TWD", "CNY"] (optional)
  #   the currency you wish to be charged in.
  # @param duration [Integer] (optional, default 1, minimum 1 maximum 10)
  #   how many years to register for.
  # @param enforce_premium [Boolean] (optional)
  #   must be set to true if the domain is a premium domain.
  # @param extra_parameters [Hash, #to_gandi, #to_json] (optional)
  #   unknown - not documented at Gandi.
  # @param lang [String] (optional)
  #   ISO-639-2 language code of the domain, required for some IDN domains.
  # @param nameserver_ips [Hash<String => Array<String>>, #to_gandi, #to_json] (optional)
  #   For glue records only - dictionnary associating a nameserver to a list of IP addresses.
  # @param nameservers [Array<String>, #to_gandi, #to_json] (optional)
  #   List of nameservers. Gandi's LiveDNS nameservers are used if omitted..
  # @param price [Numeric, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param resellee_id [String, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param smd [String, #to_gandi, #to_json] (optional)
  #   Contents of a Signed Mark Data file (used for newgtld sunrises, tld_period must be sunrise).
  # @param tld_period ["sunrise", "landrush", "eap1", "eap2", "eap3", "eap4", "eap5", "eap6",
  #   "eap7", "eap8", "eap9", "golive", #to_gandi, #to_json] (optional)
  #   @see https://docs.gandi.net/en/domain_names/register/new_gtld.html
  # @return [GandiV5::Domain] the created domain
  # @return [Hash] if doing a dry run, you get what Gandi returns
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def self.create(fqdn, dry_run: false, sharing_id: nil, **params)
    fail ArgumentError, 'missing keyword: owner' unless params.key?(:owner)

    body = params.merge(fqdn: fqdn)
                 .transform_values { |val| val.respond_to?(:to_gandi) ? val.to_gandi : val }
                 .to_json
    url_ = sharing_id ? "#{url}?sharing_id=#{sharing_id}" : url

    response, data = GandiV5.post(url_, body, 'Dry-Run': dry_run ? 1 : 0)
    dry_run ? data : fetch(response.headers[:location].split('/').last)
  end

  # Get information on a domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain
  # @param fqdn [String, #to_s] the fully qualified domain name to fetch.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.fetch(fqdn)
    _response, data = GandiV5.get url(fqdn)
    domain = from_gandi data
    domain.auto_renew.domain = fqdn
    domain
  end

  # List domains.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains
  # @param page [#each<Integer, #to_s>] the page(s) of results to retrieve.
  #   If page is not provided keep querying until an empty list is returned.
  #   If page responds to .each then iterate until an empty list is returned.
  # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page.
  # @param fqdn [String, #to_s] (optional)
  #   filters the list by domain name, with optional patterns.
  #   e.g. "example.net", "example.*", "*ample.com"
  # @param sort_by [String, #to_s] (optional default "fqdn") how to sort the list.
  # @param tld [String, #to_s] (optional) used to filter by just the top level domain.
  # @return [Array<GandiV5::Domain>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.list(page: (1..), per_page: 100, **params)
    page = [page.to_i] unless page.respond_to?(:each)

    domains = []
    page.each do |page_number|
      _resp, data = GandiV5.get url, params: params.merge(page: page_number, per_page: per_page)
      break if data.empty?

      domains += data.map { |domain| from_gandi domain }
      break if data.count < per_page
    end
    domains
  end

  private

  def url(*extra)
    "#{BASE}domain/domains/" +
      CGI.escape(fqdn) +
      (extra.empty? ? '' : "/#{extra.join('/')}")
  end

  def self.url(fqdn = nil)
    "#{BASE}domain/domains" +
      (fqdn ? "/#{CGI.escape fqdn}" : '')
  end
  private_class_method :url
end

#trustee_rolesnil, Array<Symbol> (readonly)

Returns one of: admin, tech.

Returns:

  • (nil, Array<Symbol>)

    one of: admin, tech.



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/gandi_v5/domain.rb', line 52

class Domain
  include GandiV5::Data

  SERVICES = {
    gandidns: 'GandiDNS',
    gandilivedns: 'LiveDNS',
    dnssec: 'DNSSEC',
    certificate: 'SSL Certificate',
    paas: 'PAAS',
    redirection: 'Redirection',
    gandimail: 'GandiMail',
    packmail: 'PackMail',
    blog: 'Blog',
    hosting: 'Hosting',
    site: 'Site',
    mailboxv2: 'MailboxV2'
  }.freeze

  STATUSES = {
    clientHold: 'clientHold',
    clientUpdateProhibited: 'clientUpdateProhibited',
    clientTransferProhibited: 'clientTransferProhibited',
    clientDeleteProhibited: 'clientDeleteProhibited',
    clientRenewProhibited: 'clientRenewProhibited',
    serverHold: 'serverHold',
    pendingTransfer: 'pendingTransfer',
    serverTransferProhibited: 'serverTransferProhibited'
  }.freeze

  CONTACTS_CONVERTER = lambda { |hash|
    break {} if hash.nil?

    hash = hash.transform_keys(&:to_sym)
               .transform_values { |value| GandiV5::Domain::Contact.from_gandi value }

    hash.define_singleton_method(:owner) { send :'[]', :owner }
    hash.define_singleton_method(:admin) { send :'[]', :admin }
    hash.define_singleton_method(:bill) { send :'[]', :bill }
    hash.define_singleton_method(:tech) { send :'[]', :tech }

    hash
  }
  private_constant :CONTACTS_CONVERTER

  members :fqdn, :fqdn_unicode, :tld, :can_tld_lock, :tags, :owner, :domain_owner
  member :sharing_uuid, gandi_key: 'sharing_id'
  member :name_servers, gandi_key: 'nameservers'
  member :auth_info, gandi_key: 'authinfo'
  member :organisation_owner, gandi_key: 'orga_owner'
  member :uuid, gandi_key: 'id'

  member(
    :dates,
    converter: GandiV5::Domain::Dates
  )
  member(
    :sharing_space,
    gandi_key: 'sharing_space',
    converter: GandiV5::Domain::SharingSpace
  )

  member(
    :auto_renew,
    gandi_key: 'autorenew',
    converter: GandiV5::Data::Converter.new(from_gandi: lambda { |hash|
      break nil if hash.nil?
      break nil if hash.eql?(true)
      break GandiV5::Domain::AutoRenew.from_gandi('enabled' => false) if hash.eql?(false)

      GandiV5::Domain::AutoRenew.from_gandi hash
    })
  )

  member(
    :contacts,
    converter: GandiV5::Data::Converter.new(from_gandi: CONTACTS_CONVERTER)
  )

  member(
    :name_server,
    gandi_key: 'nameserver',
    converter: GandiV5::Data::Converter.new(from_gandi: ->(hash) { hash&.[]('current')&.to_sym })
  )
  member :services, converter: GandiV5::Data::Converter::Symbol, array: true
  member :status, converter: GandiV5::Data::Converter::Symbol, array: true
  member :trustee_roles, converter: GandiV5::Data::Converter::Symbol, array: true

  alias domain_uuid uuid

  # rubocop:disable Style/AsciiComments
  # Returns the string representation of the domain.
  # @return [String] e.g. "example.com", "😀.com (xn--e28h.uk.com)"
  # rubocop:enable Style/AsciiComments
  def to_s
    string = fqdn_unicode
    string += " (#{fqdn})" unless fqdn == fqdn_unicode
    string
  end

  # Contacts for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def contacts
    @contacts ||= fetch_contacts
  end

  # Requery Gandi for the domain's contacts.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_contacts
    _response, data = GandiV5.get url('contacts')
    self.contacts = data.transform_keys(&:to_sym)
    CONTACTS_CONVERTER.call contacts
  end

  # Update some of the domain's contacts.
  # @see https://api.gandi.net/docs/domains#patch-v5-domain-domains-domain-contacts
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new administrative contact.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new billing contact.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new technical contact.
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_contacts(admin: nil, bill: nil, tech: nil)
    body = {
      admin: admin.respond_to?(:to_gandi) ? admin.to_gandi : admin,
      bill: bill.respond_to?(:to_gandi) ? bill.to_gandi : bill,
      tech: tech.respond_to?(:to_gandi) ? tech.to_gandi : tech
    }.reject { |_k, v| v.nil? }.to_json

    GandiV5.patch url('contacts'), body
    fetch_contacts
  end

  # Renewal information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renewal_information
    @renewal_information ||= fetch_renewal_information
  end

  # Requery Gandi for the domain's renewal information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_renewal_information
    _response, data = GandiV5.get url('renew')
    data = data['renew'].merge('contracts' => data['contracts'])
    @renewal_information = GandiV5::Domain::RenewalInformation.from_gandi data
  end

  # Renew domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-renew
  # @param duration [Integer, #to_s] how long to renew for (in years).
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renew_for(duration = 1)
    body = { duration: duration }.to_json
    _response, data = GandiV5.post url('renew'), body
    data['message']
  end

  # Restoration information for the domain.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore_information
    @restore_information ||= fetch_restore_information
  end

  # Requery Gandi for the domain's restore information.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_restore_information
    _response, data = GandiV5.get url('restore')
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi data
  rescue RestClient::NotFound
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi restorable: false
  end

  # Restore an expired domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-restore
  # @return [String] The confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore
    _response, data = GandiV5.post url('restore'), '{}'
    data['message']
  end

  # Requery Gandi fo this domain's information.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def refresh
    _response, data = GandiV5.get url
    from_gandi data
    auto_renew.domain = self
  end

  # Get the price for renewing this domain.
  # @param currency [String] the currency to get the price in (e.g. GBP)
  # @param period [Integer] the number of year(s) renewal to get the price for
  # @return [GandiV5::Domain::Availability::Product::Price]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def renewal_price(currency: 'GBP', period: 1)
    arguments = { processes: [:renew], currency: currency, period: period }
    GandiV5::Domain::Availability.fetch(fqdn, **arguments)
                                 .products.first
                                 .prices.first
  end

  # LiveDNS information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def livedns
    @livedns ||= fetch_livedns
  end

  # Requery Gandi for the domain's LiveDNS information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_livedns
    _response, data = GandiV5.get url('livedns')
    @livedns = GandiV5::Domain::LiveDNS.from_gandi data
    @name_server = @livedns.current
    @name_servers = @livedns.name_servers
    @livedns
  end

  # Enable LiveDNS for the domain.
  # If you want to disable LiveDNS, change the nameservers.
  # Please note that if the domain is on the classic Gandi DNS,
  # this will also perform a copy of all existing records immediately afterwards.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-livedns
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def enable_livedns
    _response, data = GandiV5.post url('livedns')
    data['message']
  end

  # Name servers for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def name_servers
    @name_servers ||= fetch_name_servers
  end

  # Requery Gandi for the domain's name servers.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_name_servers
    _response, data = GandiV5.get url('nameservers')
    @name_servers = data
  end

  # Update name servers in Gandi.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-nameservers
  # @param nameservers [Array<String>] the name servers to use.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_name_servers(nameservers)
    _response, data = GandiV5.put url('nameservers'), { 'nameservers' => nameservers }.to_json
    @name_servers = nameservers
    data['message']
  end

  # Glue records for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_records
    @glue_records ||= fetch_glue_records
  end

  # Requery Gandi for the domain's glue records.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_glue_records
    _response, data = GandiV5.get url('hosts')
    @glue_records = data.map { |record| record.values_at('name', 'ips') }.to_h
  end

  # Add a new glue record to the domain in Gandi.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def add_glue_record(name, *ips)
    _response, data = GandiV5.post url('hosts'), { 'name' => name, 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Get a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @return [nil] name was not found
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_record(name)
    records = @glue_records.key?(name) ? @glue_records : fetch_glue_records
    records.key?(name) ? records.select { |k, _v| k == name } : nil
  end

  # Update a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_glue_record(name, *ips)
    _response, data = GandiV5.put url('hosts', name), { 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Delete a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#delete-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def delete_glue_record(name)
    _response, data = GandiV5.delete url('hosts', name)
    @glue_records ||= {}
    @glue_records.delete(name)
    data['message']
  end

  # @see GandiV5::Email::Mailbox.list
  def mailboxes(**params)
    GandiV5::Email::Mailbox.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Slot.list
  def mailbox_slots(**params)
    GandiV5::Email::Slot.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Forward.list
  def email_forwards(**params)
    GandiV5::Email::Forward.list(**params, fqdn: fqdn)
  end

  # Create (register) a new domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains
  # @param fqdn [String, #to_s] the fully qualified domain name to create.
  # @param dry_run [Boolean]
  #   whether the details should be checked instead of actually creating the domain.
  # @param sharing_id [String] either:
  #   * nil (default) - nothing special happens
  #   * an organization ID - pay using another organization
  #     (you need to have billing permissions on the organization
  #     and use the same organization name for the domain name's owner).
  #     The invoice will be edited using this organization's information.
  #   * a reseller ID - buy a domain for a customer using a reseller account
  #     (you need to have billing permissions on the reseller organization
  #     and have your customer's information for the owner).
  #     The invoice will be edited using the reseller organization's information.
  # @param owner [GandiV5::Domain::Contact, #to_gandi, #to_h] (required)
  #   the owner of the new domain.
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the administrative contact for the new domain.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the billing contact for the new domain.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the technical contact for the new domain.
  # @param claims [String] (optional) unknown - not documented at Gandi.
  # @param currency ["EUR", "USD", "GBP", "TWD", "CNY"] (optional)
  #   the currency you wish to be charged in.
  # @param duration [Integer] (optional, default 1, minimum 1 maximum 10)
  #   how many years to register for.
  # @param enforce_premium [Boolean] (optional)
  #   must be set to true if the domain is a premium domain.
  # @param extra_parameters [Hash, #to_gandi, #to_json] (optional)
  #   unknown - not documented at Gandi.
  # @param lang [String] (optional)
  #   ISO-639-2 language code of the domain, required for some IDN domains.
  # @param nameserver_ips [Hash<String => Array<String>>, #to_gandi, #to_json] (optional)
  #   For glue records only - dictionnary associating a nameserver to a list of IP addresses.
  # @param nameservers [Array<String>, #to_gandi, #to_json] (optional)
  #   List of nameservers. Gandi's LiveDNS nameservers are used if omitted..
  # @param price [Numeric, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param resellee_id [String, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param smd [String, #to_gandi, #to_json] (optional)
  #   Contents of a Signed Mark Data file (used for newgtld sunrises, tld_period must be sunrise).
  # @param tld_period ["sunrise", "landrush", "eap1", "eap2", "eap3", "eap4", "eap5", "eap6",
  #   "eap7", "eap8", "eap9", "golive", #to_gandi, #to_json] (optional)
  #   @see https://docs.gandi.net/en/domain_names/register/new_gtld.html
  # @return [GandiV5::Domain] the created domain
  # @return [Hash] if doing a dry run, you get what Gandi returns
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def self.create(fqdn, dry_run: false, sharing_id: nil, **params)
    fail ArgumentError, 'missing keyword: owner' unless params.key?(:owner)

    body = params.merge(fqdn: fqdn)
                 .transform_values { |val| val.respond_to?(:to_gandi) ? val.to_gandi : val }
                 .to_json
    url_ = sharing_id ? "#{url}?sharing_id=#{sharing_id}" : url

    response, data = GandiV5.post(url_, body, 'Dry-Run': dry_run ? 1 : 0)
    dry_run ? data : fetch(response.headers[:location].split('/').last)
  end

  # Get information on a domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain
  # @param fqdn [String, #to_s] the fully qualified domain name to fetch.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.fetch(fqdn)
    _response, data = GandiV5.get url(fqdn)
    domain = from_gandi data
    domain.auto_renew.domain = fqdn
    domain
  end

  # List domains.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains
  # @param page [#each<Integer, #to_s>] the page(s) of results to retrieve.
  #   If page is not provided keep querying until an empty list is returned.
  #   If page responds to .each then iterate until an empty list is returned.
  # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page.
  # @param fqdn [String, #to_s] (optional)
  #   filters the list by domain name, with optional patterns.
  #   e.g. "example.net", "example.*", "*ample.com"
  # @param sort_by [String, #to_s] (optional default "fqdn") how to sort the list.
  # @param tld [String, #to_s] (optional) used to filter by just the top level domain.
  # @return [Array<GandiV5::Domain>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.list(page: (1..), per_page: 100, **params)
    page = [page.to_i] unless page.respond_to?(:each)

    domains = []
    page.each do |page_number|
      _resp, data = GandiV5.get url, params: params.merge(page: page_number, per_page: per_page)
      break if data.empty?

      domains += data.map { |domain| from_gandi domain }
      break if data.count < per_page
    end
    domains
  end

  private

  def url(*extra)
    "#{BASE}domain/domains/" +
      CGI.escape(fqdn) +
      (extra.empty? ? '' : "/#{extra.join('/')}")
  end

  def self.url(fqdn = nil)
    "#{BASE}domain/domains" +
      (fqdn ? "/#{CGI.escape fqdn}" : '')
  end
  private_class_method :url
end

#uuidnil, String (readonly) Also known as: domain_uuid

Returns:

  • (nil, String)


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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/gandi_v5/domain.rb', line 52

class Domain
  include GandiV5::Data

  SERVICES = {
    gandidns: 'GandiDNS',
    gandilivedns: 'LiveDNS',
    dnssec: 'DNSSEC',
    certificate: 'SSL Certificate',
    paas: 'PAAS',
    redirection: 'Redirection',
    gandimail: 'GandiMail',
    packmail: 'PackMail',
    blog: 'Blog',
    hosting: 'Hosting',
    site: 'Site',
    mailboxv2: 'MailboxV2'
  }.freeze

  STATUSES = {
    clientHold: 'clientHold',
    clientUpdateProhibited: 'clientUpdateProhibited',
    clientTransferProhibited: 'clientTransferProhibited',
    clientDeleteProhibited: 'clientDeleteProhibited',
    clientRenewProhibited: 'clientRenewProhibited',
    serverHold: 'serverHold',
    pendingTransfer: 'pendingTransfer',
    serverTransferProhibited: 'serverTransferProhibited'
  }.freeze

  CONTACTS_CONVERTER = lambda { |hash|
    break {} if hash.nil?

    hash = hash.transform_keys(&:to_sym)
               .transform_values { |value| GandiV5::Domain::Contact.from_gandi value }

    hash.define_singleton_method(:owner) { send :'[]', :owner }
    hash.define_singleton_method(:admin) { send :'[]', :admin }
    hash.define_singleton_method(:bill) { send :'[]', :bill }
    hash.define_singleton_method(:tech) { send :'[]', :tech }

    hash
  }
  private_constant :CONTACTS_CONVERTER

  members :fqdn, :fqdn_unicode, :tld, :can_tld_lock, :tags, :owner, :domain_owner
  member :sharing_uuid, gandi_key: 'sharing_id'
  member :name_servers, gandi_key: 'nameservers'
  member :auth_info, gandi_key: 'authinfo'
  member :organisation_owner, gandi_key: 'orga_owner'
  member :uuid, gandi_key: 'id'

  member(
    :dates,
    converter: GandiV5::Domain::Dates
  )
  member(
    :sharing_space,
    gandi_key: 'sharing_space',
    converter: GandiV5::Domain::SharingSpace
  )

  member(
    :auto_renew,
    gandi_key: 'autorenew',
    converter: GandiV5::Data::Converter.new(from_gandi: lambda { |hash|
      break nil if hash.nil?
      break nil if hash.eql?(true)
      break GandiV5::Domain::AutoRenew.from_gandi('enabled' => false) if hash.eql?(false)

      GandiV5::Domain::AutoRenew.from_gandi hash
    })
  )

  member(
    :contacts,
    converter: GandiV5::Data::Converter.new(from_gandi: CONTACTS_CONVERTER)
  )

  member(
    :name_server,
    gandi_key: 'nameserver',
    converter: GandiV5::Data::Converter.new(from_gandi: ->(hash) { hash&.[]('current')&.to_sym })
  )
  member :services, converter: GandiV5::Data::Converter::Symbol, array: true
  member :status, converter: GandiV5::Data::Converter::Symbol, array: true
  member :trustee_roles, converter: GandiV5::Data::Converter::Symbol, array: true

  alias domain_uuid uuid

  # rubocop:disable Style/AsciiComments
  # Returns the string representation of the domain.
  # @return [String] e.g. "example.com", "😀.com (xn--e28h.uk.com)"
  # rubocop:enable Style/AsciiComments
  def to_s
    string = fqdn_unicode
    string += " (#{fqdn})" unless fqdn == fqdn_unicode
    string
  end

  # Contacts for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def contacts
    @contacts ||= fetch_contacts
  end

  # Requery Gandi for the domain's contacts.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-contacts
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_contacts
    _response, data = GandiV5.get url('contacts')
    self.contacts = data.transform_keys(&:to_sym)
    CONTACTS_CONVERTER.call contacts
  end

  # Update some of the domain's contacts.
  # @see https://api.gandi.net/docs/domains#patch-v5-domain-domains-domain-contacts
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new administrative contact.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new billing contact.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h]
  #   details for the new technical contact.
  # @return [Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_contacts(admin: nil, bill: nil, tech: nil)
    body = {
      admin: admin.respond_to?(:to_gandi) ? admin.to_gandi : admin,
      bill: bill.respond_to?(:to_gandi) ? bill.to_gandi : bill,
      tech: tech.respond_to?(:to_gandi) ? tech.to_gandi : tech
    }.reject { |_k, v| v.nil? }.to_json

    GandiV5.patch url('contacts'), body
    fetch_contacts
  end

  # Renewal information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renewal_information
    @renewal_information ||= fetch_renewal_information
  end

  # Requery Gandi for the domain's renewal information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-renew
  # @return [GandiV5::Domain::RenewalInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_renewal_information
    _response, data = GandiV5.get url('renew')
    data = data['renew'].merge('contracts' => data['contracts'])
    @renewal_information = GandiV5::Domain::RenewalInformation.from_gandi data
  end

  # Renew domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-renew
  # @param duration [Integer, #to_s] how long to renew for (in years).
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def renew_for(duration = 1)
    body = { duration: duration }.to_json
    _response, data = GandiV5.post url('renew'), body
    data['message']
  end

  # Restoration information for the domain.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore_information
    @restore_information ||= fetch_restore_information
  end

  # Requery Gandi for the domain's restore information.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-restore
  # @return [GandiV5::Domain::RestoreInformation]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_restore_information
    _response, data = GandiV5.get url('restore')
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi data
  rescue RestClient::NotFound
    @restore_information = GandiV5::Domain::RestoreInformation.from_gandi restorable: false
  end

  # Restore an expired domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://docs.gandi.net/en/domain_names/renew/restore.html
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-restore
  # @return [String] The confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def restore
    _response, data = GandiV5.post url('restore'), '{}'
    data['message']
  end

  # Requery Gandi fo this domain's information.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def refresh
    _response, data = GandiV5.get url
    from_gandi data
    auto_renew.domain = self
  end

  # Get the price for renewing this domain.
  # @param currency [String] the currency to get the price in (e.g. GBP)
  # @param period [Integer] the number of year(s) renewal to get the price for
  # @return [GandiV5::Domain::Availability::Product::Price]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def renewal_price(currency: 'GBP', period: 1)
    arguments = { processes: [:renew], currency: currency, period: period }
    GandiV5::Domain::Availability.fetch(fqdn, **arguments)
                                 .products.first
                                 .prices.first
  end

  # LiveDNS information for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def livedns
    @livedns ||= fetch_livedns
  end

  # Requery Gandi for the domain's LiveDNS information.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-livedns
  # @return [GandiV5::Domain::LiveDNS]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_livedns
    _response, data = GandiV5.get url('livedns')
    @livedns = GandiV5::Domain::LiveDNS.from_gandi data
    @name_server = @livedns.current
    @name_servers = @livedns.name_servers
    @livedns
  end

  # Enable LiveDNS for the domain.
  # If you want to disable LiveDNS, change the nameservers.
  # Please note that if the domain is on the classic Gandi DNS,
  # this will also perform a copy of all existing records immediately afterwards.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-livedns
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def enable_livedns
    _response, data = GandiV5.post url('livedns')
    data['message']
  end

  # Name servers for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def name_servers
    @name_servers ||= fetch_name_servers
  end

  # Requery Gandi for the domain's name servers.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-nameservers
  # @return [Array<String>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_name_servers
    _response, data = GandiV5.get url('nameservers')
    @name_servers = data
  end

  # Update name servers in Gandi.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-nameservers
  # @param nameservers [Array<String>] the name servers to use.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_name_servers(nameservers)
    _response, data = GandiV5.put url('nameservers'), { 'nameservers' => nameservers }.to_json
    @name_servers = nameservers
    data['message']
  end

  # Glue records for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_records
    @glue_records ||= fetch_glue_records
  end

  # Requery Gandi for the domain's glue records.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def fetch_glue_records
    _response, data = GandiV5.get url('hosts')
    @glue_records = data.map { |record| record.values_at('name', 'ips') }.to_h
  end

  # Add a new glue record to the domain in Gandi.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def add_glue_record(name, *ips)
    _response, data = GandiV5.post url('hosts'), { 'name' => name, 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Get a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain-hosts
  # @param name [String] the DNS name (excluding FQDN) for the glue record to add (e.g. 'ns1').
  # @return [Hash<String => Array<String>>] name to list of IPs
  # @return [nil] name was not found
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def glue_record(name)
    records = @glue_records.key?(name) ? @glue_records : fetch_glue_records
    records.key?(name) ? records.select { |k, _v| k == name } : nil
  end

  # Update a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#put-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @param ips [Array<String>] the IPs for the name.
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def update_glue_record(name, *ips)
    _response, data = GandiV5.put url('hosts', name), { 'ips' => ips }.to_json
    @glue_records ||= {}
    @glue_records[name] = ips
    data['message']
  end

  # Delete a specific glue record for the domain.
  # @see https://api.gandi.net/docs/domains#delete-v5-domain-domains-domain-hosts-name
  # @param name [String] the DNS name (excluding FQDN) for the glue record to update (e.g. 'ns1').
  # @return [String] confirmation message from Gandi.
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def delete_glue_record(name)
    _response, data = GandiV5.delete url('hosts', name)
    @glue_records ||= {}
    @glue_records.delete(name)
    data['message']
  end

  # @see GandiV5::Email::Mailbox.list
  def mailboxes(**params)
    GandiV5::Email::Mailbox.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Slot.list
  def mailbox_slots(**params)
    GandiV5::Email::Slot.list(**params, fqdn: fqdn)
  end

  # @see GandiV5::Email::Forward.list
  def email_forwards(**params)
    GandiV5::Email::Forward.list(**params, fqdn: fqdn)
  end

  # Create (register) a new domain.
  # Warning! This is not a free operation. Please ensure your prepaid account has enough credit.
  # @see https://api.gandi.net/docs/domains#post-v5-domain-domains
  # @param fqdn [String, #to_s] the fully qualified domain name to create.
  # @param dry_run [Boolean]
  #   whether the details should be checked instead of actually creating the domain.
  # @param sharing_id [String] either:
  #   * nil (default) - nothing special happens
  #   * an organization ID - pay using another organization
  #     (you need to have billing permissions on the organization
  #     and use the same organization name for the domain name's owner).
  #     The invoice will be edited using this organization's information.
  #   * a reseller ID - buy a domain for a customer using a reseller account
  #     (you need to have billing permissions on the reseller organization
  #     and have your customer's information for the owner).
  #     The invoice will be edited using the reseller organization's information.
  # @param owner [GandiV5::Domain::Contact, #to_gandi, #to_h] (required)
  #   the owner of the new domain.
  # @param admin [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the administrative contact for the new domain.
  # @param bill [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the billing contact for the new domain.
  # @param tech [GandiV5::Domain::Contact, #to_gandi, #to_h] (optional, defaults to owner)
  #   the technical contact for the new domain.
  # @param claims [String] (optional) unknown - not documented at Gandi.
  # @param currency ["EUR", "USD", "GBP", "TWD", "CNY"] (optional)
  #   the currency you wish to be charged in.
  # @param duration [Integer] (optional, default 1, minimum 1 maximum 10)
  #   how many years to register for.
  # @param enforce_premium [Boolean] (optional)
  #   must be set to true if the domain is a premium domain.
  # @param extra_parameters [Hash, #to_gandi, #to_json] (optional)
  #   unknown - not documented at Gandi.
  # @param lang [String] (optional)
  #   ISO-639-2 language code of the domain, required for some IDN domains.
  # @param nameserver_ips [Hash<String => Array<String>>, #to_gandi, #to_json] (optional)
  #   For glue records only - dictionnary associating a nameserver to a list of IP addresses.
  # @param nameservers [Array<String>, #to_gandi, #to_json] (optional)
  #   List of nameservers. Gandi's LiveDNS nameservers are used if omitted..
  # @param price [Numeric, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param resellee_id [String, #to_gandi, #to_json] (optional) unknown - not documented at Gandi.
  # @param smd [String, #to_gandi, #to_json] (optional)
  #   Contents of a Signed Mark Data file (used for newgtld sunrises, tld_period must be sunrise).
  # @param tld_period ["sunrise", "landrush", "eap1", "eap2", "eap3", "eap4", "eap5", "eap6",
  #   "eap7", "eap8", "eap9", "golive", #to_gandi, #to_json] (optional)
  #   @see https://docs.gandi.net/en/domain_names/register/new_gtld.html
  # @return [GandiV5::Domain] the created domain
  # @return [Hash] if doing a dry run, you get what Gandi returns
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error
  def self.create(fqdn, dry_run: false, sharing_id: nil, **params)
    fail ArgumentError, 'missing keyword: owner' unless params.key?(:owner)

    body = params.merge(fqdn: fqdn)
                 .transform_values { |val| val.respond_to?(:to_gandi) ? val.to_gandi : val }
                 .to_json
    url_ = sharing_id ? "#{url}?sharing_id=#{sharing_id}" : url

    response, data = GandiV5.post(url_, body, 'Dry-Run': dry_run ? 1 : 0)
    dry_run ? data : fetch(response.headers[:location].split('/').last)
  end

  # Get information on a domain.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains-domain
  # @param fqdn [String, #to_s] the fully qualified domain name to fetch.
  # @return [GandiV5::Domain]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.fetch(fqdn)
    _response, data = GandiV5.get url(fqdn)
    domain = from_gandi data
    domain.auto_renew.domain = fqdn
    domain
  end

  # List domains.
  # @see https://api.gandi.net/docs/domains#get-v5-domain-domains
  # @param page [#each<Integer, #to_s>] the page(s) of results to retrieve.
  #   If page is not provided keep querying until an empty list is returned.
  #   If page responds to .each then iterate until an empty list is returned.
  # @param per_page [Integer, #to_s] (optional default 100) how many results ot get per page.
  # @param fqdn [String, #to_s] (optional)
  #   filters the list by domain name, with optional patterns.
  #   e.g. "example.net", "example.*", "*ample.com"
  # @param sort_by [String, #to_s] (optional default "fqdn") how to sort the list.
  # @param tld [String, #to_s] (optional) used to filter by just the top level domain.
  # @return [Array<GandiV5::Domain>]
  # @raise [GandiV5::Error::GandiError] if Gandi returns an error.
  def self.list(page: (1..), per_page: 100, **params)
    page = [page.to_i] unless page.respond_to?(:each)

    domains = []
    page.each do |page_number|
      _resp, data = GandiV5.get url, params: params.merge(page: page_number, per_page: per_page)
      break if data.empty?

      domains += data.map { |domain| from_gandi domain }
      break if data.count < per_page
    end
    domains
  end

  private

  def url(*extra)
    "#{BASE}domain/domains/" +
      CGI.escape(fqdn) +
      (extra.empty? ? '' : "/#{extra.join('/')}")
  end

  def self.url(fqdn = nil)
    "#{BASE}domain/domains" +
      (fqdn ? "/#{CGI.escape fqdn}" : '')
  end
  private_class_method :url
end

Class Method Details

.create(fqdn, dry_run: false, sharing_id: nil, **params) ⇒ GandiV5::Domain, Hash

Create (register) a new domain. Warning! This is not a free operation. Please ensure your prepaid account has enough credit.

Parameters:

  • fqdn (String, #to_s)

    the fully qualified domain name to create.

  • dry_run (Boolean) (defaults to: false)

    whether the details should be checked instead of actually creating the domain.

  • sharing_id (String) (defaults to: nil)

    either:

    • nil (default) - nothing special happens

    • an organization ID - pay using another organization (you need to have billing permissions on the organization and use the same organization name for the domain name’s owner). The invoice will be edited using this organization’s information.

    • a reseller ID - buy a domain for a customer using a reseller account (you need to have billing permissions on the reseller organization and have your customer’s information for the owner). The invoice will be edited using the reseller organization’s information.

  • owner (GandiV5::Domain::Contact, #to_gandi, #to_h)

    (required) the owner of the new domain.

  • admin (GandiV5::Domain::Contact, #to_gandi, #to_h)

    (optional, defaults to owner) the administrative contact for the new domain.

  • bill (GandiV5::Domain::Contact, #to_gandi, #to_h)

    (optional, defaults to owner) the billing contact for the new domain.

  • tech (GandiV5::Domain::Contact, #to_gandi, #to_h)

    (optional, defaults to owner) the technical contact for the new domain.

  • claims (String)

    (optional) unknown - not documented at Gandi.

  • currency ("EUR", "USD", "GBP", "TWD", "CNY")

    (optional) the currency you wish to be charged in.

  • duration (Integer)

    (optional, default 1, minimum 1 maximum 10) how many years to register for.

  • enforce_premium (Boolean)

    (optional) must be set to true if the domain is a premium domain.

  • extra_parameters (Hash, #to_gandi, #to_json)

    (optional) unknown - not documented at Gandi.

  • lang (String)

    (optional) ISO-639-2 language code of the domain, required for some IDN domains.

  • nameserver_ips (Hash<String => Array<String>>, #to_gandi, #to_json)

    (optional) For glue records only - dictionnary associating a nameserver to a list of IP addresses.

  • nameservers (Array<String>, #to_gandi, #to_json)

    (optional) List of nameservers. Gandi’s LiveDNS nameservers are used if omitted..

  • price (Numeric, #to_gandi, #to_json)

    (optional) unknown - not documented at Gandi.

  • resellee_id (String, #to_gandi, #to_json)

    (optional) unknown - not documented at Gandi.

  • smd (String, #to_gandi, #to_json)

    (optional) Contents of a Signed Mark Data file (used for newgtld sunrises, tld_period must be sunrise).

  • tld_period ("sunrise", "landrush", "eap1", "eap2", "eap3", "eap4", "eap5", "eap6", "eap7", "eap8", "eap9", "golive", #to_gandi, #to_json)

Returns:

  • (GandiV5::Domain)

    the created domain

  • (Hash)

    if doing a dry run, you get what Gandi returns

Raises:

See Also:



463
464
465
466
467
468
469
470
471
472
473
# File 'lib/gandi_v5/domain.rb', line 463

def self.create(fqdn, dry_run: false, sharing_id: nil, **params)
  fail ArgumentError, 'missing keyword: owner' unless params.key?(:owner)

  body = params.merge(fqdn: fqdn)
               .transform_values { |val| val.respond_to?(:to_gandi) ? val.to_gandi : val }
               .to_json
  url_ = sharing_id ? "#{url}?sharing_id=#{sharing_id}" : url

  response, data = GandiV5.post(url_, body, 'Dry-Run': dry_run ? 1 : 0)
  dry_run ? data : fetch(response.headers[:location].split('/').last)
end

.fetch(fqdn) ⇒ GandiV5::Domain

Get information on a domain.

Parameters:

  • fqdn (String, #to_s)

    the fully qualified domain name to fetch.

Returns:

Raises:

See Also:



480
481
482
483
484
485
# File 'lib/gandi_v5/domain.rb', line 480

def self.fetch(fqdn)
  _response, data = GandiV5.get url(fqdn)
  domain = from_gandi data
  domain.auto_renew.domain = fqdn
  domain
end

.list(page: (1..), per_page: 100, **params) ⇒ Array<GandiV5::Domain>

List domains.

Parameters:

  • page (#each<Integer, #to_s>) (defaults to: (1..))

    the page(s) of results to retrieve. If page is not provided keep querying until an empty list is returned. If page responds to .each then iterate until an empty list is returned.

  • per_page (Integer, #to_s) (defaults to: 100)

    (optional default 100) how many results ot get per page.

  • fqdn (String, #to_s)

    (optional) filters the list by domain name, with optional patterns. e.g. “example.net”, “example.*”, “*ample.com”

  • sort_by (String, #to_s)

    (optional default “fqdn”) how to sort the list.

  • tld (String, #to_s)

    (optional) used to filter by just the top level domain.

Returns:

Raises:

See Also:



500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/gandi_v5/domain.rb', line 500

def self.list(page: (1..), per_page: 100, **params)
  page = [page.to_i] unless page.respond_to?(:each)

  domains = []
  page.each do |page_number|
    _resp, data = GandiV5.get url, params: params.merge(page: page_number, per_page: per_page)
    break if data.empty?

    domains += data.map { |domain| from_gandi domain }
    break if data.count < per_page
  end
  domains
end

Instance Method Details

#add_glue_record(name, *ips) ⇒ String

Add a new glue record to the domain in Gandi.

Parameters:

  • name (String)

    the DNS name (excluding FQDN) for the glue record to add (e.g. ‘ns1’).

  • ips (Array<String>)

    the IPs for the name.

Returns:

  • (String)

    confirmation message from Gandi.

Raises:

See Also:



356
357
358
359
360
361
# File 'lib/gandi_v5/domain.rb', line 356

def add_glue_record(name, *ips)
  _response, data = GandiV5.post url('hosts'), { 'name' => name, 'ips' => ips }.to_json
  @glue_records ||= {}
  @glue_records[name] = ips
  data['message']
end

#delete_glue_record(name) ⇒ String

Delete a specific glue record for the domain.

Parameters:

  • name (String)

    the DNS name (excluding FQDN) for the glue record to update (e.g. ‘ns1’).

Returns:

  • (String)

    confirmation message from Gandi.

Raises:

See Also:



392
393
394
395
396
397
# File 'lib/gandi_v5/domain.rb', line 392

def delete_glue_record(name)
  _response, data = GandiV5.delete url('hosts', name)
  @glue_records ||= {}
  @glue_records.delete(name)
  data['message']
end

#email_forwards(**params) ⇒ Object



410
411
412
# File 'lib/gandi_v5/domain.rb', line 410

def email_forwards(**params)
  GandiV5::Email::Forward.list(**params, fqdn: fqdn)
end

#enable_livednsString

Enable LiveDNS for the domain. If you want to disable LiveDNS, change the nameservers. Please note that if the domain is on the classic Gandi DNS, this will also perform a copy of all existing records immediately afterwards.

Returns:

  • (String)

    confirmation message from Gandi.

Raises:

See Also:



300
301
302
303
# File 'lib/gandi_v5/domain.rb', line 300

def enable_livedns
  _response, data = GandiV5.post url('livedns')
  data['message']
end

#fetch_contactsHash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}

Requery Gandi for the domain’s contacts.

Returns:

Raises:

See Also:



163
164
165
166
167
# File 'lib/gandi_v5/domain.rb', line 163

def fetch_contacts
  _response, data = GandiV5.get url('contacts')
  self.contacts = data.transform_keys(&:to_sym)
  CONTACTS_CONVERTER.call contacts
end

#fetch_glue_recordsHash<String => Array<String>>

Requery Gandi for the domain’s glue records.

Returns:

  • (Hash<String => Array<String>>)

    name to list of IPs

Raises:

See Also:



345
346
347
348
# File 'lib/gandi_v5/domain.rb', line 345

def fetch_glue_records
  _response, data = GandiV5.get url('hosts')
  @glue_records = data.map { |record| record.values_at('name', 'ips') }.to_h
end

#fetch_livednsGandiV5::Domain::LiveDNS

Requery Gandi for the domain’s LiveDNS information.



285
286
287
288
289
290
291
# File 'lib/gandi_v5/domain.rb', line 285

def fetch_livedns
  _response, data = GandiV5.get url('livedns')
  @livedns = GandiV5::Domain::LiveDNS.from_gandi data
  @name_server = @livedns.current
  @name_servers = @livedns.name_servers
  @livedns
end

#fetch_name_serversArray<String>

Requery Gandi for the domain’s name servers.

Returns:

  • (Array<String>)

Raises:

See Also:



317
318
319
320
# File 'lib/gandi_v5/domain.rb', line 317

def fetch_name_servers
  _response, data = GandiV5.get url('nameservers')
  @name_servers = data
end

#fetch_renewal_informationGandiV5::Domain::RenewalInformation

Requery Gandi for the domain’s renewal information.



202
203
204
205
206
# File 'lib/gandi_v5/domain.rb', line 202

def fetch_renewal_information
  _response, data = GandiV5.get url('renew')
  data = data['renew'].merge('contracts' => data['contracts'])
  @renewal_information = GandiV5::Domain::RenewalInformation.from_gandi data
end

#fetch_restore_informationGandiV5::Domain::RestoreInformation

Requery Gandi for the domain’s restore information.



234
235
236
237
238
239
# File 'lib/gandi_v5/domain.rb', line 234

def fetch_restore_information
  _response, data = GandiV5.get url('restore')
  @restore_information = GandiV5::Domain::RestoreInformation.from_gandi data
rescue RestClient::NotFound
  @restore_information = GandiV5::Domain::RestoreInformation.from_gandi restorable: false
end

#glue_record(name) ⇒ Hash<String => Array<String>>?

Get a specific glue record for the domain.

Parameters:

  • name (String)

    the DNS name (excluding FQDN) for the glue record to add (e.g. ‘ns1’).

Returns:

  • (Hash<String => Array<String>>)

    name to list of IPs

  • (nil)

    name was not found

Raises:

See Also:



369
370
371
372
# File 'lib/gandi_v5/domain.rb', line 369

def glue_record(name)
  records = @glue_records.key?(name) ? @glue_records : fetch_glue_records
  records.key?(name) ? records.select { |k, _v| k == name } : nil
end

#glue_recordsHash<String => Array<String>>

Glue records for the domain.

Returns:

  • (Hash<String => Array<String>>)

    name to list of IPs

Raises:

See Also:



337
338
339
# File 'lib/gandi_v5/domain.rb', line 337

def glue_records
  @glue_records ||= fetch_glue_records
end

#livednsGandiV5::Domain::LiveDNS

LiveDNS information for the domain.



277
278
279
# File 'lib/gandi_v5/domain.rb', line 277

def livedns
  @livedns ||= fetch_livedns
end

#mailbox_slots(**params) ⇒ Object

See Also:



405
406
407
# File 'lib/gandi_v5/domain.rb', line 405

def mailbox_slots(**params)
  GandiV5::Email::Slot.list(**params, fqdn: fqdn)
end

#mailboxes(**params) ⇒ Object



400
401
402
# File 'lib/gandi_v5/domain.rb', line 400

def mailboxes(**params)
  GandiV5::Email::Mailbox.list(**params, fqdn: fqdn)
end

#refreshGandiV5::Domain

Requery Gandi fo this domain’s information.

Returns:

Raises:



255
256
257
258
259
# File 'lib/gandi_v5/domain.rb', line 255

def refresh
  _response, data = GandiV5.get url
  from_gandi data
  auto_renew.domain = self
end

#renew_for(duration = 1) ⇒ String

Renew domain. Warning! This is not a free operation. Please ensure your prepaid account has enough credit.

Parameters:

  • duration (Integer, #to_s) (defaults to: 1)

    how long to renew for (in years).

Returns:

  • (String)

    confirmation message from Gandi.

Raises:

See Also:



214
215
216
217
218
# File 'lib/gandi_v5/domain.rb', line 214

def renew_for(duration = 1)
  body = { duration: duration }.to_json
  _response, data = GandiV5.post url('renew'), body
  data['message']
end

#renewal_informationGandiV5::Domain::RenewalInformation

Renewal information for the domain.



194
195
196
# File 'lib/gandi_v5/domain.rb', line 194

def renewal_information
  @renewal_information ||= fetch_renewal_information
end

#renewal_price(currency: 'GBP', period: 1) ⇒ GandiV5::Domain::Availability::Product::Price

Get the price for renewing this domain.

Parameters:

  • currency (String) (defaults to: 'GBP')

    the currency to get the price in (e.g. GBP)

  • period (Integer) (defaults to: 1)

    the number of year(s) renewal to get the price for

Returns:

Raises:



266
267
268
269
270
271
# File 'lib/gandi_v5/domain.rb', line 266

def renewal_price(currency: 'GBP', period: 1)
  arguments = { processes: [:renew], currency: currency, period: period }
  GandiV5::Domain::Availability.fetch(fqdn, **arguments)
                               .products.first
                               .prices.first
end

#restoreString

Restore an expired domain. Warning! This is not a free operation. Please ensure your prepaid account has enough credit.

Returns:

  • (String)

    The confirmation message from Gandi.

Raises:

See Also:



247
248
249
250
# File 'lib/gandi_v5/domain.rb', line 247

def restore
  _response, data = GandiV5.post url('restore'), '{}'
  data['message']
end

#restore_informationGandiV5::Domain::RestoreInformation

Restoration information for the domain.



225
226
227
# File 'lib/gandi_v5/domain.rb', line 225

def restore_information
  @restore_information ||= fetch_restore_information
end

#to_sString

rubocop:disable Style/AsciiComments Returns the string representation of the domain. rubocop:enable Style/AsciiComments

Returns:

  • (String)

    e.g. “example.com”, “😀.com (xn–e28h.uk.com)”



145
146
147
148
149
# File 'lib/gandi_v5/domain.rb', line 145

def to_s
  string = fqdn_unicode
  string += " (#{fqdn})" unless fqdn == fqdn_unicode
  string
end

#update_contacts(admin: nil, bill: nil, tech: nil) ⇒ Hash{:owner, :admin, :bill, :tech => GandiV5::Domain::Contact}

Update some of the domain’s contacts.

Parameters:

Returns:

Raises:

See Also:



179
180
181
182
183
184
185
186
187
188
# File 'lib/gandi_v5/domain.rb', line 179

def update_contacts(admin: nil, bill: nil, tech: nil)
  body = {
    admin: admin.respond_to?(:to_gandi) ? admin.to_gandi : admin,
    bill: bill.respond_to?(:to_gandi) ? bill.to_gandi : bill,
    tech: tech.respond_to?(:to_gandi) ? tech.to_gandi : tech
  }.reject { |_k, v| v.nil? }.to_json

  GandiV5.patch url('contacts'), body
  fetch_contacts
end

#update_glue_record(name, *ips) ⇒ String

Update a specific glue record for the domain.

Parameters:

  • name (String)

    the DNS name (excluding FQDN) for the glue record to update (e.g. ‘ns1’).

  • ips (Array<String>)

    the IPs for the name.

Returns:

  • (String)

    confirmation message from Gandi.

Raises:

See Also:



380
381
382
383
384
385
# File 'lib/gandi_v5/domain.rb', line 380

def update_glue_record(name, *ips)
  _response, data = GandiV5.put url('hosts', name), { 'ips' => ips }.to_json
  @glue_records ||= {}
  @glue_records[name] = ips
  data['message']
end

#update_name_servers(nameservers) ⇒ String

Update name servers in Gandi.

Parameters:

  • nameservers (Array<String>)

    the name servers to use.

Returns:

  • (String)

    confirmation message from Gandi.

Raises:

See Also:



327
328
329
330
331
# File 'lib/gandi_v5/domain.rb', line 327

def update_name_servers(nameservers)
  _response, data = GandiV5.put url('nameservers'), { 'nameservers' => nameservers }.to_json
  @name_servers = nameservers
  data['message']
end