-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic support for domain listing
- Loading branch information
Showing
7 changed files
with
162 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Dnsimple | ||
class Client | ||
|
||
# @return [Dnsimple::Client::DomainsService] The domain-related API proxy. | ||
def domains | ||
@services[:domains] ||= Client::DomainsService.new(self) | ||
end | ||
|
||
|
||
class ClientService < ::Struct.new(:client) | ||
end | ||
|
||
|
||
require_relative 'domains' | ||
|
||
class DomainsService < ClientService | ||
include Client::Domains | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module Dnsimple | ||
class Client | ||
module Domains | ||
|
||
# Lists the domains in the account. | ||
# | ||
# @see https://developer.dnsimple.com/v2/domains/#list | ||
# | ||
# @example List domains in the first page | ||
# client.domains(1010) | ||
# | ||
# @example List domains, provide a specific page | ||
# client.domains(1010, query: { page: 2 }) | ||
# | ||
# @param [Fixnum] account_id the account ID | ||
# @param [Hash] options the filtering and sorting option | ||
# @return [Array<Struct::Domain>] | ||
# @raise [RequestError] When the request fails. | ||
def domains(account_id, options = {}) | ||
response = client.get(Client.versioned("/%s/domains" % [account_id]), options) | ||
|
||
PaginatedResponse.new(response, response["data"].map { |r| Struct::Domain.new(r) }) | ||
end | ||
alias :list :domains | ||
alias :list_domains :domains | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module Dnsimple | ||
|
||
# The PaginatedResponse wraps a standard collection response to expose the information about the pagination. | ||
# | ||
# This object behaves as an Array. In fact, it inherits from Array. | ||
class PaginatedResponse < Array | ||
|
||
# @return [Fixnum] The current page. | ||
attr_reader :page | ||
|
||
# @return [Fixnum] The number of records per page. | ||
attr_reader :per_page | ||
|
||
# @return [Fixnum] The total number of records. | ||
attr_reader :total_entries | ||
|
||
# @return [Fixnum] The total number of pages. | ||
attr_reader :total_pages | ||
|
||
|
||
# Initializes a new paginated response from the response metadata, | ||
# and with given collection. | ||
# | ||
# @param [Hash] metadata the content of the "pagination" node in the JSON response | ||
# @param [Array] collection the enumerable collection of records returned in the response | ||
def initialize(response, collection) | ||
super().concat(collection) | ||
|
||
pagination = response["pagination"] | ||
@page = pagination["current_page"] | ||
@per_page = pagination["per_page"] | ||
@total_entries = pagination["total_entries"] | ||
@total_pages = pagination["total_pages"] | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require 'spec_helper' | ||
|
||
describe Dnsimple::Client, ".domains" do | ||
|
||
subject { described_class.new(api_endpoint: "https://api.dnsimple.test", oauth_access_token: "a1b2c3").domains } | ||
|
||
|
||
describe "#domains" do | ||
let(:account_id) { 1010 } | ||
|
||
before do | ||
stub_request(:get, %r[/v2/1010/domains]) | ||
.to_return(read_fixture("domains/domains/success.http")) | ||
end | ||
|
||
it "builds the correct request" do | ||
subject.domains(account_id) | ||
|
||
expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/domains") | ||
.with(headers: { 'Accept' => 'application/json' }) | ||
end | ||
|
||
it "supports pagination" do | ||
subject.domains(account_id, query: { page: 2 }) | ||
|
||
expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/domains?page=2") | ||
end | ||
|
||
it "returns the domains" do | ||
results = subject.domains(account_id) | ||
|
||
expect(results).to be_a(Array) | ||
expect(results.size).to eq(2) | ||
|
||
results.each do |result| | ||
expect(result).to be_a(Dnsimple::Struct::Domain) | ||
expect(result.id).to be_a(Fixnum) | ||
end | ||
end | ||
|
||
it "exposes the pagination information" do | ||
results = subject.domains(account_id) | ||
|
||
expect(results.respond_to?(:page)).to be_truthy | ||
expect(results.page).to eq(1) | ||
expect(results.per_page).to be_a(Fixnum) | ||
expect(results.total_entries).to be_a(Fixnum) | ||
expect(results.total_pages).to be_a(Fixnum) | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,18 @@ | ||
HTTP/1.1 200 OK | ||
Server: nginx | ||
Date: Mon, 15 Dec 2014 20:27:04 GMT | ||
Date: Wed, 16 Dec 2015 13:36:11 GMT | ||
Content-Type: application/json; charset=utf-8 | ||
Transfer-Encoding: chunked | ||
Connection: keep-alive | ||
Status: 200 OK | ||
Strict-Transport-Security: max-age=631138519 | ||
X-Frame-Options: SAMEORIGIN | ||
X-XSS-Protection: 1 | ||
X-Content-Type-Options: nosniff | ||
Access-Control-Allow-Origin: * | ||
Access-Control-Allow-Headers: Authorization,Accepts,Content-Type,X-DNSimple-Token,X-DNSimple-Domain-Token,X-CSRF-Token,x-requested-with | ||
Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS | ||
ETag: "882c85c0756e35625681ceb5262b0fe6" | ||
Strict-Transport-Security: max-age=31536000 | ||
X-RateLimit-Limit: 4000 | ||
X-RateLimit-Remaining: 3997 | ||
X-RateLimit-Reset: 1450272970 | ||
ETag: W/"2679531e6cce6cd326f255255d7a0005" | ||
Cache-Control: max-age=0, private, must-revalidate | ||
X-Request-Id: 9a8e4510-d956-4a94-8a76-dd86669afef4 | ||
X-Runtime: 0.395985 | ||
Strict-Transport-Security: max-age=315360000 | ||
X-Request-Id: a87f1b44-150a-4ed0-b7da-9301fa1465b0 | ||
X-Runtime: 0.093714 | ||
Strict-Transport-Security: max-age=31536000 | ||
|
||
[{"domain":{"id":1,"user_id":21,"registrant_id":321,"name":"example.com","unicode_name":"example.com","token":"domain-token","state":"registered","language":null,"lockable":false,"auto_renew":true,"whois_protected":false,"record_count":5,"service_count":1,"expires_on":"2015-09-27","created_at":"2012-09-27T14:25:57.646Z","updated_at":"2014-12-15T20:27:04.552Z"}},{"domain":{"id":2,"user_id":21,"registrant_id":321,"name":"example.com","unicode_name":"example.it","token":"domain-token","state":"hosted","language":null,"lockable":false,"auto_renew":true,"whois_protected":false,"record_count":5,"service_count":1,"expires_on":null,"created_at":"2012-09-27T14:25:57.646Z","updated_at":"2014-12-15T20:27:04.552Z"}}] | ||
{"data":[{"id":1745,"account_id":24,"registrant_id":409,"name":"example-1417881397.com","unicode_name":"example-1417881397.com","token":"domain-token","state":"expired","language":null,"auto_renew":false,"private_whois":false,"expires_on":"2015-12-06","created_at":"2014-12-06T15:56:55.573Z","updated_at":"2015-12-09T00:20:56.056Z"},{"id":1744,"account_id":24,"registrant_id":409,"name":"example-1417880719.com","unicode_name":"example-1417880719.com","token":"domain-token","state":"expired","language":null,"auto_renew":false,"private_whois":false,"expires_on":"2015-12-06","created_at":"2014-12-06T15:46:52.411Z","updated_at":"2015-12-09T00:20:53.572Z"}],"pagination":{"current_page":1,"per_page":30,"total_entries":2,"total_pages":1}} |