Skip to content

Commit

Permalink
Use a slightly more rubyish approach to naming
Browse files Browse the repository at this point in the history
    client.domains.list (alias for client.domains.domains)
    client.domains.find -> client.domains.domain
    client.domains.records (was client.domains.list_records)
    client.domains.record (was client.domains.find_record)
    client.domains.create_record
    etc
  • Loading branch information
weppos committed Jan 17, 2015
1 parent 8b4e796 commit d3b958b
Show file tree
Hide file tree
Showing 36 changed files with 127 additions and 138 deletions.
6 changes: 4 additions & 2 deletions lib/dnsimple/client/certificates.rb
Expand Up @@ -12,12 +12,14 @@ module Certificates
# @return [Array<Struct::Certificate>]
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def list(domain, options = {})
def certificates(domain, options = {})
response = client.get("v1/domains/#{domain}/certificates", options)

response.map { |r| Struct::Certificate.new(r["certificate"]) }
end

alias :list :certificates

# Gets a certificate for a domain.
#
# @see http://developer.dnsimple.com/domains/certificates/#get
Expand All @@ -28,7 +30,7 @@ def list(domain, options = {})
# @return [Struct::Certificate]
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def find(domain, certificate_id)
def certificate(domain, certificate_id)
response = client.get("v1/domains/#{domain}/certificates/#{certificate_id}")

Struct::Certificate.new(response["certificate"])
Expand Down
6 changes: 4 additions & 2 deletions lib/dnsimple/client/contacts.rb
Expand Up @@ -8,12 +8,14 @@ module Contacts
#
# @return [Array<Struct::Contact>]
# @raise [RequestError] When the request fails.
def list
def contacts
response = client.get("v1/contacts")

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

alias :list :contacts

# Creates a contact in the account.
#
# @see http://developer.dnsimple.com/contacts/#create
Expand All @@ -39,7 +41,7 @@ def create(attributes = {})
# @return [Struct::Contact]
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def find(contact)
def contact(contact)
response = client.get("v1/contacts/#{contact}")

Struct::Contact.new(response["contact"])
Expand Down
6 changes: 4 additions & 2 deletions lib/dnsimple/client/domains.rb
Expand Up @@ -8,12 +8,14 @@ module Domains
#
# @return [Array<Struct::Domain>]
# @raise [RequestError] When the request fails.
def list(options = {})
def domains(options = {})
response = client.get("v1/domains", options)

response.map { |r| Struct::Domain.new(r["domain"]) }
end

alias :list :domains

# Gets a domain from the account.
#
# @see http://developer.dnsimple.com/domains/#get
Expand All @@ -23,7 +25,7 @@ def list(options = {})
# @return [Struct::Domain]
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def find(domain)
def domain(domain)
response = client.get("v1/domains/#{domain}")

Struct::Domain.new(response["domain"])
Expand Down
4 changes: 2 additions & 2 deletions lib/dnsimple/client/domains_forwards.rb
Expand Up @@ -11,7 +11,7 @@ module DomainsForwards
# @return [Array<Struct::EmailForward>]
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def list_email_forwards(domain)
def email_forwards(domain)
response = client.get("v1/domains/#{domain}/email_forwards")

response.map { |r| Struct::EmailForward.new(r["email_forward"]) }
Expand Down Expand Up @@ -45,7 +45,7 @@ def create_email_forward(domain, attributes = {})
# @return [Struct::EmailForward]
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def find_email_forward(domain, forward)
def email_forward(domain, forward)
response = client.get("v1/domains/#{domain}/email_forwards/#{forward}")

Struct::EmailForward.new(response["email_forward"])
Expand Down
4 changes: 2 additions & 2 deletions lib/dnsimple/client/domains_records.rb
Expand Up @@ -12,7 +12,7 @@ module DomainsRecords
# @return [Array<Struct::Record>]
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def list_records(domain, options = {})
def records(domain, options = {})
response = client.get("v1/domains/#{domain}/records", options)

response.map { |r| Struct::Record.new(r["record"]) }
Expand Down Expand Up @@ -46,7 +46,7 @@ def create_record(domain, attributes = {})
# @return [Struct::Record]
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def find_record(domain, record)
def record(domain, record)
response = client.get("v1/domains/#{domain}/records/#{record}")

