diff --git a/lib/dnsimple/client/webhooks.rb b/lib/dnsimple/client/webhooks.rb index 7b03efb8..7c15c7c3 100644 --- a/lib/dnsimple/client/webhooks.rb +++ b/lib/dnsimple/client/webhooks.rb @@ -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] + # + # @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 diff --git a/spec/dnsimple/client/webhooks_spec.rb b/spec/dnsimple/client/webhooks_spec.rb index 12d95ff2..19c570ce 100644 --- a/spec/dnsimple/client/webhooks_spec.rb +++ b/spec/dnsimple/client/webhooks_spec.rb @@ -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 diff --git a/spec/fixtures.http/createWebhook/created.http b/spec/fixtures.http/createWebhook/created.http new file mode 100644 index 00000000..51fa840f --- /dev/null +++ b/spec/fixtures.http/createWebhook/created.http @@ -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"}}