sparqcode-dnsimple-ruby 1.2.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. data/.bundle/config +3 -0
  2. data/.rvmrc +1 -0
  3. data/Gemfile +16 -0
  4. data/Gemfile.lock +71 -0
  5. data/LICENSE +22 -0
  6. data/README +125 -0
  7. data/README.rdoc +125 -0
  8. data/README.textile +161 -0
  9. data/Rakefile +33 -0
  10. data/VERSION +1 -0
  11. data/bin/dnsimple +4 -0
  12. data/bin/dnsimple.rb +153 -0
  13. data/dnsimple-ruby.gemspec +191 -0
  14. data/features/README +12 -0
  15. data/features/cli/certificates/purchase_certificate.feature +10 -0
  16. data/features/cli/contacts/create_contact.feature +10 -0
  17. data/features/cli/domains/check_domain.feature +10 -0
  18. data/features/cli/domains/create_domain.feature +10 -0
  19. data/features/cli/domains/delete_domain.feature +11 -0
  20. data/features/cli/domains/register_domain.feature +10 -0
  21. data/features/cli/records/create_ptr_record.feature +11 -0
  22. data/features/cli/records/create_record.feature +12 -0
  23. data/features/cli/records/delete_record.feature +14 -0
  24. data/features/cli/templates/apply_template.feature +11 -0
  25. data/features/step_definitions/certificate_steps.rb +3 -0
  26. data/features/step_definitions/cli_steps.rb +8 -0
  27. data/features/step_definitions/domain_steps.rb +32 -0
  28. data/features/step_definitions/record_steps.rb +38 -0
  29. data/features/step_definitions/template_steps.rb +9 -0
  30. data/features/support/env.rb +17 -0
  31. data/fixtures/vcr_cassettes/DNSimple_Certificate/_all.yml +44 -0
  32. data/fixtures/vcr_cassettes/DNSimple_Certificate/_purchase.yml +44 -0
  33. data/fixtures/vcr_cassettes/DNSimple_Certificate/_submit.yml +44 -0
  34. data/fixtures/vcr_cassettes/DNSimple_Contact/a_new_contact.yml +42 -0
  35. data/fixtures/vcr_cassettes/DNSimple_Contact/an_existing_contact.yml +42 -0
  36. data/fixtures/vcr_cassettes/DNSimple_Domain/_all.yml +130 -0
  37. data/fixtures/vcr_cassettes/DNSimple_Domain/applying_templates.yml +216 -0
  38. data/fixtures/vcr_cassettes/DNSimple_Domain/creating_a_new_domain.yml +44 -0
  39. data/fixtures/vcr_cassettes/DNSimple_Domain/finding_an_existing_domain/by_id.yml +44 -0
  40. data/fixtures/vcr_cassettes/DNSimple_Domain/finding_an_existing_domain/by_name.yml +44 -0
  41. data/fixtures/vcr_cassettes/DNSimple_Domain/registration/with_a_new_registrant_contact.yml +44 -0
  42. data/fixtures/vcr_cassettes/DNSimple_Domain/registration/with_an_existing_contact.yml +44 -0
  43. data/fixtures/vcr_cassettes/DNSimple_Domain/setting_name_servers.yml +30 -0
  44. data/fixtures/vcr_cassettes/DNSimple_ExtendedAttribute/list_extended_attributes/for_ca.yml +40 -0
  45. data/fixtures/vcr_cassettes/DNSimple_ExtendedAttribute/list_extended_attributes/for_com.yml +40 -0
  46. data/fixtures/vcr_cassettes/DNSimple_Record/_all.yml +173 -0
  47. data/fixtures/vcr_cassettes/DNSimple_Record/creating_a_new_record.yml +44 -0
  48. data/fixtures/vcr_cassettes/DNSimple_Record/find_a_record.yml +44 -0
  49. data/fixtures/vcr_cassettes/DNSimple_Template/a_template.yml +44 -0
  50. data/fixtures/vcr_cassettes/DNSimple_User/_me.yml +42 -0
  51. data/lib/dnsimple.rb +19 -0
  52. data/lib/dnsimple/certificate.rb +158 -0
  53. data/lib/dnsimple/cli.rb +122 -0
  54. data/lib/dnsimple/client.rb +74 -0
  55. data/lib/dnsimple/command.rb +10 -0
  56. data/lib/dnsimple/commands/add_service.rb +16 -0
  57. data/lib/dnsimple/commands/add_template_record.rb +18 -0
  58. data/lib/dnsimple/commands/apply_template.rb +15 -0
  59. data/lib/dnsimple/commands/check_domain.rb +11 -0
  60. data/lib/dnsimple/commands/clear_domain.rb +16 -0
  61. data/lib/dnsimple/commands/create_contact.rb +21 -0
  62. data/lib/dnsimple/commands/create_domain.rb +16 -0
  63. data/lib/dnsimple/commands/create_record.rb +20 -0
  64. data/lib/dnsimple/commands/create_template.rb +14 -0
  65. data/lib/dnsimple/commands/delete_contact.rb +14 -0
  66. data/lib/dnsimple/commands/delete_domain.rb +14 -0
  67. data/lib/dnsimple/commands/delete_record.rb +16 -0
  68. data/lib/dnsimple/commands/delete_template.rb +13 -0
  69. data/lib/dnsimple/commands/delete_template_record.rb +16 -0
  70. data/lib/dnsimple/commands/describe_certificate.rb +34 -0
  71. data/lib/dnsimple/commands/describe_contact.rb +25 -0
  72. data/lib/dnsimple/commands/describe_domain.rb +14 -0
  73. data/lib/dnsimple/commands/describe_record.rb +16 -0
  74. data/lib/dnsimple/commands/describe_service.rb +12 -0
  75. data/lib/dnsimple/commands/describe_user.rb +18 -0
  76. data/lib/dnsimple/commands/list_applied_services.rb +16 -0
  77. data/lib/dnsimple/commands/list_available_services.rb +16 -0
  78. data/lib/dnsimple/commands/list_certificates.rb +15 -0
  79. data/lib/dnsimple/commands/list_contacts.rb +13 -0
  80. data/lib/dnsimple/commands/list_domains.rb +13 -0
  81. data/lib/dnsimple/commands/list_extended_attributes.rb +25 -0
  82. data/lib/dnsimple/commands/list_records.rb +21 -0
  83. data/lib/dnsimple/commands/list_services.rb +14 -0
  84. data/lib/dnsimple/commands/list_template_records.rb +17 -0
  85. data/lib/dnsimple/commands/list_templates.rb +13 -0
  86. data/lib/dnsimple/commands/purchase_certificate.rb +19 -0
  87. data/lib/dnsimple/commands/register_domain.rb +33 -0
  88. data/lib/dnsimple/commands/remove_service.rb +14 -0
  89. data/lib/dnsimple/commands/submit_certificate.rb +19 -0
  90. data/lib/dnsimple/commands/transfer_domain.rb +21 -0
  91. data/lib/dnsimple/commands/update_contact.rb +25 -0
  92. data/lib/dnsimple/commands/update_record.rb +23 -0
  93. data/lib/dnsimple/contact.rb +181 -0
  94. data/lib/dnsimple/domain.rb +246 -0
  95. data/lib/dnsimple/error.rb +32 -0
  96. data/lib/dnsimple/extended_attribute.rb +73 -0
  97. data/lib/dnsimple/record.rb +128 -0
  98. data/lib/dnsimple/service.rb +58 -0
  99. data/lib/dnsimple/template.rb +91 -0
  100. data/lib/dnsimple/template_record.rb +103 -0
  101. data/lib/dnsimple/transfer_order.rb +45 -0
  102. data/lib/dnsimple/user.rb +44 -0
  103. data/spec/README +10 -0
  104. data/spec/certificate_spec.rb +46 -0
  105. data/spec/command_spec.rb +19 -0
  106. data/spec/commands/add_service_spec.rb +29 -0
  107. data/spec/commands/create_record_spec.rb +23 -0
  108. data/spec/commands/list_records_spec.rb +34 -0
  109. data/spec/commands/purchase_certificate_spec.rb +24 -0
  110. data/spec/commands/submit_certificate_spec.rb +19 -0
  111. data/spec/contact_spec.rb +35 -0
  112. data/spec/domain_spec.rb +97 -0
  113. data/spec/extended_attributes_spec.rb +19 -0
  114. data/spec/record_spec.rb +59 -0
  115. data/spec/spec_helper.rb +22 -0
  116. data/spec/template_spec.rb +11 -0
  117. data/spec/user_spec.rb +23 -0
  118. metadata +353 -0
