Skip to content

Commit

Permalink
Add ContactsService
Browse files Browse the repository at this point in the history
  • Loading branch information
weppos committed Dec 14, 2014
1 parent 3210620 commit 2346be1
Show file tree
Hide file tree
Showing 17 changed files with 468 additions and 185 deletions.
7 changes: 7 additions & 0 deletions lib/dnsimple/client.rb
@@ -1,5 +1,7 @@
require 'dnsimple/version'
require 'dnsimple/compatibility'
require 'dnsimple/client/service'
require 'dnsimple/client/contacts_service'
require 'dnsimple/client/records_service'

module Dnsimple
Expand Down Expand Up @@ -110,6 +112,11 @@ def request(method, path, options)
end


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

# @return [Dnsimple::Client::RecordsService] The record-related API proxy.
def records
@records_service ||= Client::RecordsService.new(self)
Expand Down
82 changes: 82 additions & 0 deletions lib/dnsimple/client/contacts_service.rb
@@ -0,0 +1,82 @@
module Dnsimple
class Client
class ContactsService < Service

# 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 the 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 specific contact in 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 the 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 specific 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
11 changes: 1 addition & 10 deletions lib/dnsimple/client/records_service.rb
@@ -1,6 +1,6 @@
module Dnsimple
class Client
class RecordsService < Struct.new(:client)
class RecordsService < Service

# Lists the domain records in the account.
#
Expand Down Expand Up @@ -86,15 +86,6 @@ def delete(domain, record)
client.delete("v1/domains/#{domain}/records/#{record}")
end


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
15 changes: 15 additions & 0 deletions lib/dnsimple/client/service.rb
@@ -0,0 +1,15 @@
module Dnsimple
class Client

class Service < 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
153 changes: 28 additions & 125 deletions lib/dnsimple/contact.rb
@@ -1,157 +1,60 @@
module Dnsimple

# Represents a contact.
class Contact < Base

Aliases = {
'first' => 'first_name',
'last' => 'last_name',
'state' => 'state_province',
'province' => 'state_province',
'state_or_province' => 'state_province',
'email' => 'email_address',
}

# The contact ID in DNSimple
# @return [Symbol] The contact ID in DNSimple.
attr_accessor :id

# The contact first name
# @return [Symbol] The label to represent the contact.
attr_accessor :label

# @return [Symbol] The contact first name.
attr_accessor :first_name

# The contact last name
# @return [Symbol] The contact last name.
attr_accessor :last_name

# The contact's job title
# @return [Symbol] The contact's job title.
attr_accessor :job_title

# The name of the organization in which the contact works
# @return [Symbol] The name of the organization in which the contact works.
attr_accessor :organization_name

# The contact email address
attr_accessor :email_address

# The contact phone number
attr_accessor :phone

# The contact phone extension (may be omitted)
attr_accessor :phone_ext

# The contact fax number (may be omitted)
attr_accessor :fax

# The contact street address
# @return [Symbol] The contact street address.
attr_accessor :address1

# Apartment or suite number
# @return [Symbol] Apartment or suite number.
attr_accessor :address2

# The city name
# @return [Symbol] The city name.
attr_accessor :city

# The state or province name
# @return [Symbol] The state or province name.
attr_accessor :state_province

# The contact postal code
# @return [Symbol] The contact postal code.
attr_accessor :postal_code

# The contact country (as a 2-character country code)
# @return [Symbol] The contact country (as a 2-character country code).
attr_accessor :country

# When the contact was created in DNSimple
attr_accessor :created_at
# @return [Symbol] The contact phone number.
attr_accessor :phone

# When the contact was last updated in DNSimple
attr_accessor :updated_at
# @return [Symbol] The contact fax number (may be omitted).
attr_accessor :fax

# @return [Symbol] The contact email address.
attr_accessor :email_address

# Map an aliased field name to it's real name. For example, if you
# pass "first" it will be resolved to "first_name", "email" is resolved
# to "email_address" and so on.
def self.resolve(name)
Contact::Aliases[name.to_s] || name
end

def self.resolve_attributes(attributes)
resolved_attributes = {}
attributes.each do |k, v|
resolved_attributes[resolve(k)] = v
end
resolved_attributes
end

# Create the contact with the given attributes in DNSimple.
# This method returns a Contact instance of the contact is created
# and raises an error otherwise.
def self.create(attributes, options={})
contact_hash = resolve_attributes(attributes)

options.merge!({:body => {:contact => contact_hash}})
response = Client.post("v1/contacts", options)

case response.code
when 201
new(response["contact"])
else
raise RequestError.new("Error creating contact", response)
end
end

def self.find(id, options={})
response = Client.get("v1/contacts/#{id}", options)

case response.code
when 200
new(response["contact"])
when 404
raise RecordNotFound, "Could not find contact #{id}"
else
raise RequestError.new("Error finding contact", response)
end
end

def self.all(options={})
response = Client.get("v1/contacts", options)

case response.code
when 200
response.map { |r| new(r["contact"]) }
else
raise RequestError.new("Error listing contacts", response)
end
end


def name
[first_name, last_name].join(' ')
end

def save(options={})
contact_hash = {}
%w(first_name last_name organization_name job_title address1 address2 city
state_province postal_code country email_address phone phone_ext fax).each do |attribute|
contact_hash[Contact.resolve(attribute)] = self.send(attribute)
end

options.merge!({:body => {:contact => contact_hash}})

response = Client.put("v1/contacts/#{id}", options)

case response.code
when 200
return self
else
raise RequestError.new("Error updating contact", response)
end
end

# #delete the contact from DNSimple.
#
# WARNING: this cannot be undone.
#
def delete(options={})
Client.delete("v1/contacts/#{id}", options)
end
alias :destroy :delete
# @return [Symbol] When the contact was created in DNSimple.
attr_accessor :created_at

# @return [Symbol] When the contact was last updated in DNSimple.
attr_accessor :updated_at

alias :email :email_address
alias :email= :email_address=
end

end

0 comments on commit 2346be1

Please sign in to comment.