Class: DNSimple::Contact

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

Overview

CLass representing a contact in DNSimple

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Contact

:nodoc:



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

def initialize(attributes)
  attributes.each do |key, value|
    m = "#{key}=".to_sym
    self.send(m, value) if self.respond_to?(m)
  end
end

Instance Attribute Details

#address1Object

The contact street address



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

def address1
  @address1
end

#address2Object

Apartment or suite number



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

def address2
  @address2
end

#cityObject

The city name



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

def city
  @city
end

#countryObject

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



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

def country
  @country
end

#created_atObject

When the contact was created in DNSimple



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

def created_at
  @created_at
end

#email_addressObject

The contact email address



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

def email_address
  @email_address
end

#faxObject

The contact fax number (may be omitted)



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

def fax
  @fax
end

#first_nameObject

The contact first name



14
15
16
# File 'lib/dnsimple/contact.rb', line 14

def first_name
  @first_name
end

#idObject

The contact ID in DNSimple



7
8
9
# File 'lib/dnsimple/contact.rb', line 7

def id
  @id
end

#job_titleObject

The contact’s job title (may be omitted)



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

def job_title
  @job_title
end

#last_nameObject

The contact last name



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

def last_name
  @last_name
end

#organization_nameObject

The name of the organization in which the contact works (may be omitted)



11
12
13
# File 'lib/dnsimple/contact.rb', line 11

def organization_name
  @organization_name
end

#phoneObject

The contact phone number



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

def phone
  @phone
end

#phone_extObject

The contact phone extension (may be omitted)



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

def phone_ext
  @phone_ext
end

#postal_codeObject

The contact postal code



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

def postal_code
  @postal_code
end

#state_provinceObject

The state or province name



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

def state_province
  @state_province
end

#updated_atObject

When the contact was last updated in DNSimple



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

def updated_at
  @updated_at
end

Class Method Details

.all(options = {}) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/dnsimple/contact.rb', line 165

def self.all(options={})
  options.merge!({:basic_auth => Client.credentials})
  response = self.get("#{Client.base_uri}/contacts.json", options)

  pp response if Client.debug?

  case response.code
  when 200
    response.map { |r| Contact.new(r["contact"]) }
  when 401
    raise RuntimeError, "Authentication failed"
  else
    raise RuntimeError, "Error: #{response.code}"
  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.



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

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

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

  response = self.post("#{Client.base_uri}/contacts.json", options)

  pp response if Client.debug?

  case response.code
  when 201
    return Contact.new(response["contact"])
  when 401
    raise RuntimeError, "Authentication failed"
  else
    raise RuntimeError, "Failed to create contact: #{response.inspect}"
  end
end

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



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/dnsimple/contact.rb', line 147

def self.find(id, options={})
  options.merge!({:basic_auth => Client.credentials})
  response = self.get("#{Client.base_uri}/contacts/#{id}.json", options)

  pp response if Client.debug?

  case response.code
  when 200
    return Contact.new(response["contact"])
  when 401
    raise RuntimeError, "Authentication failed"
  when 404
    raise RuntimeError, "Could not find contact #{id}"
  else
    raise DNSimple::Error.new(id, response["errors"])
  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.



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/dnsimple/contact.rb', line 104

def self.resolve(name)
  aliases = {
    'first' => 'first_name',
    'last' => 'last_name',
    'state' => 'state_province',
    'province' => 'state_province',
    'state_or_province' => 'state_province',
    'email' => 'email_address',
  }
  aliases[name.to_s] || name
end

.resolve_attributes(attributes) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/dnsimple/contact.rb', line 116

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.



95
96
97
98
# File 'lib/dnsimple/contact.rb', line 95

def delete(options={})
  options.merge!({:basic_auth => Client.credentials})
  self.class.delete("#{Client.base_uri}/contacts/#{id}.json", options)
end

#nameObject



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

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

#save(options = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dnsimple/contact.rb', line 70

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[Contact.resolve(attribute)] = self.send(attribute)
  end

  options.merge!(DNSimple::Client.standard_options_with_credentials)
  options.merge!({:body => {:contact => contact_hash}})
  
  response = self.class.put("#{Client.base_uri}/contacts/#{id}", options)

  pp response if Client.debug?

  case response.code
  when 200
    return self
  when 401
    raise RuntimeError, "Authentication failed"
  else
    raise RuntimeError, "Failed to update contact: #{response.inspect}"
  end
end