Skip to content

Commit

Permalink
Merge pull request #70 from aetrion/domain-records
Browse files Browse the repository at this point in the history
API v2 Zone > Records methods
  • Loading branch information
weppos committed Jan 7, 2016
2 parents d8bac8e + 8e839c6 commit 838cbf9
Show file tree
Hide file tree
Showing 16 changed files with 606 additions and 25 deletions.
9 changes: 9 additions & 0 deletions lib/dnsimple/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ def put(path, options = {})
execute :put, path, options
end

# Make a HTTP PATCH request.
#
# @param [String] path The path, relative to {#api_endpoint}
# @param [Hash] options Body and header params for request
# @return [HTTParty::Response]
def patch(path, options = {})
execute :patch, path, options
end

# Make a HTTP DELETE request.
#
# @param [String] path The path, relative to {#api_endpoint}
Expand Down
12 changes: 12 additions & 0 deletions lib/dnsimple/client/clients.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ def misc
@services[:misc] ||= Client::MiscService.new(self)
end

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


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

Expand Down Expand Up @@ -56,5 +61,12 @@ class MiscService < ClientService
include Client::Misc
end


require_relative 'zones_records'

class ZonesService < ClientService
include Client::ZonesRecords
end

end
end
22 changes: 12 additions & 10 deletions lib/dnsimple/client/domains.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ module Domains
# @see #all_domains
#
# @example List domains in the first page
# client.domains(1010)
# client.domains.list(1010)
#
# @example List domains, provide a specific page
# client.domains(1010, query: { page: 2 })
# client.domains.list(1010, query: { page: 2 })
#
# @param [Fixnum] account_id the account ID
# @param [Hash] options the filtering and sorting option
# @return [Dnsimple::PaginatedResponse<Dnsimple::Struct::Domain>]
#
# @raise [Dnsimple::RequestError]
def domains(account_id, options = {})
response = client.get(Client.versioned("/%s/domains" % [account_id]), options)
Expand All @@ -39,6 +40,7 @@ def domains(account_id, options = {})
# @param [Fixnum] account_id the account ID
# @param [Hash] options the filtering and sorting option
# @return [Dnsimple::CollectionResponse<Dnsimple::Struct::Domain>]
#
# @raise [Dnsimple::RequestError]
def all_domains(account_id, options = {})
paginate(:domains, account_id, options)
Expand Down Expand Up @@ -68,15 +70,15 @@ def create_domain(account_id, attributes = {}, options = {})
#
# @see https://developer.dnsimple.com/v2/domains/#get
#
# @param [Fixnum] account_id the account ID
# @param [#to_s] domain The domain id or domain name.
# @param [Fixnum, Dnsimple::Client::WILDCARD_ACCOUNT] account_id the account ID or wildcard
# @param [#to_s] domain_id The domain id or domain name.
# @param [Hash] options
# @return [Dnsimple::Response<Dnsimple::Struct::Domain>]
#
# @raise [Dnsimple::NotFoundError]
# @raise [Dnsimple::RequestError]
def domain(account_id, domain, options = {})
response = client.get(Client.versioned("/%s/domains/%s" % [account_id, domain]), options)
def domain(account_id, domain_id, options = {})
response = client.get(Client.versioned("/%s/domains/%s" % [account_id, domain_id]), options)

Dnsimple::Response.new(response, Struct::Domain.new(response["data"]))
end
Expand All @@ -87,15 +89,15 @@ def domain(account_id, domain, options = {})
#
# @see https://developer.dnsimple.com/v2/domains/#delete
#
# @param [Fixnum] account_id the account ID
# @param [#to_s] domain The domain id or domain name.
# @param [Fixnum, Dnsimple::Client::WILDCARD_ACCOUNT] account_id the account ID or wildcard
# @param [#to_s] domain_id The domain id or domain name.
# @param [Hash] options
# @return [Dnsimple::Response<nil>]
#
# @raise [Dnsimple::NotFoundError]
# @raise [Dnsimple::RequestError]
def delete_domain(account_id, domain, options = {})
response = client.delete(Client.versioned("/%s/domains/%s" % [account_id, domain]), options)
def delete_domain(account_id, domain_id, options = {})
response = client.delete(Client.versioned("/%s/domains/%s" % [account_id, domain_id]), options)

