Skip to content

Commit

Permalink
Add getZone
Browse files Browse the repository at this point in the history
  • Loading branch information
weppos committed Jan 22, 2016
1 parent 618a3af commit 93ddd7b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/dnsimple/client/zones.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ def all_zones(account_id, options = {})
paginate(:zones, account_id, options)
end

# Gets a zone from the account.
#
# @see https://developer.dnsimple.com/v2/zones/#get
#
# @param [Fixnum, Dnsimple::Client::WILDCARD_ACCOUNT] account_id the account ID or wildcard
# @param [#to_s] zone_id The zone name.
# @param [Hash] options
# @return [Dnsimple::Response<Dnsimple::Struct::Zone>]
#
# @raise [Dnsimple::NotFoundError]
# @raise [Dnsimple::RequestError]
def zone(account_id, zone_id, options = {})
response = client.get(Client.versioned("/%s/zones/%s" % [account_id, zone_id]), options)

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

end
end
end
41 changes: 41 additions & 0 deletions spec/dnsimple/client/zones_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,45 @@
end
end

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

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

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

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

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

result = response.data
expect(result).to be_a(Dnsimple::Struct::Zone)
expect(result.id).to eq(1)
expect(result.account_id).to eq(1010)
expect(result.name).to eq("example-alpha.com")
expect(result.reverse).to eq(false)
expect(result.created_at).to eq("2015-04-23T07:40:03.045Z")
expect(result.updated_at).to eq("2015-04-23T07:40:03.051Z")
end

context "when the zone does not exist" do
it "raises NotFoundError" do
stub_request(:get, %r[/v2])
.to_return(read_http_fixture("notfound-zone.http"))

expect {
subject.zone(account_id, "example.com")
}.to raise_error(Dnsimple::NotFoundError)
end
end
end

end
17 changes: 17 additions & 0 deletions spec/fixtures.http/getZone/success.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 22 Jan 2016 16:54:14 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 200 OK
X-RateLimit-Limit: 4000
X-RateLimit-Remaining: 3995
X-RateLimit-Reset: 1453484046
ETag: W/"2161245abd349a34cba32a970e6424ba"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 93182033-a215-484e-a107-5235fa48001c
X-Runtime: 0.177942
Strict-Transport-Security: max-age=31536000

{"data":{"id":1,"account_id":1010,"name":"example-alpha.com","reverse":false,"created_at":"2015-04-23T07:40:03.045Z","updated_at":"2015-04-23T07:40:03.051Z"}}

0 comments on commit 93ddd7b

Please sign in to comment.