dnsimple-ruby 1.3.2 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +9 -2
  3. data/.rspec +1 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +7 -0
  7. data/CHANGELOG.md +20 -0
  8. data/Gemfile +0 -2
  9. data/LICENSE +2 -2
  10. data/README.md +121 -0
  11. data/Rakefile +30 -14
  12. data/bin/dnsimple.rb +1 -1
  13. data/dnsimple-ruby.gemspec +9 -8
  14. data/features/README +1 -1
  15. data/features/step_definitions/cli_steps.rb +1 -1
  16. data/features/support/env.rb +4 -4
  17. data/fixtures/vcr_cassettes/DNSimple_Certificate/_all.yml +45 -32
  18. data/fixtures/vcr_cassettes/DNSimple_Certificate/_purchase.yml +45 -32
  19. data/fixtures/vcr_cassettes/DNSimple_Certificate/_submit.yml +45 -32
  20. data/fixtures/vcr_cassettes/DNSimple_Contact/a_new_contact.yml +39 -31
  21. data/fixtures/vcr_cassettes/DNSimple_Contact/an_existing_contact.yml +39 -31
  22. data/fixtures/vcr_cassettes/DNSimple_Domain/_all.yml +113 -94
  23. data/fixtures/vcr_cassettes/DNSimple_Domain/applying_templates.yml +186 -156
  24. data/fixtures/vcr_cassettes/DNSimple_Domain/creating_a_new_domain.yml +39 -32
  25. data/fixtures/vcr_cassettes/DNSimple_Domain/finding_an_existing_domain/by_id.yml +39 -32
  26. data/fixtures/vcr_cassettes/DNSimple_Domain/finding_an_existing_domain/by_name.yml +39 -32
  27. data/fixtures/vcr_cassettes/DNSimple_Domain/registration/with_a_new_registrant_contact.yml +40 -32
  28. data/fixtures/vcr_cassettes/DNSimple_Domain/registration/with_an_existing_contact.yml +40 -32
  29. data/fixtures/vcr_cassettes/DNSimple_ExtendedAttribute/list_extended_attributes/for_ca.yml +262 -29
  30. data/fixtures/vcr_cassettes/DNSimple_ExtendedAttribute/list_extended_attributes/for_com.yml +35 -29
  31. data/fixtures/vcr_cassettes/DNSimple_Record/_all.yml +147 -125
  32. data/fixtures/vcr_cassettes/DNSimple_Record/creating_a_new_record.yml +39 -32
  33. data/fixtures/vcr_cassettes/DNSimple_Record/find_a_record.yml +39 -32
  34. data/fixtures/vcr_cassettes/DNSimple_Template/a_template.yml +41 -32
  35. data/fixtures/vcr_cassettes/DNSimple_User/_me.yml +38 -31
  36. data/lib/dnsimple.rb +14 -1
  37. data/lib/dnsimple/base.rb +7 -5
  38. data/lib/dnsimple/certificate.rb +100 -95
  39. data/lib/dnsimple/cli.rb +92 -93
  40. data/lib/dnsimple/client.rb +109 -99
  41. data/lib/dnsimple/commands/{describe_certificate.rb → certificate_describe.rb} +2 -2
  42. data/lib/dnsimple/commands/{list_certificates.rb → certificate_list.rb} +2 -2
  43. data/lib/dnsimple/commands/{purchase_certificate.rb → certificate_purchase.rb} +3 -5
  44. data/lib/dnsimple/commands/{submit_certificate.rb → certificate_submit.rb} +3 -5
  45. data/lib/dnsimple/commands/{create_contact.rb → contact_create.rb} +8 -6
  46. data/lib/dnsimple/commands/{delete_contact.rb → contact_delete.rb} +2 -2
  47. data/lib/dnsimple/commands/{describe_contact.rb → contact_describe.rb} +2 -2
  48. data/lib/dnsimple/commands/{list_contacts.rb → contact_list.rb} +2 -2
  49. data/lib/dnsimple/commands/{update_contact.rb → contact_update.rb} +2 -2
  50. data/lib/dnsimple/commands/{apply_template.rb → domain_apply_template.rb} +2 -2
  51. data/lib/dnsimple/commands/{check_domain.rb → domain_check.rb} +2 -2
  52. data/lib/dnsimple/commands/{clear_domain.rb → domain_clear.rb} +2 -2
  53. data/lib/dnsimple/commands/{create_domain.rb → domain_create.rb} +2 -2
  54. data/lib/dnsimple/commands/{delete_domain.rb → domain_delete.rb} +2 -2
  55. data/lib/dnsimple/commands/{describe_domain.rb → domain_describe.rb} +2 -2
  56. data/lib/dnsimple/commands/{list_domains.rb → domain_list.rb} +2 -2
  57. data/lib/dnsimple/commands/{register_domain.rb → domain_register.rb} +2 -2
  58. data/lib/dnsimple/commands/{transfer_domain.rb → domain_transfer.rb} +2 -2
  59. data/lib/dnsimple/commands/{list_extended_attributes.rb → extended_attribute_list.rb} +2 -2
  60. data/lib/dnsimple/commands/{describe_user.rb → me.rb} +2 -2
  61. data/lib/dnsimple/commands/{create_record.rb → record_create.rb} +5 -7
  62. data/lib/dnsimple/commands/{delete_record.rb → record_delete.rb} +2 -2
  63. data/lib/dnsimple/commands/{describe_record.rb → record_describe.rb} +2 -2
  64. data/lib/dnsimple/commands/{list_records.rb → record_list.rb} +4 -6
  65. data/lib/dnsimple/commands/{update_record.rb → record_update.rb} +2 -2
  66. data/lib/dnsimple/commands/{add_service.rb → service_add.rb} +3 -5
  67. data/lib/dnsimple/commands/{describe_service.rb → service_describe.rb} +2 -2
  68. data/lib/dnsimple/commands/{list_services.rb → service_list.rb} +2 -2
  69. data/lib/dnsimple/commands/{list_applied_services.rb → service_list_applied.rb} +2 -2
  70. data/lib/dnsimple/commands/{list_available_services.rb → service_list_available.rb} +2 -2
  71. data/lib/dnsimple/commands/{remove_service.rb → service_remove.rb} +2 -2
  72. data/lib/dnsimple/commands/{create_template.rb → template_create.rb} +2 -2
  73. data/lib/dnsimple/commands/{delete_template.rb → template_delete.rb} +2 -2
  74. data/lib/dnsimple/commands/{add_template_record.rb → template_record_create.rb} +2 -2
  75. data/lib/dnsimple/commands/{delete_template_record.rb → template_record_delete.rb} +2 -2
  76. data/lib/dnsimple/commands/{list_template_records.rb → template_record_list.rb} +2 -2
  77. data/lib/dnsimple/commands/{list_templates.rb → templates_list.rb} +2 -2
  78. data/lib/dnsimple/contact.rb +115 -110
  79. data/lib/dnsimple/domain.rb +134 -132
  80. data/lib/dnsimple/error.rb +8 -19
  81. data/lib/dnsimple/extended_attribute.rb +39 -33
  82. data/lib/dnsimple/record.rb +71 -67
  83. data/lib/dnsimple/service.rb +31 -24
  84. data/lib/dnsimple/template.rb +52 -46
  85. data/lib/dnsimple/template_record.rb +61 -53
  86. data/lib/dnsimple/transfer_order.rb +24 -18
  87. data/lib/dnsimple/user.rb +22 -18
  88. data/lib/dnsimple/version.rb +1 -1
  89. data/spec/README +1 -1
  90. data/spec/ci/.dnsimple.test +3 -0
  91. data/spec/commands/{purchase_certificate_spec.rb → certificate_purchase_spec.rb} +3 -4
  92. data/spec/commands/{submit_certificate_spec.rb → certificate_submit_spec.rb} +3 -4
  93. data/spec/commands/{create_record_spec.rb → record_create_spec.rb} +4 -4
  94. data/spec/commands/{list_records_spec.rb → records_list_spec.rb} +2 -13
  95. data/spec/commands/{add_service_spec.rb → service_add_spec.rb} +3 -12
  96. data/spec/{certificate_spec.rb → dnsimple/certificate_spec.rb} +0 -0
  97. data/spec/dnsimple/client_spec.rb +45 -18
  98. data/spec/{contact_spec.rb → dnsimple/contact_spec.rb} +3 -1
  99. data/spec/{domain_spec.rb → dnsimple/domain_spec.rb} +2 -0
  100. data/spec/{extended_attributes_spec.rb → dnsimple/extended_attributes_spec.rb} +4 -0
  101. data/spec/{record_spec.rb → dnsimple/record_spec.rb} +2 -0
  102. data/spec/{template_spec.rb → dnsimple/template_spec.rb} +0 -0
  103. data/spec/{user_spec.rb → dnsimple/user_spec.rb} +2 -0
  104. data/spec/spec_helper.rb +24 -10
  105. metadata +96 -99
  106. data/Gemfile.lock +0 -71
  107. data/README +0 -108
  108. data/README.rdoc +0 -108
  109. data/README.textile +0 -138
  110. data/lib/dnsimple/command.rb +0 -10
  111. data/spec/command_spec.rb +0 -19
