Class: Gandi::Contact

Inherits:
Object
  • Object
show all
Includes:
GandiObjectMethods
Defined in:
lib/gandi/contact.rb

Constant Summary collapse

TYPES =

Contact types mapping

{
  0 => 'person',
  1 => 'company',
  2 => 'association',
  3 => 'organization',
  4 => 'reseller'
}
SECURITY_QUESTION_NUM_VALUES =

Security questions mapping

{
  1 => "What is the name of your favorite city?",
  2 => "What is your mother’s maiden name?",
  3 => "What is your favorite food?",
  4 => "What year were you born in?",
  5 => "What is your cell phone number?",
  6 => "In what year was your Gandi account created?"
}
WRITABLE_ATTRIBUTES =

Settable contact attributes (ie. not the id or handle)

:type, :orgname, :given, :family, :streetaddr, :city, :state, :zip, :country, :phone, :fax, :mobile,
:tva_number, :siren, :marque, :lang, :newsletter, :obfuscated, :whois_obfuscated, :resell, :shippingaddress,
:extra_parameters
CREATE_PARAMETERS_ATTRIBUTES =

Additional attributes used when creating an account

:password, :email,
:jo_announce_page, :jo_announce_number, :jo_declaration_date, :jo_publication_date,
:security_question_num, :security_question_answer
INFORMATION_ATTRIBUTES =

Attributes returned when calling contact.info or creating/updating a contact

WRITABLE_ATTRIBUTES + [:id]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GandiObjectMethods

#[], included, #inspect, #to_hash

Constructor Details

#initialize(handle = nil, contact = nil) ⇒ Contact

A new instance of Contact. Takes a Gandi handle and/or a contact hash with string keys. When providing only a handle the contact info will be fetched from the API.



44
45
46
47
48
# File 'lib/gandi/contact.rb', line 44

def initialize(handle = nil, contact = nil)
  @handle = handle
  @attributes = {}
  self.attributes=(contact) if (@handle || contact)
end

Instance Attribute Details

#handleObject (readonly)

Returns the value of attribute handle.



39
40
41
# File 'lib/gandi/contact.rb', line 39

def handle
  @handle
end

Class Method Details

.can_associate(contact, domain) ⇒ Object Also known as: can_associate?

Test if a contact (full contact description) can be associated to the domains. Takes a contact hash and a domain hash. TODO allow giving a string for the domain and converting it transparently, or a domain object. FIXME is checking multiple domains at once possible ? (it may be according to the Gandi documentation). FIXME OT&E seems to fail instead of returning an error hash (Error on object : OBJECT_APPLICATION (CAUSE_UNKNOWN) [Internal Server Error]).



138
139
140
# File 'lib/gandi/contact.rb', line 138

def can_associate(contact, domain)
  call('contact.can_associate', contact, domain)
end

.create(contact) ⇒ Object

Should have a all optional contact dict then look if he is a person or a company and depending on that call create_person or create_company. Returns a contact object. TODO filter params.



122
123
124
125
# File 'lib/gandi/contact.rb', line 122

def create(contact)
  contact = call('contact.create', contact)
  self.new(contact['handle'], contact)
end

.infoObject

Give all information on the contact linked to the apikey currently used. Returns a hash.



129
130
131
# File 'lib/gandi/contact.rb', line 129

def info
  call('contact.info')
end

.list(opts = {}, map_contacts = true) ⇒ Object

List all contacts linked to the connected user (it will only return contacts when the apikey belong to a reseller). The array of returned contacts are mapped to contact objects, set map_contacts to false to get contact info hashes.



147
148
149
150
151
152
153
154
155
# File 'lib/gandi/contact.rb', line 147

def list(opts = {}, map_contacts = true)
  contacts = call('contact.list', opts)
  if map_contacts
    contacts.map! do |contact|
      self.new(contact['handle'], contact)
    end
  end
  contacts
end

.update(handle, contact) ⇒ Object

Check the same way as check and then update. Returns a contact object. TODO filter params.



160
161
162
163
# File 'lib/gandi/contact.rb', line 160

def update(handle, contact)
  contact = call('contact.update', handle, contact)
  self.new(contact['handle'], contact)
end

Instance Method Details

#[]=(attribute, value) ⇒ Object

Sets a contact attribute value.

Raises:



103
104
105
106
# File 'lib/gandi/contact.rb', line 103

def []=(attribute, value)
  raise DataError, "Attribute #{attribute} is read-only" unless WRITABLE_ATTRIBUTES.include?(attribute.to_sym)
  @attributes[attribute.to_s] = value
end

#can_associate_domain(domain) ⇒ Object Also known as: can_associate_domain?

Test if a contact (defined by its handle) can create that domain. Takes a domain hash. TODO allow giving a string for the domain and converting it transparently, or a domain object. FIXME is checking multiple domains at once possible ? (it may be according to the Gandi documentation). FIXME OT&E seems to fail instead of returning an error hash (Error on object : OBJECT_APPLICATION (CAUSE_UNKNOWN) [Internal Server Error]).



55
56
57
58
# File 'lib/gandi/contact.rb', line 55

def can_associate_domain(domain)
  return false unless persisted?
  self.class.call('contact.can_associate_domain', @handle, domain)
end

#idObject

Returns the contact unique identifier.



88
89
90
# File 'lib/gandi/contact.rb', line 88

def id
  @attributes['id']
end

#infoObject

Give all information on the given contact. This should not be directly used as initializing the class already fetches the contact info.

Raises:



65
66
67
68
# File 'lib/gandi/contact.rb', line 65

def info
  raise DataError, "Cannot get informations for a new contact" unless persisted?
  self.class.call('contact.info', @handle)
end

#persisted?Boolean

Returns true if the contact exists on Gandi databases.

Returns:

  • (Boolean)


114
115
116
# File 'lib/gandi/contact.rb', line 114

def persisted?
  !!@handle
end

#to_sObject

Returns a string containing the handle of the contact.



109
110
111
# File 'lib/gandi/contact.rb', line 109

def to_s
  @handle || ''
end

#typeObject

Returns the contact type string.



93
94
95
# File 'lib/gandi/contact.rb', line 93

def type
  TYPES[@attributes['type']]
end

#type=(type_string_or_id) ⇒ Object

Sets the contact type (provided with a type string or id).



98
99
100
# File 'lib/gandi/contact.rb', line 98

def type=(type_string_or_id)
  @attributes['type'] = TYPES.invert[type_string_or_id] || type_string_or_id
end

#update(contact) ⇒ Object

Update a contact with the given information. If the contact is associated to domains, it will check the same rules as can_associate* and then update if possible.

Raises:



72
73
74
75
# File 'lib/gandi/contact.rb', line 72

def update(contact)
  raise DataError, "Cannot update a new contact, use Gandi::Contact.create instead" unless persisted?
  self.attributes = self.class.call('contact.update', @handle, contact)
end