Class: DNSimple::Contact
- Inherits:
-
Object
- Object
- DNSimple::Contact
- Includes:
- HTTParty
- Defined in:
- lib/dnsimple/contact.rb
Overview
CLass representing a contact in DNSimple
Instance Attribute Summary collapse
-
#address1 ⇒ Object
The contact street address.
-
#address2 ⇒ Object
Apartment or suite number.
-
#city ⇒ Object
The city name.
-
#country ⇒ Object
The contact country (as a 2-character country code).
-
#created_at ⇒ Object
When the contact was created in DNSimple.
-
#email_address ⇒ Object
The contact email address.
-
#fax ⇒ Object
The contact fax number (may be omitted).
-
#first_name ⇒ Object
The contact first name.
-
#id ⇒ Object
The contact ID in DNSimple.
-
#job_title ⇒ Object
The contact’s job title (may be omitted).
-
#last_name ⇒ Object
The contact last name.
-
#organization_name ⇒ Object
The name of the organization in which the contact works (may be omitted).
-
#phone ⇒ Object
The contact phone number.
-
#phone_ext ⇒ Object
The contact phone extension (may be omitted).
-
#postal_code ⇒ Object
The contact postal code.
-
#state_province ⇒ Object
The state or province name.
-
#updated_at ⇒ Object
When the contact was last updated in DNSimple.
Class Method Summary collapse
- .all(options = {}) ⇒ Object
-
.create(attributes, options = {}) ⇒ Object
Create the contact with the given attributes in DNSimple.
- .find(id, options = {}) ⇒ Object
-
.resolve(name) ⇒ Object
Map an aliased field name to it’s real name.
- .resolve_attributes(attributes) ⇒ Object
Instance Method Summary collapse
-
#delete(options = {}) ⇒ Object
(also: #destroy)
Delete the contact from DNSimple.
-
#initialize(attributes) ⇒ Contact
constructor
:nodoc:.
- #name ⇒ Object
- #save(options = {}) ⇒ Object
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
#address1 ⇒ Object
The contact street address
23 24 25 |
# File 'lib/dnsimple/contact.rb', line 23 def address1 @address1 end |
#address2 ⇒ Object
Apartment or suite number
26 27 28 |
# File 'lib/dnsimple/contact.rb', line 26 def address2 @address2 end |
#city ⇒ Object
The city name
29 30 31 |
# File 'lib/dnsimple/contact.rb', line 29 def city @city end |
#country ⇒ Object
The contact country (as a 2-character country code)
38 39 40 |
# File 'lib/dnsimple/contact.rb', line 38 def country @country end |
#created_at ⇒ Object
When the contact was created in DNSimple
53 54 55 |
# File 'lib/dnsimple/contact.rb', line 53 def created_at @created_at end |
#email_address ⇒ Object
The contact email address
41 42 43 |
# File 'lib/dnsimple/contact.rb', line 41 def email_address @email_address end |
#fax ⇒ Object
The contact fax number (may be omitted)
50 51 52 |
# File 'lib/dnsimple/contact.rb', line 50 def fax @fax end |
#first_name ⇒ Object
The contact first name
14 15 16 |
# File 'lib/dnsimple/contact.rb', line 14 def first_name @first_name end |
#id ⇒ Object
The contact ID in DNSimple
7 8 9 |
# File 'lib/dnsimple/contact.rb', line 7 def id @id end |
#job_title ⇒ Object
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_name ⇒ Object
The contact last name
17 18 19 |
# File 'lib/dnsimple/contact.rb', line 17 def last_name @last_name end |
#organization_name ⇒ Object
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 |
#phone ⇒ Object
The contact phone number
44 45 46 |
# File 'lib/dnsimple/contact.rb', line 44 def phone @phone end |
#phone_ext ⇒ Object
The contact phone extension (may be omitted)
47 48 49 |
# File 'lib/dnsimple/contact.rb', line 47 def phone_ext @phone_ext end |
#postal_code ⇒ Object
The contact postal code
35 36 37 |
# File 'lib/dnsimple/contact.rb', line 35 def postal_code @postal_code end |
#state_province ⇒ Object
The state or province name
32 33 34 |
# File 'lib/dnsimple/contact.rb', line 32 def state_province @state_province end |
#updated_at ⇒ Object
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(={}) .merge!({:basic_auth => Client.credentials}) response = self.get("#{Client.base_uri}/contacts.json", ) 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, ={}) contact_hash = resolve_attributes(attributes) .merge!({:body => {:contact => contact_hash}}) .merge!({:basic_auth => Client.credentials}) response = self.post("#{Client.base_uri}/contacts.json", ) 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, ={}) .merge!({:basic_auth => Client.credentials}) response = self.get("#{Client.base_uri}/contacts/#{id}.json", ) 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(={}) .merge!({:basic_auth => Client.credentials}) self.class.delete("#{Client.base_uri}/contacts/#{id}.json", ) end |
#name ⇒ Object
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(={}) 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 .merge!(DNSimple::Client.) .merge!({:body => {:contact => contact_hash}}) response = self.class.put("#{Client.base_uri}/contacts/#{id}", ) 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 |