Skip to content

Commit

Permalink
Implemented createWebhook()
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Mar 3, 2016
1 parent bd1ea00 commit 4cc5f8b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/dnsimple/client/webhooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ def all_webhooks(account_id, options = {})
end
alias :all :all_webhooks

# Creates a webhook in the account.
#
# @see https://developer.dnsimple.com/v2/webhooks/#create
#
# @param [Fixnum] account_id the account ID
# @param [Hash] attributes
# @param [Hash] options
# @return [Dnsimple::Response<Dnsimple::Struct::Webhook>]
#
# @raise [Dnsimple::RequestError]
def create_webhook(account_id, attributes = {}, options = {})
Extra.validate_mandatory_attributes(attributes, [:url])
options = options.merge(attributes)
response = client.post(Client.versioned("/%s/webhooks" % [account_id]), options)

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

end
end
end
28 changes: 28 additions & 0 deletions spec/dnsimple/client/webhooks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,32 @@
end
end

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

before do
stub_request(:post, %r[/v2/#{account_id}/webhooks$])
.to_return(read_http_fixture("createWebhook/created.http"))
end

let(:attributes) { {url: "https://webhook.test"} }

it "builds the correct request" do
subject.create_webhook(account_id, attributes)

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

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

result = response.data
expect(result).to be_a(Dnsimple::Struct::Webhook)
expect(result.id).to be_a(Fixnum)
end
end

end
17 changes: 17 additions & 0 deletions spec/fixtures.http/createWebhook/created.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
HTTP/1.1 201 Created
Server: nginx
Date: Mon, 15 Feb 2016 17:04:38 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 201 Created
X-RateLimit-Limit: 4000
X-RateLimit-Remaining: 3996
X-RateLimit-Reset: 1455559348
ETag: W/"a4b43ee926d18d0839f1eae08e78c66b"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: dca89281-416a-4766-9428-d0295f58586e
X-Runtime: 0.175179
Strict-Transport-Security: max-age=31536000

{"data":{"id":1,"url":"https://webhook.test"}}

0 comments on commit 4cc5f8b

Please sign in to comment.