@@ -1,7 +1,7 @@
1
1
  module DNSimple
2
2
  module Commands
3
- class ListTemplateRecords
4
- def execute(args, options={})
3
+ class TemplateRecordList
4
+ def execute(args, options = {})
5
5
  short_name = args.shift
6
6
  template_records = TemplateRecord.all(short_name)
7
7
  puts "Found #{template_records.length} records for #{short_name}"
@@ -1,7 +1,7 @@
1
1
  module DNSimple
2
2
  module Commands
3
- class ListTemplates
4
- def execute(args, options={})
3
+ class TemplateList
4
+ def execute(args, options = {})
5
5
  templates = Template.all
6
6
  puts "Found #{templates.length} templates:"
7
7
  templates.each do |template|
@@ -1,148 +1,153 @@
1
- class DNSimple::Contact < DNSimple::Base # Class representing a contact in DNSimple
1
+ module DNSimple
2
2
 
3
- Aliases = {
4
- 'first' => 'first_name',
5
- 'last' => 'last_name',
6
- 'state' => 'state_province',
7
- 'province' => 'state_province',
8
- 'state_or_province' => 'state_province',
9
- 'email' => 'email_address',
10
- }
3
+ # Represents a contact.
4
+ class Contact < Base
11
5
 
12
- # The contact ID in DNSimple
13
- attr_accessor :id
6
+ Aliases = {
7
+ 'first' => 'first_name',
8
+ 'last' => 'last_name',
9
+ 'state' => 'state_province',
10
+ 'province' => 'state_province',
11
+ 'state_or_province' => 'state_province',
12
+ 'email' => 'email_address',
13
+ }
14
14
 
