Skip to content

Commit

Permalink
Add domain registration, transfer, renewal
Browse files Browse the repository at this point in the history
  • Loading branch information
weppos committed Dec 16, 2014
1 parent 0fc9774 commit b19bd8c
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 55 deletions.
58 changes: 58 additions & 0 deletions lib/dnsimple/client/domains_service.rb
Expand Up @@ -92,6 +92,8 @@ def disable_auto_renewal(domain)

# Checks the availability of a domain name.
#
# @see http://developer.dnsimple.com/domains/registry/#check
#
# @param [#to_s] name The domain name to check.
#
# @return [String] "available" or "registered"
Expand All @@ -105,6 +107,62 @@ def check(name, options={})
end
end

# Registers a domain.
#
# @see http://developer.dnsimple.com/domains/registry/#register
#
# @param [#to_s] name The domain name to register.
# @param [Fixnum] registrant_id The id of the contact to use as registrant.
# @param [Hash] extended_attributes
# @param [Hash] options
#
# @return [Domain]
# @raise [RequestError] When the request fails.
def register(name, registrant_id, extended_attributes = {}, options = {})
body = { domain: { name: name, registrant_id: registrant_id }, extended_attribute: extended_attributes }
options[:body] = body

response = client.post("v1/domain_registrations", options)
Domain.new(response["domain"])
end

# Transfers a domain.
#
# @see http://developer.dnsimple.com/domains/registry/#transfer
#
# @param [#to_s] name The domain name to register.
# @param [String] auth_code
# @param [Fixnum] registrant_id The id of the contact to use as registrant.
# @param [Hash] extended_attributes
# @param [Hash] options
#
# @return [TransferOrder]
# @raise [RequestError] When the request fails.
def transfer(name, auth_code, registrant_id, extended_attributes = {}, options = {})
body = { domain: { name: name, registrant_id: registrant_id }, extended_attribute: extended_attributes, transfer_order: { authinfo: auth_code } }
options[:body] = body

response = client.post("v1/domain_transfers", options)
TransferOrder.new(response["transfer_order"])
end

# Renew a domain.
#
# @see http://developer.dnsimple.com/domains/registry/#renew
#
# @param [#to_s] name The domain name to renew.
# @param [Hash] options
#
# @return [Domain]
# @raise [RequestError] When the request fails.
def renew(name, options = {})
body = { domain: { name: name }}
options[:body] = body

response = client.post("v1/domain_renewals", options)
Domain.new(response["domain"])
end

end
end
end
24 changes: 0 additions & 24 deletions lib/dnsimple/domain.rb
Expand Up @@ -34,30 +34,6 @@ class Domain < Base
# The Date the domain was last update in DNSimple.
attr_accessor :updated_at


# Purchase a domain name.
def self.register(name, registrant={}, extended_attributes={}, options={})
body = {:domain => {:name => name}}
if registrant
if registrant[:id]
body[:domain][:registrant_id] = registrant[:id]
else
body.merge!(:contact => Contact.resolve_attributes(registrant))
end
end
body.merge!(:extended_attribute => extended_attributes)
options.merge!({:body => body})

response = Client.post("v1/domain_registrations", options)

case response.code
when 201
return Domain.new(response["domain"])
else
raise RequestError.new("Error registering domain", response)
end
end

end

end
28 changes: 1 addition & 27 deletions lib/dnsimple/transfer_order.rb
@@ -1,34 +1,8 @@
module Dnsimple

# Represents a transfer order.
class TransferOrder < Base

attr_accessor :id

attr_accessor :status

def self.create(name, authinfo='', registrant={}, extended_attributes={}, options={})
body = {:domain => {:name => name}, :transfer_order => {:authinfo => authinfo}}

if registrant[:id]
body[:domain][:registrant_id] = registrant[:id]
else
body.merge!(:contact => Contact.resolve_attributes(registrant))
end

body.merge!(:extended_attribute => extended_attributes)

options.merge!({:body => body})

response = Client.post("v1/domain_transfers", options)

case response.code
when 201
new(response["transfer_order"])
else
raise RequestError.new("Error creating transfer order", response)
end
end

end

end
54 changes: 54 additions & 0 deletions spec/dnsimple/client/domains_service_spec.rb
Expand Up @@ -238,4 +238,58 @@
end
end

describe "#register" do
before do
stub_request(:post, %r[/v1/domain_registrations]).
to_return(read_fixture("domains/registration/success.http"))
end

it "builds the correct request" do
subject.register("example.com", 10)

