dnsimple-ruby 1.2.6 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,220 +1,171 @@
1
- module DNSimple #:nodoc:
2
- # Class representing a single domain in DNSimple.
3
- class Domain
4
- include HTTParty
5
-
6
- # The domain ID in DNSimple
7
- attr_accessor :id
8
-
9
- # The domain name
10
- attr_accessor :name
11
-
12
- # When the domain was created in DNSimple
13
- attr_accessor :created_at
14
-
15
- # When the domain was last update in DNSimple
16
- attr_accessor :updated_at
17
-
18
- # The current known name server status
19
- attr_accessor :name_server_status
20
-
21
- #:nodoc:
22
- def initialize(attributes)
23
- attributes.each do |key, value|
24
- m = "#{key}=".to_sym
25
- self.send(m, value) if self.respond_to?(m)
26
- end
27
- end
1
+ class DNSimple::Domain < DNSimple::Base # Class representing a single domain in DNSimple.
2
+ # The domain ID in DNSimple
3
+ attr_accessor :id
28
4
 
29
- # Delete the domain from DNSimple. WARNING: this cannot
30
- # be undone.
31
- def delete(options={})
32
- options.merge!(DNSimple::Client.standard_options_with_credentials)
33
- self.class.delete("#{DNSimple::Client.base_uri}/domains/#{name}", options)
34
- end
35
- alias :destroy :delete
36
-
37
- # Apply the given named template to the domain. This will add
38
- # all of the records in the template to the domain.
39
- def apply(template, options={})
40
- options.merge!(DNSimple::Client.standard_options_with_credentials)
41
- options.merge!(:body => {})
42
- template = resolve_template(template)
43
- self.class.post("#{DNSimple::Client.base_uri}/domains/#{name}/templates/#{template.id}/apply", options)
44
- end
5
+ # The domain name
6
+ attr_accessor :name
45
7
 
46
- #:nodoc:
47
- def resolve_template(template)
48
- case template
49
- when DNSimple::Template
50
- template
51
- else
52
- DNSimple::Template.find(template)
53
- end
8
+ # When the domain was created in DNSimple
9
+ attr_accessor :created_at
10
+
11
+ # When the domain was last update in DNSimple
12
+ attr_accessor :updated_at
13
+
14
+ # The current known name server status
15
+ attr_accessor :name_server_status
16
+
17
+ # Delete the domain from DNSimple. WARNING: this cannot
18
+ # be undone.
19
+ def delete(options={})
20
+ DNSimple::Client.delete "domains/#{name}", options
21
+ end
22
+ alias :destroy :delete
23
+
24
+ # Apply the given named template to the domain. This will add
25
+ # all of the records in the template to the domain.
26
+ def apply(template, options={})
27
+ options.merge!(:body => {})
28
+ template = resolve_template(template)
29
+
30
+ DNSimple::Client.post "domains/#{name}/templates/#{template.id}/apply",
31
+ options
32
+ end
33
+
34
+ #:nodoc:
35
+ def resolve_template(template)
36
+ case template
37
+ when DNSimple::Template
38
+ template
39
+ else
40
+ DNSimple::Template.find(template)
54
41
  end
42
+ end
55
43
 
56
- def applied_services(options={})
57
- options.merge!(DNSimple::Client.standard_options_with_credentials)
58
- response = self.class.get("#{Client.base_uri}/domains/#{name}/applied_services", options)
59
- pp response if DNSimple::Client.debug?
60
- case response.code
61
- when 200
62
- response.map { |r| DNSimple::Service.new(r["service"]) }
63
- when 401
64
- raise RuntimeError, "Authentication failed"
65
- else
66
- raise RuntimeError, "Error: #{response.code}"
67
- end
44
+ def applied_services(options={})
45
+ response = DNSimple::Client.get "domains/#{name}/applied_services",
46
+ options
47
+
48
+ case response.code
49
+ when 200
50
+ response.map { |r| DNSimple::Service.new(r["service"]) }
51
+ else
52
+ raise RuntimeError, "Error: #{response.code}"
68
53
  end
54
+ end
69
55
 
