Skip to content

Commit

Permalink
Add basic support for domain listing
Browse files Browse the repository at this point in the history
  • Loading branch information
weppos committed Dec 16, 2015
2 parents cc4de78 + 4459c55 commit 93f5914
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 23 deletions.
2 changes: 2 additions & 0 deletions lib/dnsimple/client.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'dnsimple/extra'
require 'dnsimple/struct'
require 'dnsimple/paginated_response'
require 'dnsimple/client/clients'

module Dnsimple

Expand Down
21 changes: 21 additions & 0 deletions lib/dnsimple/client/clients.rb
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
29 changes: 29 additions & 0 deletions lib/dnsimple/client/domains.rb
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
38 changes: 38 additions & 0 deletions lib/dnsimple/paginated_response.rb
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
20 changes: 10 additions & 10 deletions lib/dnsimple/struct/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ class Domain < Base
# @return [Fixnum] The domain ID in DNSimple.
attr_accessor :id

# @return [Fixnum] The associated user ID.
attr_accessor :user_id
# @return [Fixnum] The associated account ID.
attr_accessor :account_id

# @return [Fixnum] The associated registrant (contact) ID.
attr_accessor :registrant_id

# The String name.
# @return [String] The domain name.
attr_accessor :name

# The String state.
attr_accessor :state

# The String API token
# @return [String] The domain API token used for domain authentication.
attr_accessor :token

# Is the domain set to auto renew?
# @return [String] The domain state.
attr_accessor :state

# @return [Bool] True if the domain is set to auto-renew, false otherwise.
attr_accessor :auto_renew

# Is the whois information protected?
attr_accessor :whois_protected
# @return [Bool] True if the domain WHOIS privacy is enabled, false otherwise.
attr_accessor :private_whois

# @return [String] The date the domain will expire.
attr_accessor :expires_on
Expand Down
52 changes: 52 additions & 0 deletions spec/dnsimple/client/domains_spec.rb
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
23 changes: 10 additions & 13 deletions spec/files/domains/domains/success.http
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}}

0 comments on commit 93f5914

Please sign in to comment.