expect(WebMock).to have_requested(:post, "https://api.zone/v1/domain_registrations").
with(body: { domain: { name: "example.com", registrant_id: "10" }}).
with(headers: { 'Accept' => 'application/json' })
end

it "returns the domain" do
result = subject.register("example.com", 10)

expect(result).to be_a(Dnsimple::Domain)
expect(result.id).to eq(1797)
end
end

describe "#renew" do
before do
stub_request(:post, %r[/v1/domain_renewals]).
to_return(read_fixture("domains/renewal/success.http"))
end

it "builds the correct request" do
subject.renew("example.com")

expect(WebMock).to have_requested(:post, "https://api.zone/v1/domain_renewals").
with(headers: { 'Accept' => 'application/json' })
end

it "returns the domain" do
result = subject.renew("example.com")

expect(result).to be_a(Dnsimple::Domain)
expect(result.id).to eq(1797)
end

context "when something does not exist" do
it "raises RecordNotFound" do
stub_request(:post, %r[/v1]).
to_return(read_fixture("domains/notfound.http"))

expect {
subject.renew("example.com")
}.to raise_error(Dnsimple::RecordNotFound)
end
end
end

end
8 changes: 4 additions & 4 deletions spec/dnsimple/client/name_servers_service_spec.rb
Expand Up @@ -8,7 +8,7 @@
describe "#list" do
before do
stub_request(:get, %r[/v1/domains/.+/name_servers]).
to_return(read_fixture("domains/nameservers/success.http"))
to_return(read_fixture("nameservers/success.http"))
end

it "builds the correct request" do
Expand All @@ -25,7 +25,7 @@
context "when something does not exist" do
it "raises RecordNotFound" do
stub_request(:get, %r[/v1]).
to_return(read_fixture("domains/nameservers/notfound.http"))
to_return(read_fixture("nameservers/notfound.http"))