70
- def available_services(options={})
71
- options.merge!(DNSimple::Client.standard_options_with_credentials)
72
- response = self.class.get("#{DNSimple::Client.base_uri}/domains/#{name}/available_services", options)
73
- pp response if DNSimple::Client.debug?
74
- case response.code
75
- when 200
76
- response.map { |r| DNSimple::Service.new(r["service"]) }
77
- when 401
78
- raise RuntimeError, "Authentication failed"
79
- else
80
- raise RuntimeError, "Error: #{response.code}"
81
- end
56
+ def available_services(options={})
57
+ response = DNSimple::Client.get "domains/#{name}/available_services",
58
+ options
59
+
60
+ case response.code
61
+ when 200
62
+ response.map { |r| DNSimple::Service.new(r["service"]) }
63
+ else
64
+ raise RuntimeError, "Error: #{response.code}"
82
65
  end
66
+ end
83
67
 
84
- def add_service(id_or_short_name, options={})
85
- options.merge!(DNSimple::Client.standard_options_with_credentials)
86
- options.merge!(:body => {:service => {:id => id_or_short_name}})
87
- response = self.class.post("#{DNSimple::Client.base_uri}/domains/#{name}/applied_services", options)
88
- pp response if DNSimple::Client.debug?
89
- case response.code
90
- when 200
91
- true
92
- when 401
93
- raise RuntimeError, "Authentication failed"
94
- else
95
- raise "Error: #{response.code}"
96
- end
68
+ def add_service(id_or_short_name, options={})
69
+ options.merge!(:body => {:service => {:id => id_or_short_name}})
70
+ response = DNSimple::Client.post "domains/#{name}/applied_services",
71
+ options
72
+
73
+ case response.code
74
+ when 200
75
+ true
76
+ else
77
+ raise "Error: #{response.code}"
97
78
  end
79
+ end
98
80
 
99
- def remove_service(id, options={})
100
- options.merge!(DNSimple::Client.standard_options_with_credentials)
101
- response = self.class.delete("#{DNSimple::Client.base_uri}/domains/#{name}/applied_services/#{id}", options)
102
- pp response if DNSimple::Client.debug?
103
- case response.code
104
- when 200
105
- true
106
- when 401
107
- raise RuntimeError, "Authentication failed"
108
- else
109
- raise "Error: #{response.code}"
110
- end
81
+ def remove_service(id, options={})
82
+ response = DNSimple::Client.delete("domains/#{name}/applied_services/#{id}", options)
83
+
84
+ case response.code
85
+ when 200
86
+ true
87
+ else
88
+ raise "Error: #{response.code}"
111
89
  end
90
+ end
112
91
 
113
- # Check the availability of a name
114
- def self.check(name, options={})
115
- options.merge!(DNSimple::Client.standard_options_with_credentials)
116
- response = self.get("#{DNSimple::Client.base_uri}/domains/#{name}/check", options)
117
- pp response if DNSimple::Client.debug?
118
- case response.code
119
- when 200
120
- "registered"
121
- when 401
122
- raise RuntimeError, "Authentication failed"
123
- when 404
124
- "available"
125
- else
126
- raise "Error: #{response.code}"
127
- end
92
+ # Check the availability of a name
93
+ def self.check(name, options={})
94
+ response = DNSimple::Client.get("domains/#{name}/check", options)
95
+
96
+ case response.code
97
+ when 200
98
+ "registered"
99
+ when 404
100
+ "available"
101
+ else
102
+ raise "Error: #{response.code}"
128
103
  end
104
+ end
129
105
 
