Skip to content

Commit

Permalink
Change registrar.check to map the original check API
Browse files Browse the repository at this point in the history
  • Loading branch information
weppos committed Jan 16, 2015
1 parent 2e00dcc commit 60714a4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 11 deletions.
27 changes: 20 additions & 7 deletions lib/dnsimple/client/registrar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,28 @@ module Registrar
#
# @param [#to_s] name The domain name to check.
#
# @return [String] "available" or "registered"
# @return [Hash] The response containing the status, price, and more details.
# @raise [RequestError] When the request fails.
def check(name, options={})
begin
client.get("v1/domains/#{name}/check", options)
"registered"
rescue NotFoundError
"available"
def check(name)
response = begin
client.get("v1/domains/#{name}/check")
rescue NotFoundError => e
e.response
end

response.parsed_response
end

# 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 [Boolean] true if the domain is available
# @raise [RequestError] When the request fails.
def available?(name)
check(name)["status"] == "available"
end

# Registers a domain.
Expand Down
50 changes: 46 additions & 4 deletions spec/dnsimple/client/registrar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,50 @@
to_return(read_fixture("registrar/check/registered.http"))
end

it "returns available" do
expect(subject.check("example.com")).to eq("registered")
it "returns the result" do
result = subject.check("example.com")

expect(result).to be_a(Hash)
expect(result['status']).to eq("unavailable")
end
end

context "the domain is available" do
before do
stub_request(:get, %r[/v1/domains/.+/check$]).
to_return(read_fixture("registrar/check/available.http"))
end

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

expect(result).to be_a(Hash)
expect(result['status']).to eq("available")
end
end
end

describe "#available?" do
before do
stub_request(:get, %r[/v1/domains/.+/check$]).
to_return(read_fixture("registrar/check/registered.http"))
end

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

expect(WebMock).to have_requested(:get, "https://api.zone/v1/domains/example.com/check").
with(headers: { 'Accept' => 'application/json' })
end

context "the domain is registered" do
before do
stub_request(:get, %r[/v1/domains/.+/check$]).
to_return(read_fixture("registrar/check/registered.http"))
end

it "returns false" do
expect(subject.available?("example.com")).to be_falsey
end
end

Expand All @@ -35,8 +77,8 @@
to_return(read_fixture("registrar/check/available.http"))
end

it "returns available" do
expect(subject.check("example.com")).to eq("available")
it "returns true" do
expect(subject.available?("example.com")).to be_truthy
end
end
end
Expand Down

0 comments on commit 60714a4

Please sign in to comment.