Skip to content

Commit

Permalink
Provide an helper to apply the API version to the path
Browse files Browse the repository at this point in the history
  • Loading branch information
weppos committed Oct 23, 2015
1 parent d34a44b commit 4dfe5ff
Show file tree
Hide file tree
Showing 18 changed files with 72 additions and 60 deletions.
12 changes: 12 additions & 0 deletions lib/dnsimple/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ class Client
HEADER_EXCHANGE_TOKEN = "X-DNSimple-OTP-Token"


# @return [String] The current API version.
API_VERSION = "v1"


# Prepends the correct API version to +path+.
#
# @return [String] The versioned path.
def self.versioned(path)
File.join(API_VERSION, path)
end


# @!attribute api_endpoint
# @return [String] Base URL for API requests. (default: https://api.dnsimple.com/)
# @!attribute username
Expand Down
10 changes: 5 additions & 5 deletions lib/dnsimple/client/certificates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Certificates
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def certificates(domain, options = {})
response = client.get("v1/domains/#{domain}/certificates", options)
response = client.get(Client.versioned("/domains/#{domain}/certificates"), options)

response.map { |r| Struct::Certificate.new(r["certificate"]) }
end
Expand All @@ -31,7 +31,7 @@ def certificates(domain, options = {})
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def certificate(domain, certificate_id, options = {})
response = client.get("v1/domains/#{domain}/certificates/#{certificate_id}", options)
response = client.get(Client.versioned("/domains/#{domain}/certificates/#{certificate_id}"), options)

Struct::Certificate.new(response["certificate"])
end
Expand Down Expand Up @@ -60,7 +60,7 @@ def certificate(domain, certificate_id, options = {})
# @raise [RequestError] When the request fails.
def purchase(domain, name, contact_id, options = {})
options = Extra.deep_merge(options, { certificate: { name: name, contact_id: contact_id }})
response = client.post("v1/domains/#{domain}/certificates", options)
response = client.post(Client.versioned("/domains/#{domain}/certificates"), options)

Struct::Certificate.new(response["certificate"])
end
Expand All @@ -74,7 +74,7 @@ def purchase(domain, name, contact_id, options = {})
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def configure(domain, certificate_id, options = {})
response = client.put("v1/domains/#{domain}/certificates/#{certificate_id}/configure", options)
response = client.put(Client.versioned("/domains/#{domain}/certificates/#{certificate_id}/configure"), options)

Struct::Certificate.new(response["certificate"])
end
Expand All @@ -90,7 +90,7 @@ def configure(domain, certificate_id, options = {})
# @raise [RequestError] When the request fails.
def submit(domain, certificate_id, email, options = {})
options = options.merge(certificate: { approver_email: email })
response = client.put("v1/domains/#{domain}/certificates/#{certificate_id}/submit", options)
response = client.put(Client.versioned("/domains/#{domain}/certificates/#{certificate_id}/submit"), options)

Struct::Certificate.new(response["certificate"])
end
Expand Down
10 changes: 5 additions & 5 deletions lib/dnsimple/client/contacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Contacts
#
# @raise [RequestError] When the request fails.
def contacts(options = {})
response = client.get("v1/contacts", options)
response = client.get(Client.versioned("/contacts"), options)

response.map { |r| Struct::Contact.new(r["contact"]) }
end
Expand All @@ -28,7 +28,7 @@ def contacts(options = {})
def create_contact(attributes = {}, options = {})
Extra.validate_mandatory_attributes(attributes, [:first_name, :last_name, :address1, :city, :state_province, :postal_code, :country, :phone, :email_address])
options = options.merge(contact: attributes)
response = client.post("v1/contacts", options)
response = client.post(Client.versioned("/contacts"), options)

Struct::Contact.new(response["contact"])
end
Expand All @@ -44,7 +44,7 @@ def create_contact(attributes = {}, options = {})
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def contact(contact, options = {})
response = client.get("v1/contacts/#{contact}", options)
response = client.get(Client.versioned("/contacts/#{contact}"), options)