130
- # Create the domain with the given name in DNSimple. This
131
- # method returns a Domain instance if the name is created
132
- # and raises an error otherwise.
133
- def self.create(name, options={})
134
- options.merge!(DNSimple::Client.standard_options_with_credentials)
135
-
136
- domain_hash = {:name => name}
137
- options.merge!({:body => {:domain => domain_hash}})
138
-
139
- response = self.post("#{DNSimple::Client.base_uri}/domains", options)
140
-
141
- pp response if DNSimple::Client.debug?
142
-
143
- case response.code
144
- when 201
145
- return DNSimple::Domain.new(response["domain"])
146
- when 401
147
- raise RuntimeError, "Authentication failed"
148
- else
149
- raise DNSimple::DomainError.new(name, response["errors"])
150
- end
106
+ # Create the domain with the given name in DNSimple. This
107
+ # method returns a Domain instance if the name is created
108
+ # and raises an error otherwise.
109
+ def self.create(name, options={})
110
+ options.merge!({:body => {:domain => {:name => name}}})
111
+
112
+ response = DNSimple::Client.post('domains', options)
113
+
114
+ case response.code
115
+ when 201
116
+ return new(response["domain"])
117
+ else
118
+ raise DNSimple::DomainError.new(name, response["errors"])
151
119
  end
120
+ end
152
121
 
153
- # Purchase a domain name.
154
- def self.register(name, registrant={}, extended_attributes={}, options={})
155
- options.merge!(DNSimple::Client.standard_options_with_credentials)
156
-
157
- body = {:domain => {:name => name}}
158
- if registrant
159
- if registrant[:id]
160
- body[:domain][:registrant_id] = registrant[:id]
161
- else
162
- body.merge!(:contact => Contact.resolve_attributes(registrant))
163
- end
164
- end
165
- body.merge!(:extended_attribute => extended_attributes)
166
- options.merge!({:body => body})
167
-
168
- response = self.post("#{DNSimple::Client.base_uri}/domain_registrations", options)
169
-
170
- pp response if DNSimple::Client.debug?
171
-
172
- case response.code
173
- when 201
174
- return DNSimple::Domain.new(response["domain"])
175
- when 401
176
- raise RuntimeError, "Authentication failed"
122
+ # Purchase a domain name.
123
+ def self.register(name, registrant={}, extended_attributes={}, options={})
124
+ body = {:domain => {:name => name}}
125
+ if registrant
126
+ if registrant[:id]
127
+ body[:domain][:registrant_id] = registrant[:id]
177
128
  else
178
- raise DNSimple::DomainError.new(name, response["errors"])
129
+ body.merge!(:contact => DNSimple::Contact.resolve_attributes(registrant))
179
130
  end
180
131
  end
132
+ body.merge!(:extended_attribute => extended_attributes)
133
+ options.merge!({:body => body})
181
134
 
182
- # Find a specific domain in the account either by the numeric ID
183
- # or by the fully-qualified domain name.
184
- def self.find(id_or_name, options={})
185
- options.merge!(DNSimple::Client.standard_options_with_credentials)
186
- response = self.get("#{DNSimple::Client.base_uri}/domains/#{id_or_name}", options)
187
-
188
- pp response if DNSimple::Client.debug?
189
-
190
- case response.code
191
- when 200
192
- return DNSimple::Domain.new(response["domain"])
193
- when 401
194
- raise RuntimeError, "Authentication failed"
195
- when 404
196
- raise RuntimeError, "Could not find domain #{id_or_name}"
197
- else
198
- raise DNSimple::DomainError.new(id_or_name, response["errors"])
199
- end
135
+ response = DNSimple::Client.post('domain_registrations', options)
136
+
137
+ case response.code
138
+ when 201
139
+ return DNSimple::Domain.new(response["domain"])
140
+ else
141
+ raise DNSimple::DomainError.new(name, response["errors"])
200
142
  end
143
+ end
201
144
 
202
- # Get all domains for the account.
203
- def self.all(options={})
204
- options.merge!(DNSimple::Client.standard_options_with_credentials)
205
- response = self.get("#{DNSimple::Client.base_uri}/domains", options)
206
-
207
- pp response if DNSimple::Client.debug?
208
-
209
- case response.code
210
- when 200
211
- response.map { |r| DNSimple::Domain.new(r["domain"]) }
212
- when 401
213
- raise RuntimeError, "Authentication failed"
214
- else
215
- raise RuntimeError, "Error: #{response.code}"
216
- end
145
+ # Find a specific domain in the account either by the numeric ID
146
+ # or by the fully-qualified domain name.
147
+ def self.find(id_or_name, options={})
148
+ response = DNSimple::Client.get "domains/#{id_or_name}", options
149
+
150
+ case response.code
151
+ when 200
152
+ return new(response["domain"])
153
+ when 404
154
+ raise RuntimeError, "Could not find domain #{id_or_name}"
155
+ else
156
+ raise DNSimple::Error.new(id_or_name, response["errors"])
217
157
  end