Struct::Record.new(response["record"])
Expand Down
2 changes: 1 addition & 1 deletion lib/dnsimple/client/domains_sharing.rb
Expand Up @@ -11,7 +11,7 @@ module DomainsSharing
# @return [Array<Struct::Membership>]
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def list_memberships(domain)
def memberships(domain)
response = client.get("v1/domains/#{domain}/memberships")

response.map { |r| Struct::Membership.new(r["membership"]) }
Expand Down
4 changes: 3 additions & 1 deletion lib/dnsimple/client/name_servers.rb
Expand Up @@ -11,12 +11,14 @@ module NameServers
# @return [Array<String>] The delegates name servers.
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def list(domain)
def name_servers(domain)
response = client.get("v1/domains/#{domain}/name_servers")

response.parsed_response
end

alias :list :name_servers

# Changes the name servers for a domain.
#
# @see http://developer.dnsimple.com/domains/nameservers/#change
Expand Down
6 changes: 4 additions & 2 deletions lib/dnsimple/client/services.rb
Expand Up @@ -8,12 +8,14 @@ module Services
#
# @return [Array<Struct::Service>]
# @raise [RequestError] When the request fails.
def list
def services
response = client.get("v1/services")

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

alias :list :services

# Gets a service.
#
# @see http://developer.dnsimple.com/services/#get
Expand All @@ -23,7 +25,7 @@ def list
# @return [Struct::Service]
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def find(service)
def service(service)
response = client.get("v1/services/#{service}")

Struct::Service.new(response["service"])
Expand Down
6 changes: 4 additions & 2 deletions lib/dnsimple/client/templates.rb
Expand Up @@ -8,12 +8,14 @@ module Templates
#
# @return [Array<Struct::Template>]
# @raise [RequestError] When the request fails.
def list
def templates
response = client.get("v1/templates")

response.map { |r| Struct::Template.new(r["dns_template"]) }
end

alias :list :templates

# Creates a template in the account.
#
# @see http://developer.dnsimple.com/templates/#create
Expand All @@ -39,7 +41,7 @@ def create(attributes = {})
# @return [Struct::Template]
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def find(template)
def template(template)
response = client.get("v1/templates/#{template}")

Struct::Template.new(response["dns_template"])
Expand Down
4 changes: 2 additions & 2 deletions lib/dnsimple/client/templates_records.rb
Expand Up @@ -11,7 +11,7 @@ module TemplatesRecords
# @return [Array<Struct::TemplateRecord>]
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def list_records(template)
def records(template)
response = client.get("v1/templates/#{template}/records")

response.map { |r| Struct::TemplateRecord.new(r["dns_template_record"]) }
Expand Down Expand Up @@ -45,7 +45,7 @@ def create_record(template, attributes = {})
# @return [Struct::TemplateRecord]
# @raise [NotFoundError]
# @raise [RequestError] When the request fails.
def find_record(template, record)
def record(template, record)
response = client.get("v1/templates/#{template}/records/#{record}")

Struct::TemplateRecord.new(response["dns_template_record"])
Expand Down
20 changes: 10 additions & 10 deletions spec/dnsimple/client/certificates_spec.rb
Expand Up @@ -5,21 +5,21 @@
subject { described_class.new(api_endpoint: "https://api.zone", username: "user", api_token: "token").certificates }


describe "#list" do
describe "#certificates" do
before do
stub_request(:get, %r[/v1/domains/.+/certificates$]).
to_return(read_fixture("certificates/index/success.http"))
to_return(read_fixture("certificates/list/success.http"))
end

it "builds the correct request" do
subject.list("example.com")
subject.certificates("example.com")

expect(WebMock).to have_requested(:get, "https://api.zone/v1/domains/example.com/certificates").
with(headers: { 'Accept' => 'application/json' })
end

it "returns the records" do
results = subject.list("example.com")
results = subject.certificates("example.com")

expect(results).to be_a(Array)
expect(results.size).to eq(2)
Expand All @@ -36,27 +36,27 @@
to_return(read_fixture("domains/notfound.http"))

expect {
subject.list("example.com")
subject.certificates("example.com")
}.to raise_error(Dnsimple::NotFoundError)
end
end
end

describe "#find" do
describe "#certificate" do
before do
stub_request(:get, %r[/v1/domains/.+/certificates/.+$]).
to_return(read_fixture("certificates/show/success.http"))
to_return(read_fixture("certificates/get/success.http"))
end