15
- # The name of the organization in which the contact works
16
- # (may be omitted)
17
- attr_accessor :organization_name
15
+ # The contact ID in DNSimple
16
+ attr_accessor :id
18
17
 
19
- # The contact first name
20
- attr_accessor :first_name
18
+ # The name of the organization in which the contact works
19
+ # (may be omitted)
20
+ attr_accessor :organization_name
21
21
 
22
- # The contact last name
23
- attr_accessor :last_name
22
+ # The contact first name
23
+ attr_accessor :first_name
24
24
 
25
- # The contact's job title (may be omitted)
26
- attr_accessor :job_title
25
+ # The contact last name
26
+ attr_accessor :last_name
27
27
 
28
- # The contact street address
29
- attr_accessor :address1
28
+ # The contact's job title (may be omitted)
29
+ attr_accessor :job_title
30
30
 
31
- # Apartment or suite number
32
- attr_accessor :address2
31
+ # The contact street address
32
+ attr_accessor :address1
33
33
 
34
- # The city name
35
- attr_accessor :city
34
+ # Apartment or suite number
35
+ attr_accessor :address2
36
36
 
37
- # The state or province name
38
- attr_accessor :state_province
37
+ # The city name
38
+ attr_accessor :city
39
39
 
40
- # The contact postal code
41
- attr_accessor :postal_code
40
+ # The state or province name
41
+ attr_accessor :state_province
42
42
 
