Skip to content

Commit

Permalink
Merge f597eac into 4dcb03f
Browse files Browse the repository at this point in the history
  • Loading branch information
weppos committed Dec 15, 2014
2 parents 4dcb03f + f597eac commit be8a2f0
Show file tree
Hide file tree
Showing 39 changed files with 1,374 additions and 488 deletions.
41 changes: 36 additions & 5 deletions lib/dnsimple/client.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
require 'dnsimple/version'
require 'dnsimple/compatibility'
require 'dnsimple/client/client_service'
require 'dnsimple/client/contacts_service'
require 'dnsimple/client/name_servers_service'
require 'dnsimple/client/records_service'
require 'dnsimple/client/services_service'

module Dnsimple

Expand Down Expand Up @@ -47,6 +52,8 @@ def initialize(options = {})
Dnsimple::Default.keys.each do |key|
instance_variable_set(:"@#{key}", options[key] || defaults[key])
end

@services = {}
end


Expand Down Expand Up @@ -96,13 +103,37 @@ def delete(path, options = {})
def request(method, path, options)
response = HTTParty.send(method, api_endpoint + path, base_options.merge(options))

if response.code == 401 && response.headers[HEADER_OTP_TOKEN] == "required"
raise TwoFactorAuthenticationRequired, response["message"]
elsif response.code == 401
raise AuthenticationFailed, response["message"]
case response.code
when 200..299
response
when 401
raise (response.headers[HEADER_OTP_TOKEN] == "required" ? TwoFactorAuthenticationRequired : AuthenticationFailed), response["message"]
when 404
raise RecordNotFound.new(response)
else
raise RequestError(response)
end
end


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

# @return [Dnsimple::Client::NameServersService] The name server-related API proxy.
def name_servers
@services[:name_servers] ||= Client::NameServersService.new(self)
end

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

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


Expand Down
15 changes: 15 additions & 0 deletions lib/dnsimple/client/client_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Dnsimple
class Client

class ClientService < Struct.new(:client)
private

def validate_mandatory_attributes(attributes, required)
required.each do |name|
attributes.key?(name) or raise(ArgumentError, ":#{name} is required")
end
end
end

end
end
82 changes: 82 additions & 0 deletions lib/dnsimple/client/contacts_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
module Dnsimple
class Client
class ContactsService < ClientService

# Lists the contacts in the account.
#
# @see http://developer.dnsimple.com/contacts/#list
#
# @return [Array<Record>]
# @raise [RequestError] When the request fails.
def list
response = client.get("v1/contacts")

response.map { |r| Contact.new(r["contact"]) }
end

# Creates a contact in the account.
#
# @see http://developer.dnsimple.com/contacts/#create
#
# @param [Hash] attributes
#
# @return [Contact]
# @raise [RequestError] When the request fails.
def create(attributes = {})
validate_mandatory_attributes(attributes, [:first_name, :last_name, :address1, :city, :state_province, :postal_code, :country, :phone, :email_address])
options = { body: { contact: attributes }}
response = client.post("v1/contacts", options)

Contact.new(response["contact"])
end

# Gets a contact from the account.
#
# @see http://developer.dnsimple.com/contacts/#get
#
# @param [Fixnum] contact The contact id.
#
# @return [Contact]
# @raise [RecordNotFound]
# @raise [RequestError] When the request fails.
def find(contact)
response = client.get("v1/contacts/#{contact}")

Contact.new(response["contact"])
end

# Updates a contact in the account.
#
# @see http://developer.dnsimple.com/contacts/#update
#
# @param [Fixnum] contact The contact id.
# @param [Hash] attributes
#
# @return [Contact]
# @raise [RecordNotFound]
# @raise [RequestError] When the request fails.
def update(contact, attributes = {})
options = { body: { contact: attributes }}
response = client.put("v1/contacts/#{contact}", options)

Contact.new(response["contact"])
end

# Deletes a contact from the account.
#
# WARNING: this cannot be undone.
#
# @see http://developer.dnsimple.com/contacts/#delete
#
# @param [Fixnum] contact The contact id.
#
# @return [void]
# @raise [RecordNotFound]
# @raise [RequestError] When the request fails.
def delete(contact)
client.delete("v1/contacts/#{contact}")
end

end
end
end
38 changes: 38 additions & 0 deletions lib/dnsimple/client/name_servers_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Dnsimple
class Client
class NameServersService < ClientService

# Lists the name servers for a domain.
#
# @see http://developer.dnsimple.com/domains/nameservers/#list
#
# @param [#to_s] domain The domain id or domain name.
#
# @return [Array<String>] The delegates name servers.
# @raise [RecordNotFound]
# @raise [RequestError] When the request fails.
def list(domain)
response = client.get("v1/domains/#{domain}/name_servers")
response.parsed_response
end

# Changes the name servers for a domain.
#
# @see http://developer.dnsimple.com/domains/nameservers/#change
#
# @param [#to_s] domain The domain id or domain name.
# @param [Array<String>] servers The name server list.
#
# @return [Array<String>] The delegates name servers.
# @raise [RecordNotFound]
# @raise [RequestError] When the request fails.
def change(domain, servers)
servers = servers.inject({}) { |hash, server| hash.merge("ns#{hash.length + 1}" => server) }
options = { body: { name_servers: servers } }
response = client.post("v1/domains/#{domain}/name_servers", options)
response.parsed_response
end