Struct::Contact.new(response["contact"])
end
Expand All @@ -61,7 +61,7 @@ def contact(contact, options = {})
# @raise [RequestError] When the request fails.
def update_contact(contact, attributes = {}, options = {})
options = options.merge(contact: attributes)
response = client.put("v1/contacts/#{contact}", options)
response = client.put(Client.versioned("/contacts/#{contact}"), options)

Struct::Contact.new(response["contact"])
end
Expand All @@ -79,7 +79,7 @@ def update_contact(contact, attributes = {}, options = {})
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def delete_contact(contact, options = {})
client.delete("v1/contacts/#{contact}", options)
client.delete(Client.versioned("contacts/#{contact}"), options)
end
alias :delete :delete_contact

Expand Down
8 changes: 4 additions & 4 deletions lib/dnsimple/client/domains.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Domains
#
# @raise [RequestError] When the request fails.
def domains(options = {})
response = client.get("v1/domains", options)
response = client.get(Client.versioned("/domains"), options)

response.map { |r| Struct::Domain.new(r["domain"]) }
end
Expand All @@ -29,7 +29,7 @@ def domains(options = {})
def create_domain(attributes = {}, options = {})
Extra.validate_mandatory_attributes(attributes, [:name])
options = options.merge({ domain: attributes })
response = client.post("v1/domains", options)
response = client.post(Client.versioned("/domains"), options)

Struct::Domain.new(response["domain"])
end
Expand All @@ -45,7 +45,7 @@ def create_domain(attributes = {}, options = {})
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def domain(domain, options = {})
response = client.get("v1/domains/#{domain}", options)
response = client.get(Client.versioned("/domains/#{domain}"), options)

Struct::Domain.new(response["domain"])
end
Expand All @@ -62,7 +62,7 @@ def domain(domain, options = {})
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def delete_domain(domain, options = {})
client.delete("v1/domains/#{domain}", options)
client.delete(Client.versioned("/domains/#{domain}"), options)
end
alias :delete :delete_domain

Expand Down
4 changes: 2 additions & 2 deletions lib/dnsimple/client/domains_autorenewal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module DomainsAutorenewal
#
# @raise [RequestError] When the request fails.
def enable_auto_renewal(domain, options = {})
response = client.post("v1/domains/#{domain}/auto_renewal", options)
response = client.post(Client.versioned("/domains/#{domain}/auto_renewal"), options)

Struct::Domain.new(response["domain"])
end
Expand All @@ -25,7 +25,7 @@ def enable_auto_renewal(domain, options = {})
#
# @raise [RequestError] When the request fails.
def disable_auto_renewal(domain, options = {})
response = client.delete("v1/domains/#{domain}/auto_renewal", options)
response = client.delete(Client.versioned("/domains/#{domain}/auto_renewal"), options)

Struct::Domain.new(response["domain"])
end
Expand Down
8 changes: 4 additions & 4 deletions lib/dnsimple/client/domains_forwards.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module DomainsForwards
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def email_forwards(domain, options = {})
response = client.get("v1/domains/#{domain}/email_forwards", options)
response = client.get(Client.versioned("/domains/#{domain}/email_forwards"), options)

response.map { |r| Struct::EmailForward.new(r["email_forward"]) }
end
Expand All @@ -31,7 +31,7 @@ def email_forwards(domain, options = {})
def create_email_forward(domain, attributes = {}, options = {})
Extra.validate_mandatory_attributes(attributes, [:from, :to])
options = options.merge({ email_forward: attributes })
response = client.post("v1/domains/#{domain}/email_forwards", options)
response = client.post(Client.versioned("/domains/#{domain}/email_forwards"), options)

Struct::EmailForward.new(response["email_forward"])
end
Expand All @@ -47,7 +47,7 @@ def create_email_forward(domain, attributes = {}, options = {})
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def email_forward(domain, forward, options = {})
response = client.get("v1/domains/#{domain}/email_forwards/#{forward}", options)
response = client.get(Client.versioned("/domains/#{domain}/email_forwards/#{forward}"), options)

