Skip to content

Commit

Permalink
Merge 856c7eb into 304c61c
Browse files Browse the repository at this point in the history
  • Loading branch information
jacegu committed Mar 2, 2016
2 parents 304c61c + 856c7eb commit de08363
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 24 deletions.
45 changes: 34 additions & 11 deletions lib/dnsimple/client/registrar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@ module Dnsimple
class Client
module Registrar

# Checks whether a domain is available to be registered.
#
# @see https://developer.dnsimple.com/v2/registrar/#check
#
# @example Check whether example.com is available:
# client.registrar.check(1010, "example.com")
#
# @param [Fixnum] account_id the account ID
# @param [#to_s] domain_name The domain name to check.
# @param [Hash] options
# @return [Struct::DomainCheck]
#
# @raise [RequestError] When the request fails.
def check(account_id, domain_name, options = {})
endpoint = Client.versioned("/%s/registrar/domains/%s/check" % [account_id, domain_name])
response = client.get(endpoint, options)

Dnsimple::Response.new(response, Struct::DomainCheck.new(response["data"]))
end

# Registers a domain.
#
# @see https://developer.dnsimple.com/v2/registrar/#register
Expand All @@ -15,29 +35,32 @@ module Registrar
# @raise [RequestError] When the request fails.
def register(account_id, domain_name, attributes = {}, options = {})
Extra.validate_mandatory_attributes(attributes, [:registrant_id])
options = options.merge(attributes)
response = client.post(Client.versioned("/%s/registrar/domains/%s/registration" % [account_id, domain_name]), options)
endpoint = Client.versioned("/%s/registrar/domains/%s/registration" % [account_id, domain_name])
response = client.post(endpoint, options.merge(attributes))

Dnsimple::Response.new(response, Struct::Domain.new(response["data"]))
end

# Checks whether a domain is available to be registered.
# Starts the transfer of a domain to DNSimple.
#
# @see https://developer.dnsimple.com/v2/registrar/#check
# @see https://developer.dnsimple.com/v2/registrar/#transfer
#
# @example Check whether example.com is available.
# client.registrar.check(1010, "example.com")
# @example Initiate the transfer for example.com using the contact 1234 as registrant:
# client.registrar.transfer(1010, "example.com", registrant_id: 1234, auth_info: "x1y2z3")
#
# @param [Fixnum] account_id the account ID
# @param [#to_s] domain_name The domain name to check.
# @param [#to_s] domain_name The domain name to transfer.
# @param [Hash] attributes
# @param [Hash] options
# @return [Struct::DomainCheck]
# @return [Struct::Domain]
#
# @raise [RequestError] When the request fails.
def check(account_id, domain_name, options = {})
response = client.get(Client.versioned("/%s/registrar/domains/%s/check" % [account_id, domain_name]), options)
def transfer(account_id, domain_name, attributes = {}, options = {})
Extra.validate_mandatory_attributes(attributes, [:registrant_id, :auth_info])
endpoint = Client.versioned("/%s/registrar/domains/%s/transfer" % [account_id, domain_name])
response = client.post(endpoint, options.merge(attributes))

Dnsimple::Response.new(response, Struct::DomainCheck.new(response["data"]))
Dnsimple::Response.new(response, Struct::Domain.new(response["data"]))
end

end
Expand Down
73 changes: 60 additions & 13 deletions spec/dnsimple/client/registrar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,33 @@
subject { described_class.new(base_url: "https://api.dnsimple.test", access_token: "a1b2c3").registrar }


describe "#check" do
let(:account_id) { 1010 }