43
- # The contact country (as a 2-character country code)
44
- attr_accessor :country
43
+ # The contact postal code
44
+ attr_accessor :postal_code
45
45
 
46
- # The contact email address
47
- attr_accessor :email_address
46
+ # The contact country (as a 2-character country code)
47
+ attr_accessor :country
48
48
 
49
- # The contact phone number
50
- attr_accessor :phone
49
+ # The contact email address
50
+ attr_accessor :email_address
51
51
 
52
- # The contact phone extension (may be omitted)
53
- attr_accessor :phone_ext
52
+ # The contact phone number
53
+ attr_accessor :phone
54
54
 
55
- # The contact fax number (may be omitted)
56
- attr_accessor :fax
55
+ # The contact phone extension (may be omitted)
56
+ attr_accessor :phone_ext
57
57
 
58
- # When the contact was created in DNSimple
59
- attr_accessor :created_at
58
+ # The contact fax number (may be omitted)
59
+ attr_accessor :fax
60
60
 
61
- # When the contact was last updated in DNSimple
62
- attr_accessor :updated_at
61
+ # When the contact was created in DNSimple
62
+ attr_accessor :created_at
63
63
 
64
- def name
65
- [first_name, last_name].join(' ')
66
- end
64
+ # When the contact was last updated in DNSimple
65
+ attr_accessor :updated_at
67
66
 
68
- def save(options={})
69
- contact_hash = {}
70
- %w(first_name last_name organization_name job_title address1 address2 city
71
- state_province postal_code country email_address phone phone_ext fax).each do |attribute|
72
- contact_hash[DNSimple::Contact.resolve(attribute)] = self.send(attribute)
67
+ def name
68
+ [first_name, last_name].join(' ')
73
69
  end
74
70
 
75
- options.merge!({:body => {:contact => contact_hash}})
76
-
77
- response = DNSimple::Client.put("contacts/#{id}", options)
71
+ def save(options={})
72
+ contact_hash = {}
73
+ %w(first_name last_name organization_name job_title address1 address2 city
74
+ state_province postal_code country email_address phone phone_ext fax).each do |attribute|
75
+ contact_hash[DNSimple::Contact.resolve(attribute)] = self.send(attribute)
76
+ end
78
77
 
79
- case response.code
80
- when 200
81
- return self
82
- else
83
- raise RuntimeError, "Failed to update contact: #{response.inspect}"
84
- end
85
- end
78
+ options.merge!({:body => {:contact => contact_hash}})
86
79
 
87
- # Delete the contact from DNSimple. WARNING: this cannot be undone.
88
- def delete(options={})
89
- DNSimple::Client.delete("contacts/#{id}.json", options)
90
- end
91
- alias :destroy :delete
92
-
93
- # Map an aliased field name to it's real name. For example, if you
94
- # pass "first" it will be resolved to "first_name", "email" is resolved
95
- # to "email_address" and so on.
96
- def self.resolve(name)
97
- DNSimple::Contact::Aliases[name.to_s] || name
98
- end
80
+ response = DNSimple::Client.put("contacts/#{id}", options)
99
81
 
100
- def self.resolve_attributes(attributes)
101
- resolved_attributes = {}
102
- attributes.each do |k, v|
103
- resolved_attributes[resolve(k)] = v
82
+ case response.code
83
+ when 200
84
+ return self
85
+ else
86
+ raise RequestError.new("Error updating contact", response)
87
+ end
104
88
  end