158
+ end
159
+
160
+ # Get all domains for the account.
161
+ def self.all(options={})
162
+ response = DNSimple::Client.get 'domains', options
218
163
 
164
+ case response.code
165
+ when 200
166
+ response.map { |r| new(r["domain"]) }
167
+ else
168
+ raise RuntimeError, "Error: #{response.code}"
169
+ end
219
170
  end
220
171
  end
@@ -1,73 +1,46 @@
1
- module DNSimple #:nodoc:
2
- # Used for domains that require extended attributes.
3
- class ExtendedAttribute
4
- # An option for an extended attribute
5
- class Option
6
- # The option name
7
- attr_accessor :title
1
+ class DNSimple::ExtendedAttribute < DNSimple::Base # Used for domains that require extended attributes.
2
+ # An option for an extended attribute
3
+ class Option < DNSimple::Base
4
+ # The option name
5
+ attr_accessor :title
8
6
 
9
- # The option value
10
- attr_accessor :value
7
+ # The option value
8
+ attr_accessor :value
11
9
 
12
- # A long description of the option
13
- attr_accessor :description
14
-
15
- #:nodoc:
16
- def initialize(attributes)
17
- attributes.each do |key, value|
18
- m = "#{key}=".to_sym
19
- self.send(m, value) if self.respond_to?(m)
20
- end
21
- end
22
- end
23
-
24
- include HTTParty
25
-
26
- # The extended attribute name
27
- attr_accessor :name
28
-
29
- # A description of the extended attribute
10
+ # A long description of the option
30
11
  attr_accessor :description
12
+ end
31
13
 
32
- # Boolean indicating if the extended attribute is required
33
- attr_accessor :required
14
+ # The extended attribute name
15
+ attr_accessor :name
34
16
 
35
- #:nodoc:
36
- def initialize(attributes)
37
- attributes.each do |key, value|
38
- m = "#{key}=".to_sym
39
- self.send(m, value) if self.respond_to?(m)
40
- end
41
- end
17
+ # A description of the extended attribute
18
+ attr_accessor :description
42
19
 
43
- # An array of options for the extended attribute
44
- def options
45
- @options ||= []
46
- end
20
+ # Boolean indicating if the extended attribute is required
21
+ attr_accessor :required
47
22
 
48
- def options=(opts)
49
- @options = []
50
- opts.each do |opt|
51
- @options << Option.new(opt)
52
- end
53
- end
23
+ # An array of options for the extended attribute
24
+ def options
25
+ @options ||= []
26
+ end
54
27
 
55
- # Find the extended attributes for the given TLD
56
- def self.find(tld, options={})
57
- options.merge!({:basic_auth => Client.credentials})
58
-
59
- response = self.get("#{Client.base_uri}/extended_attributes/#{tld}", options)
28
+ def options=(opts)
29
+ @options = []
30
+ opts.each do |opt|
31
+ @options << DNSimple::ExtendedAttribute::Option.new(opt)
32
+ end
33
+ end
60
34
 
61
- pp response if Client.debug?
35
+ # Find the extended attributes for the given TLD
36
+ def self.find(tld, options={})
37
+ response = DNSimple::Client.get "extended_attributes/#{tld}.json", options
62
38
 
63
- case response.code
64
- when 200
65
- response.map { |r| ExtendedAttribute.new(r) }
66
- when 401
67
- raise RuntimeError, "Authentication failed"
68
- else
69
- raise RuntimeError, "Error: #{response.code}"
70
- end
39
+ case response.code
40
+ when 200
41
+ response.map { |r| new(r) }
42
+ else
43
+ raise RuntimeError, "Error: #{response.code}"
71
44
  end
72
45
  end
73
46
  end