Dnsimple::Response.new(response, nil)
end
Expand Down
129 changes: 129 additions & 0 deletions lib/dnsimple/client/zones_records.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
module Dnsimple
class Client
module ZonesRecords

# Lists the zone records in the account.
#
# @see https://developer.dnsimple.com/v2/zones/records/#list
# @see #all_records
#
# @example List records for the zone "example.com" in the first page
# client.zones.records(1010, "example.com")
#
# @example List records for the zone "example.com", provide a specific page
# client.zones.records(1010, "example.com", query: { page: 2 })
#
# @param [Fixnum, Dnsimple::Client::WILDCARD_ACCOUNT] account_id the account ID or wildcard
# @param [String] zone_id the zone name
# @param [Hash] options the filtering and sorting option
# @return [Dnsimple::PaginatedResponse<Dnsimple::Struct::Record>]
#
# @raise [Dnsimple::RequestError]
def records(account_id, zone_id, options = {})
response = client.get(Client.versioned("/%s/zones/%s/records" % [account_id, zone_id]), options)

Dnsimple::PaginatedResponse.new(response, response["data"].map { |r| Struct::Record.new(r) })
end
alias :list_records :records

# Lists ALL the zone records in the account.
#
# This method is similar to {#records}, but instead of returning the results of a specific page
# it iterates all the pages and returns the entire collection.
#
# Please use this method carefully, as fetching the entire collection will increase the number of requests
# you send to the API server and you may eventually risk to hit the throttle limit.
#
# @see https://developer.dnsimple.com/v2/zones/records/#list
# @see #records
#
# @param [Fixnum, Dnsimple::Client::WILDCARD_ACCOUNT] account_id the account ID or wildcard
# @param [String] zone_id the zone name
# @param [Hash] options the filtering and sorting option
# @return [Dnsimple::CollectionResponse<Dnsimple::Struct::Record>]
#
# @raise [Dnsimple::RequestError]
def all_records(account_id, zone_id, options = {})
paginate(:records, account_id, zone_id, options)
end

# Creates a zone record in the account.
#
# @see https://developer.dnsimple.com/v2/zones/records/#create
#
# @param [Fixnum, Dnsimple::Client::WILDCARD_ACCOUNT] account_id the account ID or wildcard
# @param [String] zone_id the zone name
# @param [Hash] attributes
# @param [Hash] options
# @return [Dnsimple::Response<Dnsimple::Struct::Record>]
#
# @raise [Dnsimple::RequestError]
def create_record(account_id, zone_id, attributes = {}, options = {})
Extra.validate_mandatory_attributes(attributes, [:record_type, :name, :content])
options = options.merge(attributes)
response = client.post(Client.versioned("/%s/zones/%s/records" % [account_id, zone_id]), options)

Dnsimple::Response.new(response, Struct::Record.new(response["data"]))
end

# Gets a zone record from the account.
#
# @see https://developer.dnsimple.com/v2/zones/records/#get
#
# @param [Fixnum, Dnsimple::Client::WILDCARD_ACCOUNT] account_id the account ID or wildcard
# @param [String] zone_id the zone name
# @param [Fixnum] record_id the record ID
# @param [Hash] options
# @return [Dnsimple::Response<Dnsimple::Struct::Domain>]
#
# @raise [Dnsimple::NotFoundError]
# @raise [Dnsimple::RequestError]
def record(account_id, zone_id, record_id, options = {})
response = client.get(Client.versioned("/%s/zones/%s/records/%s" % [account_id, zone_id, record_id]), options)