Struct::EmailForward.new(response["email_forward"])
end
Expand All @@ -63,7 +63,7 @@ def email_forward(domain, forward, options = {})
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def delete_email_forward(domain, forward, options = {})
client.delete("v1/domains/#{domain}/email_forwards/#{forward}", options)
client.delete(Client.versioned("/domains/#{domain}/email_forwards/#{forward}"), options)
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/dnsimple/client/domains_privacy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module DomainsPrivacy
#
# @raise [RequestError] When the request fails.
def enable_whois_privacy(domain, options = {})
response = client.post("v1/domains/#{domain}/whois_privacy", options)
response = client.post(Client.versioned("domains/#{domain}/whois_privacy"), options)

Struct::WhoisPrivacy.new(response["whois_privacy"])
end
Expand All @@ -25,7 +25,7 @@ def enable_whois_privacy(domain, options = {})
#
# @raise [RequestError] When the request fails.
def disable_whois_privacy(domain, options = {})
response = client.delete("v1/domains/#{domain}/whois_privacy", options)
response = client.delete(Client.versioned("domains/#{domain}/whois_privacy"), options)

Struct::WhoisPrivacy.new(response["whois_privacy"])
end
Expand Down
10 changes: 5 additions & 5 deletions lib/dnsimple/client/domains_records.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module DomainsRecords
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def records(domain, options = {})
response = client.get("v1/domains/#{domain}/records", options)
response = client.get(Client.versioned("domains/#{domain}/records"), options)

response.map { |r| Struct::Record.new(r["record"]) }
end
Expand All @@ -32,7 +32,7 @@ def records(domain, options = {})
def create_record(domain, attributes = {}, options = {})
Extra.validate_mandatory_attributes(attributes, [:name, :record_type, :content])
options = options.merge({ record: attributes })
response = client.post("v1/domains/#{domain}/records", options)
response = client.post(Client.versioned("domains/#{domain}/records"), options)

Struct::Record.new(response["record"])
end
Expand All @@ -48,7 +48,7 @@ def create_record(domain, attributes = {}, options = {})
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def record(domain, record, options = {})
response = client.get("v1/domains/#{domain}/records/#{record}", options)
response = client.get(Client.versioned("domains/#{domain}/records/#{record}"), options)

Struct::Record.new(response["record"])
end
Expand All @@ -66,7 +66,7 @@ def record(domain, record, options = {})
# @raise [RequestError] When the request fails.
def update_record(domain, record, attributes = {}, options = {})
options = options.merge({ record: attributes })
response = client.put("v1/domains/#{domain}/records/#{record}", options)
response = client.put(Client.versioned("domains/#{domain}/records/#{record}"), options)

Struct::Record.new(response["record"])
end
Expand All @@ -82,7 +82,7 @@ def update_record(domain, record, attributes = {}, options = {})
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def delete_record(domain, record, options = {})
client.delete("v1/domains/#{domain}/records/#{record}", options)
client.delete(Client.versioned("domains/#{domain}/records/#{record}"), options)
end

end
Expand Down
6 changes: 3 additions & 3 deletions lib/dnsimple/client/domains_sharing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module DomainsSharing
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def memberships(domain, options = {})
response = client.get("v1/domains/#{domain}/memberships", options)
response = client.get(Client.versioned("/domains/#{domain}/memberships"), options)

response.map { |r| Struct::Membership.new(r["membership"]) }
end
Expand All @@ -30,7 +30,7 @@ def memberships(domain, options = {})
# @raise [RequestError] When the request fails.
def create_membership(domain, email, options = {})
options = options.merge({ membership: { email: email }})
response = client.post("v1/domains/#{domain}/memberships", options)
response = client.post(Client.versioned("/domains/#{domain}/memberships"), options)

Struct::Membership.new(response["membership"])
end
Expand All @@ -46,7 +46,7 @@ def create_membership(domain, email, options = {})
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def delete_membership(domain, membership, options = {})
client.delete("v1/domains/#{domain}/memberships/#{membership}", options)
client.delete(Client.versioned("/domains/#{domain}/memberships/#{membership}"), options)
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/dnsimple/client/domains_zones.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module DomainsZones
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def zone(domain, options = {})
response = client.get("v1/domains/#{domain}/zone", options)
response = client.get(Client.versioned("/domains/#{domain}/zone"), options)