105
- resolved_attributes
106
- end
107
89
 
108
- # Create the contact with the given attributes in DNSimple.
109
- # This method returns a Contact instance of the contact is created
110
- # and raises an error otherwise.
111
- def self.create(attributes, options={})
112
- contact_hash = resolve_attributes(attributes)
90
+ # Delete the contact from DNSimple. WARNING: this cannot be undone.
91
+ def delete(options={})
92
+ DNSimple::Client.delete("contacts/#{id}", options)
93
+ end
94
+ alias :destroy :delete
113
95
 
114
- options.merge!({:body => {:contact => contact_hash}})
115
- response = DNSimple::Client.post 'contacts.json', options
96
+ # Map an aliased field name to it's real name. For example, if you
97
+ # pass "first" it will be resolved to "first_name", "email" is resolved
98
+ # to "email_address" and so on.
99
+ def self.resolve(name)
100
+ DNSimple::Contact::Aliases[name.to_s] || name
101
+ end
116
102
 
117
- case response.code
118
- when 201
119
- return new(response["contact"])
120
- else
121
- raise RuntimeError, "Failed to create contact: #{response.inspect}"
103
+ def self.resolve_attributes(attributes)
104
+ resolved_attributes = {}
105
+ attributes.each do |k, v|
106
+ resolved_attributes[resolve(k)] = v
107
+ end
108
+ resolved_attributes
122
109
  end
123
- end
124
110
 
125
- def self.find(id, options={})
126
- response = DNSimple::Client.get "contacts/#{id}.json", options
111
+ # Create the contact with the given attributes in DNSimple.
112
+ # This method returns a Contact instance of the contact is created
113
+ # and raises an error otherwise.
114
+ def self.create(attributes, options={})
115
+ contact_hash = resolve_attributes(attributes)
116
+
117
+ options.merge!({:body => {:contact => contact_hash}})
118
+ response = DNSimple::Client.post("contacts", options)
119
+
120
+ case response.code
121
+ when 201
122
+ new(response["contact"])
123
+ else
124
+ raise RequestError.new("Error creating contact", response)
125
+ end
126
+ end
127
127
 
128
- case response.code
129
- when 200
130
- return new(response["contact"])
131
- when 404
132
- raise RuntimeError, "Could not find contact #{id}"
133
- else
134
- raise DNSimple::Error.new(id, response["errors"])
128
+ def self.find(id, options={})
129
+ response = DNSimple::Client.get("contacts/#{id}", options)
130
+
131
+ case response.code
132
+ when 200
133
+ new(response["contact"])
134
+ when 404
135
+ raise RecordNotFound, "Could not find contact #{id}"
136
+ else
137
+ raise RequestError.new("Error finding contact", response)
138
+ end
135
139
  end
136
- end
137
140
 
138
- def self.all(options={})
139
- response = DNSimple::Client.get 'contacts.json', options
141
+ def self.all(options={})
142
+ response = DNSimple::Client.get("contacts", options)
140
143
 
141
- case response.code
142
- when 200
143
- response.map { |r| new(r["contact"]) }
144
- else
145
- raise RuntimeError, "Error: #{response.code}"
144
+ case response.code
145
+ when 200
146
+ response.map { |r| new(r["contact"]) }
147
+ else
148
+ raise RequestError.new("Error listing contacts", response)
149
+ end
146
150
  end
151
+
147
152
  end
148
153
  end
@@ -1,171 +1,173 @@
1
- class DNSimple::Domain < DNSimple::Base # Class representing a single domain in DNSimple.
2
- # The domain ID in DNSimple
3
- attr_accessor :id
1
+ module DNSimple
4
2
 
5
- # The domain name
6
- attr_accessor :name
3
+ # Represents a single domain.
4
+ class Domain < Base
5
+ # The domain ID in DNSimple
6
+ attr_accessor :id
7
7
 