Dnsimple::Response.new(response, Struct::Record.new(response["data"]))
end

# Updates a zone record in the account.
#
# @see https://developer.dnsimple.com/v2/zones/records/#update
#
# @param [Fixnum, Dnsimple::Client::WILDCARD_ACCOUNT] account_id the account ID or wildcard
# @param [String] zone_id the zone name
# @param [Fixnum] record_id the record ID
# @param [Hash] attributes
# @param [Hash] options
# @return [Dnsimple::Response<Dnsimple::Struct::Record>]
#
# @raise [Dnsimple::NotFoundError]
# @raise [Dnsimple::RequestError]
def update_record(account_id, zone_id, record_id, attributes = {}, options = {})
options = options.merge(attributes)
response = client.patch(Client.versioned("/%s/zones/%s/records/%s" % [account_id, zone_id, record_id]), options)

Dnsimple::Response.new(response, Struct::Record.new(response["data"]))
end

# Deletes a zone record from the account.
#
# WARNING: this cannot be undone.
#
# @see https://developer.dnsimple.com/v2/zones/records/#delete
#
# @param [Fixnum, Dnsimple::Client::WILDCARD_ACCOUNT] account_id the account ID or wildcard
# @param [String] zone_id the zone name
# @param [Fixnum] record_id the record ID
# @param [Hash] options
# @return [Dnsimple::Response<nil>]
#
# @raise [Dnsimple::NotFoundError]
# @raise [Dnsimple::RequestError]
def delete_record(account_id, zone_id, record_id, options = {})
response = client.delete(Client.versioned("/%s/zones/%s/records/%s" % [account_id, zone_id, record_id]), options)

Dnsimple::Response.new(response, nil)
end

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 @@ -15,4 +15,5 @@ def initialize(attributes = {})

require_relative 'struct/account'
require_relative 'struct/domain'
require_relative 'struct/record'
require_relative 'struct/user'
40 changes: 40 additions & 0 deletions lib/dnsimple/struct/record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Dnsimple
module Struct

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

# @return [Fixnum] The associated domain ID.
attr_accessor :domain_id

# @return [Fixnum] The ID of the parent record, if this record is dependent on another record.
attr_accessor :parent_id

# @return [String] The type of record, in uppercase.
attr_accessor :type

# @return [String] The record name (without the domain name).
attr_accessor :name

# @return [String] The plain-text record content.
attr_accessor :content

# @return [Fixnum] The TTL value.
attr_accessor :ttl

# @return [Fixnum] The priority value, if the type of record accepts a priority.
attr_accessor :priority

# @return [Bool] True if this is a system record created by DNSimple. System records are read-only.
attr_accessor :system_record

# @return [String] When the domain was created in DNSimple.
attr_accessor :created_at

# @return [String] When the domain was last updated in DNSimple.
attr_accessor :updated_at
end

end
end
14 changes: 10 additions & 4 deletions spec/dnsimple/client/domains_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/domains?page=2")
end

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

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

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

Expand Down Expand Up @@ -120,10 +126,10 @@
expect(result.updated_at).to eq("2015-12-09T00:20:56.056Z")
end

context "when something does not exist" do
context "when the domain does not exist" do
it "raises NotFoundError" do
stub_request(:get, %r[/v2])
.to_return(read_fixture("domains/notfound-domain.http"))
.to_return(read_fixture("notfound-domain.http"))

expect {
subject.domain(account_id, "example.com")
Expand Down Expand Up @@ -155,10 +161,10 @@
expect(result).to be_nil
end

context "when something does not exist" do
context "when the domain does not exist" do
it "raises NotFoundError" do
stub_request(:delete, %r[/v2])
.to_return(read_fixture("domains/notfound-domain.http"))
.to_return(read_fixture("notfound-domain.http"))

expect {
subject.delete_domain(account_id, "example.com")
Expand Down
Loading

0 comments on commit 838cbf9

Please sign in to comment.