response["zone"]
end
Expand Down
8 changes: 4 additions & 4 deletions lib/dnsimple/client/name_servers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module NameServers
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def name_servers(domain, options = {})
response = client.get("v1/domains/#{domain}/name_servers", options)
response = client.get(Client.versioned("/domains/#{domain}/name_servers"), options)

response.parsed_response
end
Expand All @@ -32,7 +32,7 @@ def name_servers(domain, options = {})
def change(domain, servers, options = {})
servers = servers.inject({}) { |hash, server| hash.merge("ns#{hash.length + 1}" => server) }
options = options.merge({ name_servers: servers })
response = client.post("v1/domains/#{domain}/name_servers", options)
response = client.post(Client.versioned("/domains/#{domain}/name_servers"), options)

response.parsed_response
end
Expand All @@ -50,7 +50,7 @@ def change(domain, servers, options = {})
# @raise [RequestError] When the request fails.
def register(domain, name, ip, options = {})
options = options.merge({ name_server: { name: name, ip: ip } })
client.post("v1/domains/#{domain}/registry_name_servers", options)
client.post(Client.versioned("/domains/#{domain}/registry_name_servers"), options)
end

# De-registers a name server at the registry.
Expand All @@ -63,7 +63,7 @@ def register(domain, name, ip, options = {})
#
# @raise [RequestError] When the request fails.
def deregister(domain, name, options = {})
client.delete("v1/domains/#{domain}/registry_name_servers/#{name}", options)
client.delete(Client.versioned("/domains/#{domain}/registry_name_servers/#{name}"), options)
end

end
Expand Down
12 changes: 6 additions & 6 deletions lib/dnsimple/client/registrar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Registrar
# @raise [RequestError] When the request fails.
def check(name, options = {})
response = begin
client.get("v1/domains/#{name}/check", options)
client.get(Client.versioned("/domains/#{name}/check"), options)
rescue NotFoundError => e
e.response
end
Expand Down Expand Up @@ -45,7 +45,7 @@ def available?(name, options = {})
# @raise [RequestError] When the request fails.
def register(name, registrant_id, extended_attributes = {}, options = {})
options = Extra.deep_merge(options, { domain: { name: name, registrant_id: registrant_id }, extended_attribute: extended_attributes })
response = client.post("v1/domain_registrations", options)
response = client.post(Client.versioned("/domain_registrations"), options)

Struct::Domain.new(response["domain"])
end
Expand All @@ -64,7 +64,7 @@ def register(name, registrant_id, extended_attributes = {}, options = {})
# @raise [RequestError] When the request fails.
def transfer(name, auth_code, registrant_id, extended_attributes = {}, options = {})
options = Extra.deep_merge(options, { domain: { name: name, registrant_id: registrant_id }, extended_attribute: extended_attributes, transfer_order: { authinfo: auth_code }})
response = client.post("v1/domain_transfers", options)
response = client.post(Client.versioned("/domain_transfers"), options)

Struct::TransferOrder.new(response["transfer_order"])
end
Expand All @@ -80,7 +80,7 @@ def transfer(name, auth_code, registrant_id, extended_attributes = {}, options =
# @raise [RequestError] When the request fails.
def renew(name, options = {})
options = Extra.deep_merge(options, { domain: { name: name }})
response = client.post("v1/domain_renewals", options)
response = client.post(Client.versioned("/domain_renewals"), options)

Struct::Domain.new(response["domain"])
end
Expand All @@ -95,7 +95,7 @@ def renew(name, options = {})
#
# @raise [RequestError] When the request fails.
def extended_attributes(tld, options = {})
response = client.get("v1/extended_attributes/#{tld}", options)
response = client.get(Client.versioned("/extended_attributes/#{tld}"), options)

response.map { |r| Struct::ExtendedAttribute.new(r) }
end
Expand All @@ -109,7 +109,7 @@ def extended_attributes(tld, options = {})
#
# @raise [RequestError] When the request fails.
def prices(options = {})
response = client.get("v1/prices", options)
response = client.get(Client.versioned("/prices"), options)

response.map { |r| Struct::Price.new(r["price"]) }
end
Expand Down
Loading

0 comments on commit 4dfe5ff

Please sign in to comment.