8
- # When the domain was created in DNSimple
9
- attr_accessor :created_at
8
+ # The domain name
9
+ attr_accessor :name
10
10
 
11
- # When the domain was last update in DNSimple
12
- attr_accessor :updated_at
11
+ # When the domain was created in DNSimple
12
+ attr_accessor :created_at
13
13
 
14
- # The current known name server status
15
- attr_accessor :name_server_status
14
+ # When the domain was last update in DNSimple
15
+ attr_accessor :updated_at
16
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
17
+ # The current known name server status
18
+ attr_accessor :name_server_status
23
19
 
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)
20
+ # Delete the domain from DNSimple. WARNING: this cannot
21
+ # be undone.
22
+ def delete(options={})
23
+ DNSimple::Client.delete("domains/#{name}", options)
24
+ end
25
+ alias :destroy :delete
29
26
 
30
- DNSimple::Client.post "domains/#{name}/templates/#{template.id}/apply",
31
- options
32
- end
27
+ # Apply the given named template to the domain. This will add
28
+ # all of the records in the template to the domain.
29
+ def apply(template, options={})
30
+ options.merge!(:body => {})
31
+ template = resolve_template(template)
33
32
 
34
- #:nodoc:
35
- def resolve_template(template)
36
- case template
37
- when DNSimple::Template
38
- template
39
- else
40
- DNSimple::Template.find(template)
33
+ DNSimple::Client.post("domains/#{name}/templates/#{template.id}/apply", options)
41
34
  end
42
- end
43
-
44
- def applied_services(options={})
45
- response = DNSimple::Client.get "domains/#{name}/applied_services",
46
- options
47
35
 
48
- case response.code
49
- when 200
50
- response.map { |r| DNSimple::Service.new(r["service"]) }
51
- else
52
- raise RuntimeError, "Error: #{response.code}"
36
+ #:nodoc:
37
+ def resolve_template(template)
38
+ case template
39
+ when DNSimple::Template
40
+ template
41
+ else
42
+ DNSimple::Template.find(template)
43
+ end
53
44
  end
54
- end
55
45
 
56
- def available_services(options={})
57
- response = DNSimple::Client.get "domains/#{name}/available_services",
58
- options
46
+ def applied_services(options={})
47
+ response = DNSimple::Client.get("domains/#{name}/applied_services", options)
59
48
 
60
- case response.code
61
- when 200
62
- response.map { |r| DNSimple::Service.new(r["service"]) }
63
- else
64
- raise RuntimeError, "Error: #{response.code}"
49
+ case response.code
50
+ when 200
51
+ response.map { |r| DNSimple::Service.new(r["service"]) }
52
+ else
53
+ raise RequestError.new("Error listing applied services", response)
54
+ end
65
55
  end
66
- end
67
56
 
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
57
+ def available_services(options={})
58
+ response = DNSimple::Client.get("domains/#{name}/available_services", options)
72
59
 
73
- case response.code
74
- when 200
75
- true
76
- else
77
- raise "Error: #{response.code}"
60
+ case response.code
61
+ when 200
62
+ response.map { |r| DNSimple::Service.new(r["service"]) }
63
+ else
64
+ raise RequestError.new("Error listing available services", response)
65
+ end
78
66
  end
79
- end
80
67
 
81
- def remove_service(id, options={})
82
- response = DNSimple::Client.delete("domains/#{name}/applied_services/#{id}", options)
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", options)
83
71
 
84
- case response.code
85
- when 200
86
- true
87
- else
88
- raise "Error: #{response.code}"
72
+ case response.code
73
+ when 200
74
+ true
75
+ else
76
+ raise RequestError.new("Error adding service", response)
77
+ end
89
78
  end