expect {
subject.list("example.com")
Expand All @@ -37,7 +37,7 @@
describe "#change" do
before do
stub_request(:post, %r[/v1/domains/.+/name_servers]).
to_return(read_fixture("domains/nameservers/success.http"))
to_return(read_fixture("nameservers/success.http"))
end

it "builds the correct request" do
Expand All @@ -55,7 +55,7 @@
context "when something does not exist" do
it "raises RecordNotFound" do
stub_request(:post, %r[/v1]).
to_return(read_fixture("domains/nameservers/notfound.http"))
to_return(read_fixture("nameservers/notfound.http"))

expect {
subject.change("example.com", %w())
Expand Down
19 changes: 19 additions & 0 deletions spec/files/domains/registration/badrequest-missingdomain.http
@@ -0,0 +1,19 @@
HTTP/1.1 400 Bad Request
Server: nginx
Date: Mon, 15 Dec 2014 22:49:03 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Status: 400 Bad Request
Strict-Transport-Security: max-age=631138519
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1
X-Content-Type-Options: nosniff
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Authorization,Accepts,Content-Type,X-DNSimple-Token,X-DNSimple-Domain-Token,X-CSRF-Token,x-requested-with
Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS
Cache-Control: no-cache
X-Request-Id: 03ac0f0b-c485-4465-bed9-d584f9865ccd
X-Runtime: 0.027365

{"message":"Required parameter missing: domain"}
19 changes: 19 additions & 0 deletions spec/files/domains/registration/badrequest-missingregistrant.http
@@ -0,0 +1,19 @@
HTTP/1.1 400 Bad Request
Server: nginx
Date: Mon, 15 Dec 2014 22:52:06 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Status: 400 Bad Request
Strict-Transport-Security: max-age=631138519
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1
X-Content-Type-Options: nosniff
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Authorization,Accepts,Content-Type,X-DNSimple-Token,X-DNSimple-Domain-Token,X-CSRF-Token,x-requested-with
Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS
Cache-Control: no-cache
X-Request-Id: 5542b2f5-628d-4ddc-845e-32282496d37c
X-Runtime: 1.826527

{"message":"Validation failed","errors":{"base":["A registrant is required"]}}
21 changes: 21 additions & 0 deletions spec/files/domains/registration/success.http
@@ -0,0 +1,21 @@
HTTP/1.1 201 Created
Server: nginx
Date: Mon, 15 Dec 2014 23:04:01 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Status: 201 Created
Strict-Transport-Security: max-age=631138519
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1
X-Content-Type-Options: nosniff
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Authorization,Accepts,Content-Type,X-DNSimple-Token,X-DNSimple-Domain-Token,X-CSRF-Token,x-requested-with
Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS
ETag: "642e37a05eb4016a974eb52085385822"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: c67b3494-bea3-4fe3-9fdd-8ec5ca89095e
X-Runtime: 5.852385
Strict-Transport-Security: max-age=315360000

{"domain":{"id":1797,"user_id":null,"registrant_id":409,"name":"example-1418683185.com","unicode_name":"example-1418683185.com","token":"domain-token","state":"registered","language":null,"lockable":true,"auto_renew":false,"whois_protected":false,"record_count":5,"service_count":0,"expires_on":"2015-12-15","created_at":"2014-12-15T23:03:56.636Z","updated_at":"2014-12-15T23:04:01.406Z"}}
19 changes: 19 additions & 0 deletions spec/files/domains/renewal/badrequest-missingrenewal.http
@@ -0,0 +1,19 @@
HTTP/1.1 400 Bad Request
Server: nginx
Date: Mon, 15 Dec 2014 23:22:54 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Status: 400 Bad Request
Strict-Transport-Security: max-age=631138519
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1
X-Content-Type-Options: nosniff
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Authorization,Accepts,Content-Type,X-DNSimple-Token,X-DNSimple-Domain-Token,X-CSRF-Token,x-requested-with
Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS
Cache-Control: no-cache
X-Request-Id: 94aa82e2-ae9e-4298-9524-5ac0e9d0a1b7
X-Runtime: 0.041781

{"message":"Required parameter missing: domain_renewal"}
19 changes: 19 additions & 0 deletions spec/files/domains/renewal/badrequest-unable.http
@@ -0,0 +1,19 @@
HTTP/1.1 422 Unprocessable Entity
Server: nginx
Date: Mon, 15 Dec 2014 23:21:08 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Status: 422 Unprocessable Entity
Strict-Transport-Security: max-age=631138519
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1
X-Content-Type-Options: nosniff
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Authorization,Accepts,Content-Type,X-DNSimple-Token,X-DNSimple-Domain-Token,X-CSRF-Token,x-requested-with
Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS
Cache-Control: no-cache
X-Request-Id: 60f79466-a97b-4f6a-b3e2-95a7f22da87c
X-Runtime: 0.060428

{"message":"example-1418683185.com may not be renewed at this time"}
21 changes: 21 additions & 0 deletions spec/files/domains/renewal/success.http
@@ -0,0 +1,21 @@
HTTP/1.1 201 Created
Server: nginx
Date: Mon, 15 Dec 2014 23:04:01 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Status: 201 Created
Strict-Transport-Security: max-age=631138519
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1
X-Content-Type-Options: nosniff
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Authorization,Accepts,Content-Type,X-DNSimple-Token,X-DNSimple-Domain-Token,X-CSRF-Token,x-requested-with
Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS
ETag: "642e37a05eb4016a974eb52085385822"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: c67b3494-bea3-4fe3-9fdd-8ec5ca89095e
X-Runtime: 5.852385
Strict-Transport-Security: max-age=315360000

{"domain":{"id":1797,"user_id":null,"registrant_id":409,"name":"example-1418683185.com","unicode_name":"example-1418683185.com","token":"domain-token","state":"registered","language":null,"lockable":true,"auto_renew":false,"whois_protected":false,"record_count":5,"service_count":0,"expires_on":"2015-12-15","created_at":"2014-12-15T23:03:56.636Z","updated_at":"2014-12-15T23:04:01.406Z"}}
21 changes: 21 additions & 0 deletions spec/files/domains/transfer/success.http
@@ -0,0 +1,21 @@
HTTP/1.1 201 Created
Server: nginx
Date: Tue, 16 Dec 2014 00:15:46 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Status: 201 Created
Strict-Transport-Security: max-age=631138519
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1
X-Content-Type-Options: nosniff
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Authorization,Accepts,Content-Type,X-DNSimple-Token,X-DNSimple-Domain-Token,X-CSRF-Token,x-requested-with
Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS
ETag: "8ae2f909210b303eebe68b66e636b117"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: c1ecc834-9cf7-4973-b060-34b701e6b686
X-Runtime: 16.594202
Strict-Transport-Security: max-age=315360000

{"transfer_order":{"id":11,"domain_id":1803,"state":"submitted","created_at":"2014-12-16T00:15:44.781Z","updated_at":"2014-12-16T00:15:46.065Z"}}
File renamed without changes.
File renamed without changes.

0 comments on commit b19bd8c

Please sign in to comment.