it "builds the correct request" do
subject.find("example.com", 2)
subject.certificate("example.com", 2)

expect(WebMock).to have_requested(:get, "https://api.zone/v1/domains/example.com/certificates/2").
with(headers: { 'Accept' => 'application/json' })
end

it "returns the certificate" do
result = subject.find("example.com", 2)
result = subject.certificate("example.com", 2)

expect(result).to be_a(Dnsimple::Struct::Certificate)
expect(result.id).to eq(4576)
Expand All @@ -80,7 +80,7 @@
to_return(read_fixture("certificates/notfound.http"))

expect {
subject.find("example.com", 2)
subject.certificate("example.com", 2)
}.to raise_error(Dnsimple::NotFoundError)
end
end
Expand Down
36 changes: 18 additions & 18 deletions spec/dnsimple/client/contacts_spec.rb
Expand Up @@ -5,24 +5,24 @@
subject { described_class.new(api_endpoint: "https://api.zone", username: "user", api_token: "token").contacts }


describe "#list" do
describe "#contacts" do
before do
stub_request(:get, %r[/v1/contacts$]).
to_return(read_fixture("contacts/index/success.http"))
to_return(read_fixture("contacts/list/success.http"))
end

it "builds the correct request" do
subject.list
subject.contacts

expect(WebMock).to have_requested(:get, "https://api.zone/v1/contacts").
with(headers: { 'Accept' => 'application/json' })
end

it "returns the contacts" do
results = subject.list
results = subject.contacts

expect(results).to be_a(Array)
expect(results.size).to eq(1)
expect(results.size).to eq(2)

results.each do |result|
expect(result).to be_a(Dnsimple::Struct::Contact)
Expand All @@ -33,7 +33,7 @@

describe "#create" do
before do
stub_request(:post, %r[/v1/contacts]).
stub_request(:post, %r[/v1/contacts$]).
to_return(read_fixture("contacts/create/created.http"))
end

Expand All @@ -55,40 +55,40 @@
end
end

describe "#find" do
describe "#contact" do
before do
stub_request(:get, %r[/v1/contacts/.+$]).
to_return(read_fixture("contacts/show/success.http"))
to_return(read_fixture("contacts/get/success.http"))
end

it "builds the correct request" do
subject.find(1)
subject.contact(1)

expect(WebMock).to have_requested(:get, "https://api.zone/v1/contacts/1").
with(headers: { 'Accept' => 'application/json' })
end

it "returns the contact" do
result = subject.find(1)
result = subject.contact(1)

expect(result).to be_a(Dnsimple::Struct::Contact)
expect(result.id).to eq(2)
expect(result.id).to eq(1)
expect(result.label).to eq("Default")
expect(result.first_name).to eq("Simone")
expect(result.last_name).to eq("Carletti")
expect(result.job_title).to eq("Underwater Programmer")
expect(result.organization_name).to eq("Dnsimple")
expect(result.email_address).to eq("example@example.com")
expect(result.phone).to eq("+1 111 000000")
expect(result.fax).to eq("+1 222 000000")
expect(result.organization_name).to eq("DNSimple")
expect(result.email_address).to eq("simone.carletti@dnsimple.com")
expect(result.phone).to eq("+1 111 4567890")
expect(result.fax).to eq("+1 222 4567890")
expect(result.address1).to eq("Awesome Street")
expect(result.address2).to eq("c/o Someone")
expect(result.city).to eq("Rome")
expect(result.state_province).to eq("RM")
expect(result.postal_code).to eq("00171")
expect(result.country).to eq("IT")
expect(result.created_at).to eq("2013-11-08T17:23:15Z")
expect(result.updated_at).to eq("2013-11-08T17:23:15Z")
expect(result.created_at).to eq("2014-01-15T22:08:07.390Z")
expect(result.updated_at).to eq("2014-01-15T22:08:07.390Z")
end

context "when something does not exist" do
Expand All @@ -97,7 +97,7 @@
to_return(read_fixture("contacts/notfound.http"))

expect {
subject.find(1)
subject.contact(1)
}.to raise_error(Dnsimple::NotFoundError)
end
end
Expand Down

0 comments on commit d3b958b

Please sign in to comment.