@@ -0,0 +1,13 @@
1
+ module DNSimple
2
+ module Commands
3
+ class ListTemplates
4
+ def execute(args, options={})
5
+ templates = Template.all
6
+ puts "Found #{templates.length} templates:"
7
+ templates.each do |template|
8
+ puts "\t#{template.name} (short_name:#{template.short_name})"
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ require 'dnsimple/command'
2
+
3
+ module DNSimple
4
+ module Commands
5
+ class PurchaseCertificate < Command
6
+ def execute(args, options={})
7
+ domain_name = args.shift
8
+ name = args.shift
9
+ contact_id = args.shift
10
+
11
+ domain = Domain.find(domain_name)
12
+ contact = Contact.find(contact_id)
13
+
14
+ certificate = Certificate.purchase(domain, name, contact)
15
+ say "Purchased certificate for #{certificate.fqdn}"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,33 @@
1
+ module DNSimple
2
+ module Commands
3
+ class RegisterDomain
4
+ def execute(args, options={})
5
+ name = args.shift
6
+ registrant = nil
7
+
8
+ registrant_id_or_attribute = args.shift
9
+ if registrant_id_or_attribute
10
+ if registrant_id_or_attribute =~ /^\d+$/
11
+ registrant = {:id => registrant_id_or_attribute}
12
+ else
13
+ args.unshift(registrant_id_or_attribute)
14
+ end
15
+ end
16
+
17
+ extended_attributes = {}
18
+ args.each do |arg|
19
+ n, v = arg.split(":")
20
+ extended_attributes[n] = v
21
+ end
22
+
23
+ domain = Domain.register(name, registrant, extended_attributes)
24
+ puts "Registered #{domain.name}"
25
+
26
+ if template = options.delete(:template)
27
+ domain.apply(template)
28
+ puts "Applied template #{template} to #{domain.name}"
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,14 @@
1
+ module DNSimple
2
+ module Commands
3
+ class RemoveService
4
+ def execute(args, options={})
5
+ domain_name = args.shift
6
+ domain = Domain.find(domain_name)
7
+ short_name = args.shift
8
+ service = Service.find(short_name)
9
+ domain.remove_service(service.id)
10
+ puts "Removed #{service.name} from #{domain_name}"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ require 'dnsimple/command'
2
+
3
+ module DNSimple
4
+ module Commands
5
+ class SubmitCertificate < Command
6
+ def execute(args, options={})
7
+ domain_name = args.shift
8
+ certificate_id = args.shift
9
+ approver_email = args.shift
10
+
11
+ domain = DNSimple::Domain.find(domain_name)
12
+ certificate = DNSimple::Certificate.find(domain, certificate_id)
13
+ certificate.submit(approver_email)
14
+
15
+ say "Certificate submitted, authorization by email required"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ module DNSimple
2
+ module Commands
3
+ class TransferDomain
4
+ def execute(args, options={})
5
+ name = args.shift
6
+ registrant = {:id => args.shift}
7
+ authinfo = args.shift unless args.empty?
8
+ authinfo ||= ''
9
+
10
+ extended_attributes = {}
11
+ args.each do |arg|
12
+ n, v = arg.split(":")
13
+ extended_attributes[n] = v
14
+ end
15
+
16
+ transfer_order = TransferOrder.create(name, authinfo, registrant, extended_attributes)
17
+ puts "Transfer order issued for #{name}"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ module DNSimple
2
+ module Commands
3
+ class UpdateContact
4
+ # Execute the contact:update command.
5
+ #
6
+ # Args expected:
7
+ # id [name:value name:value ...]
8
+ def execute(args, options={})
9
+ attributes = {}
10
+ id = args.shift
11
+ args.each do |arg|
12
+ name, value = arg.split(":")
13
+ attributes[Contact.resolve(name)] = value
14
+ end
15
+
16
+ contact = Contact.find(id)
17
+ attributes.each do |name, value|
18
+ contact.send("#{name}=", value)
19
+ end
20
+ contact.save
21
+ puts "Updated contact #{contact.name} (id: #{contact.id})"
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ module DNSimple
2
+ module Commands
3
+ class UpdateRecord
4
+ def execute(args, options={})
5
+ attributes = {}
6
+ domain_name = args.shift
7
+ id = args.shift
8
+ args.each do |arg|
9
+ name, value = arg.split(":")
10
+ attributes[Record.resolve(name)] = value
11
+ end
12
+
13
+ domain = Domain.find(domain_name)
14
+ record = Record.find(domain, id)
15
+ attributes.each do |name, value|
16
+ record.send("#{name}=", value)
17
+ end
18
+ record.save
19
+ puts "Updated record #{record.fqdn} (id: #{record.id})"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,181 @@
1
+ module DNSimple #:nodoc:
2
+ # CLass representing a contact in DNSimple
3
+ class Contact
4
+ include HTTParty
5
+
6
+ # The contact ID in DNSimple
7
+ attr_accessor :id
8
+
9
+ # The name of the organization in which the contact works
10
+ # (may be omitted)
11
+ attr_accessor :organization_name
12
+
13
+ # The contact first name
14
+ attr_accessor :first_name
15
+
16
+ # The contact last name
17
+ attr_accessor :last_name
18
+
19
+ # The contact's job title (may be omitted)
20
+ attr_accessor :job_title
21
+
22
+ # The contact street address
23
+ attr_accessor :address1
24
+
25
+ # Apartment or suite number
26
+ attr_accessor :address2
27
+
28
+ # The city name
29
+ attr_accessor :city
30
+
31
+ # The state or province name
32
+ attr_accessor :state_province
33
+
34
+ # The contact postal code
35
+ attr_accessor :postal_code
36
+
37
+ # The contact country (as a 2-character country code)
38
+ attr_accessor :country
39
+
40
+ # The contact email address
41
+ attr_accessor :email_address
42
+
43
+ # The contact phone number
44
+ attr_accessor :phone
45
+
46
+ # The contact phone extension (may be omitted)
47
+ attr_accessor :phone_ext
48
+
49
+ # The contact fax number (may be omitted)
50
+ attr_accessor :fax
51
+
52
+ # When the contact was created in DNSimple
53
+ attr_accessor :created_at
54
+
55
+ # When the contact was last updated in DNSimple
56
+ attr_accessor :updated_at
57
+
58
+ #:nodoc:
59
+ def initialize(attributes)
60
+ attributes.each do |key, value|
61
+ m = "#{key}=".to_sym
62
+ self.send(m, value) if self.respond_to?(m)
63
+ end
64
+ end
65
+
66
+ def name
67
+ [first_name, last_name].join(' ')
68
+ end
69
+
70
+ def save(options={})
71
+ contact_hash = {}
72
+ %w(first_name last_name organization_name job_title address1 address2 city
73
+ state_province postal_code country email_address phone phone_ext fax).each do |attribute|
74
+ contact_hash[Contact.resolve(attribute)] = self.send(attribute)
75
+ end
76
+
77
+ options.merge!(DNSimple::Client.standard_options_with_credentials)
78
+ options.merge!({:body => {:contact => contact_hash}})
79
+
80
+ response = self.class.put("#{Client.base_uri}/contacts/#{id}", options)
81
+
82
+ pp response if Client.debug?
83
+
84
+ case response.code
85
+ when 200
86
+ return self
87
+ when 401
88
+ raise RuntimeError, "Authentication failed"
89
+ else
90
+ raise RuntimeError, "Failed to update contact: #{response.inspect}"
91
+ end
92
+ end
93
+
94
+ # Delete the contact from DNSimple. WARNING: this cannot be undone.
95
+ def delete(options={})
96
+ options.merge!({:basic_auth => Client.credentials})
97
+ self.class.delete("#{Client.base_uri}/contacts/#{id}.json", options)
98
+ end
99
+ alias :destroy :delete
100
+
101
+ # Map an aliased field name to it's real name. For example, if you
102
+ # pass "first" it will be resolved to "first_name", "email" is resolved
103
+ # to "email_address" and so on.
104
+ def self.resolve(name)
105
+ aliases = {
106
+ 'first' => 'first_name',
107
+ 'last' => 'last_name',
108
+ 'state' => 'state_province',
109
+ 'province' => 'state_province',
110
+ 'state_or_province' => 'state_province',
111
+ 'email' => 'email_address',
112
+ }
113
+ aliases[name.to_s] || name
114
+ end
115
+
116
+ def self.resolve_attributes(attributes)
117
+ resolved_attributes = {}
118
+ attributes.each do |k, v|
119
+ resolved_attributes[resolve(k)] = v
120
+ end
121
+ resolved_attributes
122
+ end
123
+
124
+ # Create the contact with the given attributes in DNSimple.
125
+ # This method returns a Contact instance of the contact is created
126
+ # and raises an error otherwise.
127
+ def self.create(attributes, options={})
128
+ contact_hash = resolve_attributes(attributes)
129
+
130
+ options.merge!({:body => {:contact => contact_hash}})
131
+ options.merge!({:basic_auth => Client.credentials})
132
+
133
+ response = self.post("#{Client.base_uri}/contacts.json", options)
134
+
135
+ pp response if Client.debug?
136
+
137
+ case response.code
138
+ when 201
139
+ return Contact.new(response["contact"])
140
+ when 401
141
+ raise RuntimeError, "Authentication failed"
142
+ else
143
+ raise RuntimeError, "Failed to create contact: #{response.inspect}"
144
+ end
145
+ end
146
+
147
+ def self.find(id, options={})
148
+ options.merge!({:basic_auth => Client.credentials})
149
+ response = self.get("#{Client.base_uri}/contacts/#{id}.json", options)
150
+
151
+ pp response if Client.debug?
152
+
153
+ case response.code
154
+ when 200
155
+ return Contact.new(response["contact"])
156
+ when 401
157
+ raise RuntimeError, "Authentication failed"
158
+ when 404
159
+ raise RuntimeError, "Could not find contact #{id}"
160
+ else
161
+ raise DNSimple::Error.new(id, response["errors"])
162
+ end
163
+ end
164
+
165
+ def self.all(options={})
166
+ options.merge!({:basic_auth => Client.credentials})
167
+ response = self.get("#{Client.base_uri}/contacts.json", options)
168
+
169
+ pp response if Client.debug?
170
+
171
+ case response.code
172
+ when 200
173
+ response.map { |r| Contact.new(r["contact"]) }
174
+ when 401
175
+ raise RuntimeError, "Authentication failed"
176
+ else
177
+ raise RuntimeError, "Error: #{response.code}"
178
+ end
179
+ end
180
+ end
181
+ end
@@ -0,0 +1,246 @@
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
28
+
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
45
+
46
+ # Overwrite the nameservers for a given domain.
47
+ def update_name_servers(name_servers, options = {})
48
+ options.merge!(DNSimple::Client.standard_options_with_credentials)
49
+
50
+ name_servers_hash = {}
51
+ ns_index = 1
52
+
53
+ name_servers.each do |name_server|
54
+ name_servers_hash["ns#{ns_index}"] = name_server
55
+ ns_index += 1
56
+ end
57
+
58
+ options.merge!(:body => { :name_servers => name_servers_hash })
59
+
60
+ response = self.class.post("#{DNSimple::Client.base_uri}/domains/#{name}/name_servers.json", options)
61
+ pp response if DNSimple::Client.debug?
62
+
63
+ case response.code
64
+
65
+ when 200
66
+ name_servers
67
+ else
68
+ raise RuntimeError, "Error: #{response.code}"
69
+ end
70
+ end
71
+
72
+ #:nodoc:
73
+ def resolve_template(template)
74
+ case template
75
+ when DNSimple::Template
76
+ template
77
+ else
78
+ DNSimple::Template.find(template)
79
+ end
80
+ end
81
+
82
+ def applied_services(options={})
83
+ options.merge!(DNSimple::Client.standard_options_with_credentials)
84
+ response = self.class.get("#{Client.base_uri}/domains/#{name}/applied_services", options)
85
+ pp response if DNSimple::Client.debug?
86
+ case response.code
87
+ when 200
88
+ response.map { |r| DNSimple::Service.new(r["service"]) }
89
+ when 401
90
+ raise RuntimeError, "Authentication failed"
91
+ else
92
+ raise RuntimeError, "Error: #{response.code}"
93
+ end
94
+ end
95
+
96
+ def available_services(options={})
97
+ options.merge!(DNSimple::Client.standard_options_with_credentials)
98
+ response = self.class.get("#{DNSimple::Client.base_uri}/domains/#{name}/available_services", options)
99
+ pp response if DNSimple::Client.debug?
100
+ case response.code
101
+ when 200
102
+ response.map { |r| DNSimple::Service.new(r["service"]) }
103
+ when 401
104
+ raise RuntimeError, "Authentication failed"
105
+ else
106
+ raise RuntimeError, "Error: #{response.code}"
107
+ end
108
+ end
109
+
110
+ def add_service(id_or_short_name, options={})
111
+ options.merge!(DNSimple::Client.standard_options_with_credentials)
112
+ options.merge!(:body => {:service => {:id => id_or_short_name}})
113
+ response = self.class.post("#{DNSimple::Client.base_uri}/domains/#{name}/applied_services", options)
114
+ pp response if DNSimple::Client.debug?
115
+ case response.code
116
+ when 200
117
+ true
118
+ when 401
119
+ raise RuntimeError, "Authentication failed"
120
+ else
121
+ raise "Error: #{response.code}"
122
+ end
123
+ end
124
+
125
+ def remove_service(id, options={})
126
+ options.merge!(DNSimple::Client.standard_options_with_credentials)
127
+ response = self.class.delete("#{DNSimple::Client.base_uri}/domains/#{name}/applied_services/#{id}", options)
128
+ pp response if DNSimple::Client.debug?
129
+ case response.code
130
+ when 200
131
+ true
132
+ when 401
133
+ raise RuntimeError, "Authentication failed"
134
+ else
135
+ raise "Error: #{response.code}"
136
+ end
137
+ end
138
+
139
+ # Check the availability of a name
140
+ def self.check(name, options={})
141
+ options.merge!(DNSimple::Client.standard_options_with_credentials)
142
+ response = self.get("#{DNSimple::Client.base_uri}/domains/#{name}/check", options)
143
+ pp response if DNSimple::Client.debug?
144
+ case response.code
145
+ when 200
146
+ "registered"
147
+ when 401
148
+ raise RuntimeError, "Authentication failed"
149
+ when 404
150
+ "available"
151
+ else
152
+ raise "Error: #{response.code}"
153
+ end
154
+ end
155
+
156
+ # Create the domain with the given name in DNSimple. This
157
+ # method returns a Domain instance if the name is created
158
+ # and raises an error otherwise.
159
+ def self.create(name, options={})
160
+ options.merge!(DNSimple::Client.standard_options_with_credentials)
161
+
162
+ domain_hash = {:name => name}
163
+ options.merge!({:body => {:domain => domain_hash}})
164
+
165
+ response = self.post("#{DNSimple::Client.base_uri}/domains", options)
166
+
167
+ pp response if DNSimple::Client.debug?
168
+
169
+ case response.code
170
+ when 201
171
+ return DNSimple::Domain.new(response["domain"])
172
+ when 401
173
+ raise RuntimeError, "Authentication failed"
174
+ else
175
+ raise DNSimple::DomainError.new(name, response["errors"])
176
+ end
177
+ end
178
+
179
+ # Purchase a domain name.
180
+ def self.register(name, registrant={}, extended_attributes={}, options={})
181
+ options.merge!(DNSimple::Client.standard_options_with_credentials)
182
+
183
+ body = {:domain => {:name => name}}
184
+ if registrant
185
+ if registrant[:id]
186
+ body[:domain][:registrant_id] = registrant[:id]
187
+ else
188
+ body.merge!(:contact => Contact.resolve_attributes(registrant))
189
+ end
190
+ end
191
+ body.merge!(:extended_attribute => extended_attributes)
192
+ options.merge!({:body => body})
193
+
194
+ response = self.post("#{DNSimple::Client.base_uri}/domain_registrations", options)
195
+
196
+ pp response if DNSimple::Client.debug?
197
+
198
+ case response.code
199
+ when 201
200
+ return DNSimple::Domain.new(response["domain"])
201
+ when 401
202
+ raise RuntimeError, "Authentication failed"
203
+ else
204
+ raise DNSimple::DomainError.new(name, response["errors"])
205
+ end
206
+ end
207
+
208
+ # Find a specific domain in the account either by the numeric ID
209
+ # or by the fully-qualified domain name.
210
+ def self.find(id_or_name, options={})
211
+ options.merge!(DNSimple::Client.standard_options_with_credentials)
212
+ response = self.get("#{DNSimple::Client.base_uri}/domains/#{id_or_name}", options)
213
+
214
+ pp response if DNSimple::Client.debug?
215
+
216
+ case response.code
217
+ when 200
218
+ return DNSimple::Domain.new(response["domain"])
219
+ when 401
220
+ raise RuntimeError, "Authentication failed"
221
+ when 404
222
+ raise RuntimeError, "Could not find domain #{id_or_name}"
223
+ else
224
+ raise DNSimple::DomainError.new(id_or_name, response["errors"])
225
+ end
226
+ end
227
+
228
+ # Get all domains for the account.
229
+ def self.all(options={})
230
+ options.merge!(DNSimple::Client.standard_options_with_credentials)
231
+ response = self.get("#{DNSimple::Client.base_uri}/domains", options)
232
+
233
+ pp response if DNSimple::Client.debug?
234
+
235
+ case response.code
236
+ when 200
237
+ response.map { |r| DNSimple::Domain.new(r["domain"]) }
238
+ when 401
239
+ raise RuntimeError, "Authentication failed"
240
+ else
241
+ raise RuntimeError, "Error: #{response.code}"
242
+ end
243
+ end
244
+
245
+ end
246
+ end