Class: DNSimple::Contact

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

Overview

Represents a contact.

Constant Summary collapse

Aliases =
{
  'first'             => 'first_name',
  'last'              => 'last_name',
  'state'             => 'state_province',
  'province'          => 'state_province',
  'state_or_province' => 'state_province',
  'email'             => 'email_address',
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from DNSimple::Base

Instance Attribute Details

#address1Object

The contact street address



43
44
45
# File 'lib/dnsimple/contact.rb', line 43

def address1
  @address1
end

#address2Object

Apartment or suite number



46
47
48
# File 'lib/dnsimple/contact.rb', line 46

def address2
  @address2
end

#cityObject

The city name



49
50
51
# File 'lib/dnsimple/contact.rb', line 49

def city
  @city
end

#countryObject

The contact country (as a 2-character country code)



58
59
60
# File 'lib/dnsimple/contact.rb', line 58

def country
  @country
end

#created_atObject

When the contact was created in DNSimple



61
62
63
# File 'lib/dnsimple/contact.rb', line 61

def created_at
  @created_at
end

#email_addressObject

The contact email address



31
32
33
# File 'lib/dnsimple/contact.rb', line 31

def email_address
  @email_address
end

#faxObject

The contact fax number (may be omitted)



40
41
42
# File 'lib/dnsimple/contact.rb', line 40

def fax
  @fax
end

#first_nameObject

The contact first name



19
20
21
# File 'lib/dnsimple/contact.rb', line 19

def first_name
  @first_name
end

#idObject

The contact ID in DNSimple



16
17
18
# File 'lib/dnsimple/contact.rb', line 16

def id
  @id
end

#job_titleObject

The contact’s job title



25
26
27
# File 'lib/dnsimple/contact.rb', line 25

def job_title
  @job_title
end

#last_nameObject

The contact last name



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

def last_name
  @last_name
end

#organization_nameObject

The name of the organization in which the contact works



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

def organization_name
  @organization_name
end

#phoneObject

The contact phone number



34
35
36
# File 'lib/dnsimple/contact.rb', line 34

def phone
  @phone
end

#phone_extObject

The contact phone extension (may be omitted)



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

def phone_ext
  @phone_ext
end

#postal_codeObject

The contact postal code



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

def postal_code
  @postal_code
end

#state_provinceObject

The state or province name



52
53
54
# File 'lib/dnsimple/contact.rb', line 52

def state_province
  @state_province
end

#updated_atObject

When the contact was last updated in DNSimple



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

def updated_at
  @updated_at
end

Class Method Details

.all(options = {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/dnsimple/contact.rb', line 112

def self.all(options={})
  response = DNSimple::Client.get("/v1/contacts", options)

  case response.code
  when 200
    response.map { |r| new(r["contact"]) }
  else
    raise RequestError.new("Error listing contacts", response)
  end
end

.create(attributes, options = {}) ⇒ Object

Create the contact with the given attributes in DNSimple. This method returns a Contact instance of the contact is created and raises an error otherwise.



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dnsimple/contact.rb', line 85

def self.create(attributes, options={})
  contact_hash = resolve_attributes(attributes)

  options.merge!({:body => {:contact => contact_hash}})
  response = DNSimple::Client.post("/v1/contacts", options)

  case response.code
  when 201
    new(response["contact"])
  else
    raise RequestError.new("Error creating contact", response)
  end
end

.find(id, options = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/dnsimple/contact.rb', line 99

def self.find(id, options={})
  response = DNSimple::Client.get("/v1/contacts/#{id}", options)

  case response.code
  when 200
    new(response["contact"])
  when 404
    raise RecordNotFound, "Could not find contact #{id}"
  else
    raise RequestError.new("Error finding contact", response)
  end
end

.resolve(name) ⇒ Object

Map an aliased field name to it’s real name. For example, if you pass “first” it will be resolved to “first_name”, “email” is resolved to “email_address” and so on.



70
71
72
# File 'lib/dnsimple/contact.rb', line 70

def self.resolve(name)
  DNSimple::Contact::Aliases[name.to_s] || name
end

.resolve_attributes(attributes) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/dnsimple/contact.rb', line 74

def self.resolve_attributes(attributes)
  resolved_attributes = {}
  attributes.each do |k, v|
    resolved_attributes[resolve(k)] = v
  end
  resolved_attributes
end

Instance Method Details

#delete(options = {}) ⇒ Object Also known as: destroy

Delete the contact from DNSimple. WARNING: this cannot be undone.



148
149
150
# File 'lib/dnsimple/contact.rb', line 148

def delete(options={})
  DNSimple::Client.delete("/v1/contacts/#{id}", options)
end

#nameObject



124
125
126
# File 'lib/dnsimple/contact.rb', line 124

def name
  [first_name, last_name].join(' ')
end

#save(options = {}) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/dnsimple/contact.rb', line 128

def save(options={})
  contact_hash = {}
  %w(first_name last_name organization_name job_title address1 address2 city
  state_province postal_code country email_address phone phone_ext fax).each do |attribute|
    contact_hash[DNSimple::Contact.resolve(attribute)] = self.send(attribute)
  end

  options.merge!({:body => {:contact => contact_hash}})

  response = DNSimple::Client.put("/v1/contacts/#{id}", options)

  case response.code
    when 200
      return self
    else
      raise RequestError.new("Error updating contact", response)
  end
end