90
- end
91
79
 
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}"
103
- end
104
- end
80
+ def remove_service(id, options={})
81
+ response = DNSimple::Client.delete("domains/#{name}/applied_services/#{id}", options)
105
82
 
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}}})
83
+ case response.code
84
+ when 200
85
+ true
86
+ else
87
+ raise RequestError.new("Error removing service", response)
88
+ end
89
+ end
111
90
 
112
- response = DNSimple::Client.post('domains', options)
91
+ # Check the availability of a name
92
+ def self.check(name, options={})
93
+ response = DNSimple::Client.get("domains/#{name}/check", options)
113
94
 
114
- case response.code
115
- when 201
116
- return new(response["domain"])
117
- else
118
- raise DNSimple::DomainError.new(name, response["errors"])
95
+ case response.code
96
+ when 200
97
+ "registered"
98
+ when 404
99
+ "available"
100
+ else
101
+ raise RequestError.new("Error checking availability", response)
102
+ end
119
103
  end
120
- end
121
104
 
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]
105
+ # Create the domain with the given name in DNSimple. This
106
+ # method returns a Domain instance if the name is created
107
+ # and raises an error otherwise.
108
+ def self.create(name, options={})
109
+ options.merge!({:body => {:domain => {:name => name}}})
110
+
111
+ response = DNSimple::Client.post("domains", options)
112
+
113
+ case response.code
114
+ when 201
115
+ new(response["domain"])
128
116
  else
129
- body.merge!(:contact => DNSimple::Contact.resolve_attributes(registrant))
117
+ raise RequestError.new("Error creating domain", response)
130
118
  end
131
119
  end
132
- body.merge!(:extended_attribute => extended_attributes)
133
- options.merge!({:body => body})
134
120
 
135
- response = DNSimple::Client.post('domain_registrations', options)
121
+ # Purchase a domain name.
122
+ def self.register(name, registrant={}, extended_attributes={}, options={})
123
+ body = {:domain => {:name => name}}
124
+ if registrant
125
+ if registrant[:id]
126
+ body[:domain][:registrant_id] = registrant[:id]
127
+ else
128
+ body.merge!(:contact => DNSimple::Contact.resolve_attributes(registrant))
129
+ end
130
+ end
131
+ body.merge!(:extended_attribute => extended_attributes)
132
+ options.merge!({:body => body})
133
+
134
+ response = DNSimple::Client.post("domain_registrations", options)
136
135
 
137
- case response.code
138
- when 201
139
- return DNSimple::Domain.new(response["domain"])
140
- else
141
- raise DNSimple::DomainError.new(name, response["errors"])
136
+ case response.code
137
+ when 201
138
+ return DNSimple::Domain.new(response["domain"])
139
+ else
140
+ raise RequestError.new("Error registering domain", response)
141
+ end
142
142
  end
143
- end
144
143
 
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"])
144
+ # Find a specific domain in the account either by the numeric ID
145
+ # or by the fully-qualified domain name.
146
+ def self.find(id_or_name, options={})
147
+ id = id_or_name
148
+ response = DNSimple::Client.get("domains/#{id}", options)
149
+
150
+ case response.code
151
+ when 200
152
+ new(response["domain"])
153
+ when 404
154
+ raise RecordNotFound, "Could not find domain #{id}"
155
+ else
156
+ raise RequestError.new("Error finding domain", response)
157
+ end
157
158
  end
158
- end
159
159
 
160
- # Get all domains for the account.
161
- def self.all(options={})
162
- response = DNSimple::Client.get 'domains', options
160
+ # Get all domains for the account.
161
+ def self.all(options={})
162
+ response = DNSimple::Client.get("domains", options)
163
163
 
164
- case response.code
165
- when 200
166
- response.map { |r| new(r["domain"]) }
167
- else
168
- raise RuntimeError, "Error: #{response.code}"
164
+ case response.code
165
+ when 200
166
+ response.map { |r| new(r["domain"]) }
167
+ else
168
+ raise RequestError.new("Error listing domains", response)
169
+ end
169
170
  end
171
+
170
172
  end
171
173
  end