Class: Gandi::Contact

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

Constant Summary collapse

COUNTRIES =
['AF','ZA','AL','DZ','DE','AD','AO','AI','AG','AN','SA','AR','AM','AW','AC','AU','AT','AZ','BS','BH','BD','BB','BY','BE','BZ','BJ','BM','BT','BO','BA','BW','BR','BN','BG','BF','BI','KH','CM','CA','CV','KY','CF','CL','CN','CY','CO','KM','CG','CD','CK','KR','KP','CR','CI','HR','CU','DK','DJ','DO','DM','EG','SV','AE','EC','ER','ES','EE','US','ET','FK','FO','FJ','FI','FR','GA','GM','GE','GH','GI','GR','GD','GL','GU','GT','GN','GQ','GW','GY','HT','HI','HN','HK','HU','IN','ID','IR','IQ','IE','IS','IL','IT','JM','JP','JE','JO','KZ','KE','KG','KI','KW','LA','LS','LV','LB','LR','LY','LI','LT','LU','MO','MK','MG','MY','MW','MV','ML','MT','MP','MA','MH','MU','MR','MX','FM','MD','MC','MN','MS','MZ','MM','NA','NR','NP','NI','NE','NG','NU','NF','NO','NZ','OM','UG','UZ','PK','PW','PS','PA','PG','PY','NL','PE','PH','PL','PT','PR','QA','RO','GB','RU','RW','SH','LC','KN','SM','VC','SB','AS','WS','ST','SN','SC','SL','SG','SK','SI','SO','SD','LK','SE','CH','SR','SZ','SY','TJ','TW','TZ','TD','CZ','TH','TP','TG','TK','TO','TT','TN','TM','TC','TR','TV','UA','UY','VU','VE','VI','VG','VN','YE','ZM','ZW','RE','PF','AQ','GF','NC','MQ','GP','PM','TF','YT','RS','TL','CC','CX','HM','AX','BV','GS','GG','IM','ME','IO','PN','EH','BL','MF','VA','SJ','WF','EP']
TYPES =
{0 => 'person', 1 => 'company', 2 => 'association', 3 => 'public_body'}
ATTRIBUTE_MAP =
{
  'type'              => 'type',
  'given'             => 'first_name',
  'family'            => 'last_name',
  'orgname'           => 'organization',
  'password'          => 'password',
  'streetaddr'        => 'address',
  'city'              => 'city',
  'zip'               => 'post_code',
  'country'           => 'country',
  'email'             => 'email_address',
  'phone'             => 'phone',
  'extra_parameters'  => 'extra_parameters'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContact

Returns a new instance of Contact.



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

def initialize
  @attributes = {}
end

Instance Attribute Details

#addressObject

Address Fields



35
36
37
# File 'lib/gandi/contact.rb', line 35

def address
  @address
end

#attributesObject (readonly)

Returns the value of attribute attributes.



23
24
25
# File 'lib/gandi/contact.rb', line 23

def attributes
  @attributes
end

#cityObject

Returns the value of attribute city.



36
37
38
# File 'lib/gandi/contact.rb', line 36

def city
  @city
end

#countryObject

Returns the value of attribute country.



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

def country
  @country
end

#email_addressObject

Contact details



41
42
43
# File 'lib/gandi/contact.rb', line 41

def email_address
  @email_address
end

#extra_parametersObject

Return extra parameters for this contact



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

def extra_parameters
  @extra_parameters
end

#first_nameObject

Name



27
28
29
# File 'lib/gandi/contact.rb', line 27

def first_name
  @first_name
end

#handleObject (readonly)

Type of object



22
23
24
# File 'lib/gandi/contact.rb', line 22

def handle
  @handle
end

#last_nameObject

Returns the value of attribute last_name.



28
29
30
# File 'lib/gandi/contact.rb', line 28

def last_name
  @last_name
end

#organizationObject

Returns the value of attribute organization.



29
30
31
# File 'lib/gandi/contact.rb', line 29

def organization
  @organization
end

#passwordObject

Credentials



32
33
34
# File 'lib/gandi/contact.rb', line 32

def password
  @password
end

#phoneObject

Returns the value of attribute phone.



42
43
44
# File 'lib/gandi/contact.rb', line 42

def phone
  @phone
end

#post_codeObject

Returns the value of attribute post_code.



37
38
39
# File 'lib/gandi/contact.rb', line 37

def post_code
  @post_code
end

#typeObject

Returns the value of attribute type.



24
25
26
# File 'lib/gandi/contact.rb', line 24

def type
  @type
end

Class Method Details

.find(handle) ⇒ Object

Find a contact based on it’s handle



52
53
54
55
56
57
58
59
# File 'lib/gandi/contact.rb', line 52

def self.find(handle)
  info = Gandi.call("contact.info", handle)
  contact = self.new
  contact.set_attributes(info)
  contact
rescue Gandi::DataError => e
  e.message =~ /CAUSE_NOTFOUND/ ? nil : raise
end

Instance Method Details

#can_associate?(domain) ⇒ Boolean

Return whether this contact is suitable for use with a certain domain

Returns:

  • (Boolean)


181
182
183
184
185
186
187
188
189
# File 'lib/gandi/contact.rb', line 181

def can_associate?(domain)
  spec = {'domain' => domain, 'owner' => true, 'admin' => true}
  res = Gandi.call('contact.can_associate_domain', self.handle, spec)
  if res == true
    true
  else
    res
  end
end

#createObject

Create a contact



85
86
87
88
89
90
91
# File 'lib/gandi/contact.rb', line 85

def create
  return false unless new_record?
  raise Gandi::ValidationError, self.errors unless self.errors.empty?
  result = Gandi.call('contact.create', dirty_attributes_with_remote_keys)
  set_attributes(result)
  true
end

#dirty?Boolean

Is the current instance dirty? (I.e. need updating remotely)

Returns:

  • (Boolean)


107
108
109
# File 'lib/gandi/contact.rb', line 107

def dirty?
  dirty_attributes.empty?
end

#dirty_attributesObject

Returns a hash of all dirty attributes (with local keys)



114
115
116
117
118
119
120
121
122
123
# File 'lib/gandi/contact.rb', line 114

def dirty_attributes
  ATTRIBUTE_MAP.values.inject(Hash.new) do |hash, local|
    if @attributes.keys.include?(local) && @attributes[local] == self.send(local)
      next hash
    else
      hash[local] = self.send(local)
    end
    hash
  end
end

#dirty_attributes_with_remote_keysObject

Returns a hash of all dirty_attributes (with remote keys)



128
129
130
131
132
133
134
# File 'lib/gandi/contact.rb', line 128

def dirty_attributes_with_remote_keys
  dirty_attributes.inject(Hash.new) do |hash, (local, value)|
    remote = ATTRIBUTE_MAP.select { |k,v| v == local }.first[0]
    hash[remote] = value || ''
    hash
  end
end

#domainsObject

Return an array of domains associated with this contact



194
195
196
197
# File 'lib/gandi/contact.rb', line 194

def domains
  return [] if new_record?
  Gandi.call('domain.list', {'handle' => self.handle}).map { |d| Domain.new(d, true)}
end

#errorsObject

Validate the contact looks OK to avoid getting ugly-ass and unhelpful messages from the Gandi endpoint.

Returns an array of problems or empty if no issues.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/gandi/contact.rb', line 156

def errors
  Hash.new.tap do |a|
    a[:type] = "must be one of #{TYPES.keys.join(',')}" unless TYPES.keys.include?(self.type)
    a[:city]          = 'is required'   if is_blank?(self.city)
    a[:first_name]    = 'is required'   if is_blank?(self.first_name)
    a[:last_name]     = 'is required'   if is_blank?(self.last_name)
    a[:organization]  = 'is required'   if is_blank?(self.organization) && [1,2,3].include?(self.type)
    a[:country]       = 'is invalid'    unless COUNTRIES.include?(self.country)
    a[:address]       = 'is required'   if is_blank?(self.address)
    a[:phone]         = 'is invalid'    unless self.phone =~ /^\+\d{1,3}\.\d+$/
    a[:password]      = 'is required'   if is_blank?(self.password) && new_record?
    a[:email_address] = 'is invalid'    unless self.email_address =~ /^(([a-z0-9_\.\+\-\=\?\^\#]){1,64}\@(([a-z0-9\-]){1,251}\.){1,252}[-a-z0-9]{2,63})$/
  end
end

#is_blank?(variable) ⇒ Boolean

Is the given variable blank?

Returns:

  • (Boolean)


174
175
176
# File 'lib/gandi/contact.rb', line 174

def is_blank?(variable)
  variable.nil? || variable.length == 0
end

#new_record?Boolean

Is this a new record?

Returns:

  • (Boolean)


78
79
80
# File 'lib/gandi/contact.rb', line 78

def new_record?
  self.handle.nil?
end

#saveObject

Save the contact’s information



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

def save
  new_record? ? create : update
end

#set_attributes(hash) ⇒ Object

Set the attributes from a remote hash



139
140
141
142
143
144
145
146
147
148
# File 'lib/gandi/contact.rb', line 139

def set_attributes(hash)
  @handle = hash['handle']
  @attributes = {}
  ATTRIBUTE_MAP.each do |remote, local|
    self.send(local + '=', hash[remote])
    remote_value = hash[remote]
    remote_value = remote_value.dup if remote_value && !remote_value.is_a?(Fixnum)
    @attributes[local] = remote_value
  end
end

#updateObject

Update a contact



96
97
98
99
100
101
102
# File 'lib/gandi/contact.rb', line 96

def update
  return false if new_record?
  raise Gandi::ValidationError, self.errors unless self.errors.empty?
  result = Gandi.call('contact.update', self.handle, dirty_attributes_with_remote_keys)
  set_attributes(result)
  true
end