before do
stub_request(:get, %r[/v2/#{account_id}/registrar/domains/.+/check$])
.to_return(read_http_fixture("checkDomain/success.http"))
end

it "builds the correct request" do
subject.check(account_id, domain_name = "example.com")

expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/registrar/domains/#{domain_name}/check")
.with(headers: { "Accept" => "application/json" })
end

it "returns the availability" do
response = subject.check(account_id, "example.com")
expect(response).to be_a(Dnsimple::Response)

result = response.data
expect(result).to be_a(Dnsimple::Struct::DomainCheck)
expect(result.domain).to eq("example.com")
expect(result.available).to be_truthy
expect(result.premium).to be_falsey
end
end

describe "#register" do
let(:account_id) { 1010 }

Expand All @@ -20,7 +47,7 @@

expect(WebMock).to have_requested(:post, "https://api.dnsimple.test/v2/#{account_id}/registrar/domains/#{domain_name}/registration")
.with(body: attributes)
.with(headers: { 'Accept' => 'application/json' })
.with(headers: { "Accept" => "application/json" })
end

it "returns the domain" do
Expand All @@ -33,30 +60,50 @@
end
end

describe "#check" do
describe "#transfer" do
let(:account_id) { 1010 }

before do
stub_request(:get, %r[/v2/#{account_id}/registrar/domains/.+/check$])
.to_return(read_http_fixture("checkDomain/success.http"))
stub_request(:post, %r[/v2/#{account_id}/registrar/domains/.+/transfer$])
.to_return(read_http_fixture("transferDomain/success.http"))
end

let(:attributes) { { registrant_id: "10", auth_info: "x1y2z3" } }

it "builds the correct request" do
subject.check(account_id, domain_name = "example.com")
subject.transfer(account_id, domain_name = "example.com", attributes)

expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/registrar/domains/#{domain_name}/check")
.with(headers: { 'Accept' => 'application/json' })
expect(WebMock).to have_requested(:post, "https://api.dnsimple.test/v2/#{account_id}/registrar/domains/#{domain_name}/transfer")
.with(body: attributes)
.with(headers: { "Accept" => "application/json" })
end

it "returns the availability" do
response = subject.check(account_id, "example.com")
it "returns the domain" do
response = subject.transfer(account_id, "example.com", attributes)
expect(response).to be_a(Dnsimple::Response)

result = response.data
expect(result).to be_a(Dnsimple::Struct::DomainCheck)
expect(result.domain).to eq("example.com")
expect(result.available).to be_truthy
expect(result.premium).to be_falsey
expect(result).to be_a(Dnsimple::Struct::Domain)
expect(result.name).to eq("example.com")
expect(result.registrant_id).to eq(10)
end

context "when the attributes are incomplete" do
it "raises ArgumentError" do
expect { subject.transfer(account_id, "example.com", auth_info: "x1y2z3") }.to raise_error(ArgumentError)
expect { subject.transfer(account_id, "example.com", registrant_id: "10") }.to raise_error(ArgumentError)
end
end

context "when the domain is already in DNSimple" do
it "raises a BadRequestError" do
stub_request(:post, %r[/v2/#{account_id}/registrar/domains/.+/transfer$])
.to_return(read_http_fixture("transferDomain/error-indnsimple.http"))

expect {
subject.transfer(account_id, "example.com", attributes)
}.to raise_error(Dnsimple::RequestError)
end
end
end

Expand Down
15 changes: 15 additions & 0 deletions spec/fixtures.http/transferDomain/error-indnsimple.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
HTTP/1.1 400 Bad Request
Server: nginx
Date: Sun, 21 Feb 2016 13:11:54 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 400 Bad Request
X-RateLimit-Limit: 4000
X-RateLimit-Remaining: 3996
X-RateLimit-Reset: 1456063541
Cache-Control: no-cache
X-Request-Id: 15d2e302-e835-4dda-9652-03d8962280ae
X-Runtime: 0.994068

{"message":"The domain google.com is already in DNSimple and cannot be added"}
17 changes: 17 additions & 0 deletions spec/fixtures.http/transferDomain/success.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
HTTP/1.1 201 Created
Server: nginx
Date: Sun, 21 Feb 2016 13:32:02 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 201 Created
X-RateLimit-Limit: 4000
X-RateLimit-Remaining: 3992
X-RateLimit-Reset: 1456063540
ETag: W/"68e1aa1046c3f33f2ad796befc2b9f5b"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: c3401f82-d570-4998-a614-245f3a7677ba
X-Runtime: 15.045124
Strict-Transport-Security: max-age=31536000

{"data":{"id":1,"account_id":1010,"registrant_id":10,"name":"example.com","unicode_name":"example.com","token":"domain-token","state":"hosted","auto_renew":false,"private_whois":false,"expires_on":null,"created_at":"2016-02-21T13:31:58.745Z","updated_at":"2016-02-21T13:31:58.745Z"}}

0 comments on commit de08363

Please sign in to comment.