Skip to content

Commit

Permalink
Implemented getWebhook()
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Mar 3, 2016
1 parent 4cc5f8b commit 538e53a
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/dnsimple/client/webhooks.rb
Expand Up @@ -61,6 +61,23 @@ def create_webhook(account_id, attributes = {}, options = {})
end
alias :create :create_webhook

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

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

end
end
end
37 changes: 37 additions & 0 deletions spec/dnsimple/client/webhooks_spec.rb
Expand Up @@ -76,4 +76,41 @@
end
end

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

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

it "builds the correct request" do
subject.webhook(account_id, webhook = 1)

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

it "returns the webhook" do
response = subject.webhook(account_id, 0)
expect(response).to be_a(Dnsimple::Response)

result = response.data
expect(result).to be_a(Dnsimple::Struct::Webhook)
expect(result.id).to eq(1)
expect(result.url).to eq("https://webhook.test")
end

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

expect {
subject.webhook(account_id, 0)
}.to raise_error(Dnsimple::NotFoundError)
end
end
end

end
17 changes: 17 additions & 0 deletions spec/fixtures.http/getWebhook/success.http
@@ -0,0 +1,17 @@
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 15 Feb 2016 17:06:09 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 200 OK
X-RateLimit-Limit: 4000
X-RateLimit-Remaining: 3992
X-RateLimit-Reset: 1455559348
ETag: W/"cbb707ff6fc185d71f5a8df3110f1379"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 0109ea48-b7f0-4f78-a970-6866653b83eb
X-Runtime: 0.087618
Strict-Transport-Security: max-age=31536000

{"data":{"id":1,"url":"https://webhook.test"}}
12 changes: 12 additions & 0 deletions spec/fixtures.http/notfound-webhook.http
@@ -0,0 +1,12 @@
HTTP/1.1 404 Not Found
Server: nginx
Date: Thu, 03 Mar 2016 11:55:29 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 404 Not Found
Cache-Control: no-cache
X-Request-Id: 4f154fa2-3ce9-4bdd-88a3-42ccc8dbc087
X-Runtime: 0.012159

{"message":"Webhook `0` not found"}

0 comments on commit 538e53a

Please sign in to comment.