end
end
end
91 changes: 91 additions & 0 deletions lib/dnsimple/client/records_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
module Dnsimple
class Client
class RecordsService < ClientService

# Lists the records in the account.
#
# @see http://developer.dnsimple.com/domains/records/#list
#
# @param [#to_s] domain The domain id or domain name.
# @param [Hash] options
#
# @return [Array<Record>]
# @raise [RecordNotFound]
# @raise [RequestError] When the request fails.
def list(domain, options = {})
response = client.get("v1/domains/#{domain}/records", options)

response.map { |r| Record.new(r["record"]) }
end

# Creates a record in the account.
#
# @see http://developer.dnsimple.com/domains/records/#create
#
# @param [#to_s] domain The domain id or domain name.
# @param [Hash] attributes
#
# @return [Record]
# @raise [RecordNotFound]
# @raise [RequestError] When the request fails.
def create(domain, attributes = {})
validate_mandatory_attributes(attributes, [:name, :record_type, :content])
options = { body: { record: attributes }}
response = client.post("v1/domains/#{domain}/records", options)

Record.new(response["record"])
end

# Gets a record from the account.
#
# @see http://developer.dnsimple.com/domains/records/#get
#
# @param [#to_s] domain The domain id or domain name.
# @param [Fixnum] record The record id.
#
# @return [Record]
# @raise [RecordNotFound]
# @raise [RequestError] When the request fails.
def find(domain, record)
response = client.get("v1/domains/#{domain}/records/#{record}")

Record.new(response["record"])
end

# Updates a record in the account.
#
# @see http://developer.dnsimple.com/domains/records/#update
#
# @param [#to_s] domain The domain id or domain name.
# @param [Fixnum] record The record id.
# @param [Hash] attributes
#
# @return [Record]
# @raise [RecordNotFound]
# @raise [RequestError] When the request fails.
def update(domain, record, attributes = {})
options = { body: { record: attributes }}
response = client.put("v1/domains/#{domain}/records/#{record}", options)

Record.new(response["record"])
end

# Deletes a record from the account.
#
# WARNING: this cannot be undone.
#
# @see http://developer.dnsimple.com/domains/records/#delete
#
# @param [#to_s] domain The domain id or domain name.
# @param [Fixnum] record The record id.
#
# @return [void]
# @raise [RecordNotFound]
# @raise [RequestError] When the request fails.
def delete(domain, record)
client.delete("v1/domains/#{domain}/records/#{record}")
end

end
end
end
95 changes: 95 additions & 0 deletions lib/dnsimple/client/services_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
module Dnsimple
class Client
class ServicesService < ClientService

# Lists the supported services.
#
# @see http://developer.dnsimple.com/services/#list
#
# @return [Array<Service>]
# @raise [RequestError] When the request fails.
def list
response = client.get("v1/services")

response.map { |r| Service.new(r["service"]) }
end

# Gets a service.
#
# @see http://developer.dnsimple.com/services/#get
#
# @param [Fixnum] service The service id.
#
# @return [Service]
# @raise [RecordNotFound]
# @raise [RequestError] When the request fails.
def find(service)
response = client.get("v1/services/#{service}")

Service.new(response["service"])
end

# List the services applied to a domain.
#
# @see http://developer.dnsimple.com/services/#applied
#
# @param [#to_s] domain The domain id or domain name.
#
# @return [Array<Service>]
# @raise [RecordNotFound]
# @raise [RequestError] When the request fails.
def applied(domain)
response = client.get("v1/domains/#{domain}/applied_services")

response.map { |r| Service.new(r["service"]) }
end

# List the services not applied to a domain.
#
# @see http://developer.dnsimple.com/services/#available
#
# @param [#to_s] domain The domain id or domain name.
#
# @return [Array<Service>]
# @raise [RecordNotFound]
# @raise [RequestError] When the request fails.
def available(domain)
response = client.get("v1/domains/#{domain}/available_services")

response.map { |r| Service.new(r["service"]) }
end

# Apply a service to a domain.
#
# @see http://developer.dnsimple.com/services/#apply
#
# @param [#to_s] domain The domain id or domain name.
# @param [Fixnum] service The service id.
#
# @return [void]
# @raise [RecordNotFound]
# @raise [RequestError] When the request fails.
def apply(domain, service)
options = { body: { service: { id: service }}}
response = client.post("v1/domains/#{domain}/applied_services", options)
response.code == 200
end

# Un-apply a service from a domain.
#
# @see http://developer.dnsimple.com/services/#unapply
#
# @param [#to_s] domain The domain id or domain name.
# @param [Fixnum] service The service id.
#
# @return [void]
# @raise [RecordNotFound]
# @raise [RequestError] When the request fails.
def unapply(domain, service)
response = client.delete("v1/domains/#{domain}/applied_services/#{service}")
response.code == 200
end

end
end
end
Loading

0 comments on commit be8a2f0

Please sign in to comment.