Skip to content

Commit

Permalink
Implemented listWebhooks()
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Mar 3, 2016
1 parent 3dfce79 commit bd1ea00
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/dnsimple/client/clients.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def zones
@services[:zones] ||= Client::ZonesService.new(self)
end

# @return [Dnsimple::Client::WebhooksService] The webhooks-related API proxy.
def webhooks
@services[:webhooks] ||= Client::WebhooksService.new(self)
end


class ClientService < ::Struct.new(:client)

Expand Down Expand Up @@ -110,5 +115,11 @@ class ZonesService < ClientService
include Client::ZonesRecords
end

require_relative 'webhooks'

class WebhooksService < ClientService
include Client::Webhooks
end

end
end
47 changes: 47 additions & 0 deletions lib/dnsimple/client/webhooks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Dnsimple
class Client
module Webhooks

# Lists the webhooks in the account.
#
# @see https://developer.dnsimple.com/v2/webhooks/#list
# @see #all_webhooks
#
# @example List all webhooks
# client.webhooks.list(1010)
#
# @param [Fixnum] account_id the account ID
# @param [Hash] options the filtering and sorting option
# @return [Dnsimple::CollectionResponse<Dnsimple::Struct::Webhook>]
#
# @raise [Dnsimple::RequestError]
def webhooks(account_id, options = {})
response = client.get(Client.versioned("/%s/webhooks" % [account_id]), options)

Dnsimple::CollectionResponse.new(response, response["data"].map { |r| Struct::Webhook.new(r) })
end
alias :list :webhooks
alias :list_webhooks :webhooks

# Lists ALL the webhooks in the account.
#
# This is equal to {#webhooks}.
# We have implemented both methods to have a consistent design with other
# resources that implements pagination.
#
# @see https://developer.dnsimple.com/v2/webhooks/#list
# @see #webhooks
#
# @param [Fixnum] account_id the account ID
# @param [Hash] options the filtering and sorting option
# @return [Dnsimple::CollectionResponse<Dnsimple::Struct::Webhook>]
#
# @raise [Dnsimple::RequestError]
def all_webhooks(account_id, options = {})
webhooks(account_id, options)
end
alias :all :all_webhooks

end
end
end
1 change: 1 addition & 0 deletions lib/dnsimple/struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ def initialize(attributes = {})
require_relative 'struct/record'
require_relative 'struct/user'
require_relative 'struct/zone'
require_relative 'struct/webhook'
13 changes: 13 additions & 0 deletions lib/dnsimple/struct/webhook.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Dnsimple
module Struct

class Webhook < Base
# @return [Fixnum] The contact ID in DNSimple.
attr_accessor :id

# @return [String] The callback URL.
attr_accessor :url
end

end
end
51 changes: 51 additions & 0 deletions spec/dnsimple/client/webhooks_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'spec_helper'

describe Dnsimple::Client, ".webhooks" do

subject { described_class.new(base_url: "https://api.dnsimple.test", access_token: "a1b2c3").webhooks }

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

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

it "builds the correct request" do
subject.webhooks(account_id)

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

it "supports extra request options" do
subject.webhooks(account_id, query: { foo: "bar" })

expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/webhooks?foo=bar")
end

it "returns the webhooks" do
response = subject.webhooks(account_id)

expect(response).to be_a(Dnsimple::CollectionResponse)
expect(response.data).to be_a(Array)
expect(response.data.size).to eq(2)

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

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

it "delegates to client.webhooks" do
expect(subject).to receive(:webhooks).with(account_id, { foo: "bar" })
subject.all_webhooks(account_id, { foo: "bar" })
end
end

end
17 changes: 17 additions & 0 deletions spec/fixtures.http/listWebhooks/success.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 15 Feb 2016 17:06:21 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 200 OK
X-RateLimit-Limit: 4000
X-RateLimit-Remaining: 3991
X-RateLimit-Reset: 1455559348
ETag: W/"01f1ea26e8e06d8d969bf06678bf7d12"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: bc611cd0-d1a9-48d0-b450-c9c86f0d0dcf
X-Runtime: 0.104174
Strict-Transport-Security: max-age=31536000

{"data":[{"id":1,"url":"https://webhook.test"},{"id":2,"url":"https://another.test"}]}

0 comments on commit bd1ea00

Please sign in to comment.