Skip to content

Commit

Permalink
Merge 4ff67e1 into 8f9f511
Browse files Browse the repository at this point in the history
  • Loading branch information
aeden committed Feb 4, 2016
2 parents 8f9f511 + 4ff67e1 commit beb9260
Show file tree
Hide file tree
Showing 10 changed files with 399 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/dnsimple/client/clients.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ class ContactsService < ClientService


require_relative 'domains'
require_relative 'domains_email_forwards'

class DomainsService < ClientService
include Client::Domains
include Client::DomainsEmailForwards
end


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

# Lists the email forwards for the domain.
#
# @see https://developer.dnsimple.com/v2/domains/email-forwards/#list
#
# @example List email forwards in the first page
# client.domains.email_forwards(1010, "example.com")
#
# @example List email forwards, provide a specific page
# client.domains.email_forwards(1010, "example.com", query: { page: 2 })
#
# @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 the filtering and sorting option
# @return [Dnsimple::PaginatedResponse<Dnsimple::Struct::EmailForward>]
#
# @raise [Dnsimple::RequestError]
def email_forwards(account_id, domain_id, options = {})
response = client.get(Client.versioned("/%s/domains/%s/email_forwards" % [account_id, domain_id]), options)

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

# Lists ALL the email forwards for the domain.
#
# This method is similar to {#email_forwards}, 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/domains/email-forwards/#list
# @see #email_forwards
#
# @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 the filtering and sorting option
# @return [Dnsimple::CollectionResponse<Dnsimple::Struct::EmailForward>]
#
# @raise [Dnsimple::RequestError]
def all_email_forwards(account_id, domain_id, options = {})
paginate(:email_forwards, account_id, domain_id, options)
end

# Creates an email forward for the domain.
#
# @see https://developer.dnsimple.com/v2/domains/email-forwards/#create
#
# @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] attributes
# @param [Hash] options
# @return [Dnsimple::Response<Dnsimple::Struct::EmailForward>]
#
# @raise [Dnsimple::RequestError]
def create_email_forward(account_id, domain_id, attributes = {}, options = {})
Extra.validate_mandatory_attributes(attributes, [:from, :to])
options = options.merge(attributes)
response = client.post(Client.versioned("/%s/domains/%s/email_forwards" % [account_id, domain_id]), options)

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

# Gets a email forward for the domain.
#
# @see https://developer.dnsimple.com/v2/domains/email-forwards/#get
#
# @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 [Fixnum] email_forward_id The email forward id.
# @param [Hash] options
# @return [Dnsimple::Response<Dnsimple::Struct::EmailForward>]
#
# @raise [Dnsimple::NotFoundError]
# @raise [Dnsimple::RequestError]
def email_forward(account_id, domain_id, email_forward_id, options = {})
response = client.get(Client.versioned("/%s/domains/%s/email_forwards/%s" % [account_id, domain_id, email_forward_id]), options)

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

# Deletes an email forward for the domain.
#
# WARNING: this cannot be undone.
#
# @see https://developer.dnsimple.com/v2/domains/email-forwards/#delete
#
# @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 [Fixnum] email_forward_id The email forward id
# @param [Hash] options
# @return [Dnsimple::Response<nil>]
#
# @raise [Dnsimple::NotFoundError]
# @raise [Dnsimple::RequestError]
def delete_email_forward(account_id, domain_id, email_forward_id, options = {})
response = client.delete(Client.versioned("/%s/domains/%s/email_forwards/%s" % [account_id, domain_id, email_forward_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 @@ -16,6 +16,7 @@ def initialize(attributes = {})
require_relative 'struct/account'
require_relative 'struct/contact'
require_relative 'struct/domain'
require_relative 'struct/email_forward'
require_relative 'struct/record'
require_relative 'struct/user'
require_relative 'struct/zone'
24 changes: 24 additions & 0 deletions lib/dnsimple/struct/email_forward.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Dnsimple
module Struct

class EmailForward < Base
# @return [Fixnum] The email forward ID in DNSimple.
attr_accessor :id

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

# @return [String] The "local part" of the originating email address. Anything to the left of the @ symbol.
attr_accessor :from

# @return [String] The full email address to forward to.
attr_accessor :to

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

# @return [String] Then the email forward was last updated in DNSimple.
attr_accessor :updated_at
end
end
end
190 changes: 190 additions & 0 deletions spec/dnsimple/client/domains_email_forwards_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
require 'spec_helper'

describe Dnsimple::Client, ".domains" do

subject { described_class.new(base_url: "https://api.dnsimple.test", access_token: "a1b2c3").domains }


describe "#email_forwards" do
let(:account_id) { 1010 }
let(:domain_id) { "example.com" }

before do
stub_request(:get, %r[/v2/#{account_id}/domains/#{domain_id}/email_forwards])
.to_return(read_http_fixture("listEmailForwards/success.http"))
end

it "builds the correct request" do
subject.email_forwards(account_id, domain_id)

expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/domains/#{domain_id}/email_forwards")
.with(headers: { 'Accept' => 'application/json' })
end

it "supports pagination" do
subject.email_forwards(account_id, domain_id, query: { page: 2 })

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

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

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

it "returns the email forwards" do
response = subject.email_forwards(account_id, domain_id)

expect(response).to be_a(Dnsimple::PaginatedResponse)
expect(response.data).to be_a(Array)
expect(response.data.size).to eq(2)

response.data.each do |result|
expect(result).to be_a(Dnsimple::Struct::EmailForward)
expect(result.id).to be_a(Fixnum)
end
end

it "exposes the pagination information" do
response = subject.email_forwards(account_id, domain_id)

expect(response.respond_to?(:page)).to be_truthy
expect(response.page).to eq(1)
expect(response.per_page).to be_a(Fixnum)
expect(response.total_entries).to be_a(Fixnum)
expect(response.total_pages).to be_a(Fixnum)
end

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

expect {
subject.email_forwards(account_id, domain_id)
}.to raise_error(Dnsimple::NotFoundError)
end
end
end

describe "#all_email_forwards" do
let(:account_id) { 1010 }
let(:domain_id) { "example.com" }

it "delegates to client.paginate" do
expect(subject).to receive(:paginate).with(:email_forwards, account_id, domain_id, { foo: "bar" })
subject.all_email_forwards(account_id, domain_id, { foo: "bar" })
end
end

describe "#create_email_forward" do
let(:account_id) { 1010 }
let(:domain_id) { "example.com" }

before do
stub_request(:post, %r[/v2/#{account_id}/domains/#{domain_id}/email_forwards$])
.to_return(read_http_fixture("createEmailForward/created.http"))
end

let(:attributes) { { from: "jim", to: "jim@another.com" } }

it "builds the correct request" do
subject.create_email_forward(account_id, domain_id, attributes)

expect(WebMock).to have_requested(:post, "https://api.dnsimple.test/v2/#{account_id}/domains/#{domain_id}/email_forwards")
.with(body: attributes)
.with(headers: { 'Accept' => 'application/json' })
end

it "returns the email forward" do
response = subject.create_email_forward(account_id, domain_id, attributes)
expect(response).to be_a(Dnsimple::Response)

result = response.data
expect(result).to be_a(Dnsimple::Struct::EmailForward)
expect(result.id).to be_a(Fixnum)
end
end

describe "#email_forward" do
let(:account_id) { 1010 }
let(:domain_id) { "example.com" }
let(:email_forward_id) { 17706 }

before do
stub_request(:get, %r[/v2/#{account_id}/domains/#{domain_id}/email_forwards.+$])
.to_return(read_http_fixture("getEmailForward/success.http"))
end

it "builds the correct request" do
subject.email_forward(account_id, domain_id, email_forward_id)

expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/domains/#{domain_id}/email_forwards/#{email_forward_id}")
.with(headers: { 'Accept' => 'application/json' })
end

it "returns the email forward" do
response = subject.email_forward(account_id, domain_id, email_forward_id)
expect(response).to be_a(Dnsimple::Response)

result = response.data
expect(result).to be_a(Dnsimple::Struct::EmailForward)
expect(result.id).to eq(17706)
expect(result.domain_id).to eq(228963)
expect(result.from).to eq("jim@a-domain.com")
expect(result.to).to eq("jim@another.com")
expect(result.created_at).to eq("2016-02-04T14:26:50.282Z")
expect(result.updated_at).to eq("2016-02-04T14:26:50.282Z")
end

context "when the email forward does not exist" do
it "raises NotFoundError" do
stub_request(:get, %r[/v2])
.to_return(read_http_fixture("notfound-emailforward.http"))

expect {
subject.email_forward(account_id, domain_id, email_forward_id)
}.to raise_error(Dnsimple::NotFoundError)
end
end
end

describe "#delete_email_forward" do
let(:account_id) { 1010 }
let(:domain_id) { "example.com" }
let(:email_forward_id) { 1 }

before do
stub_request(:delete, %r[/v2/#{account_id}/domains/#{domain_id}/email_forwards/#{email_forward_id}$])
.to_return(read_http_fixture("deleteEmailForward/success.http"))
end

it "builds the correct request" do
subject.delete_email_forward(account_id, domain_id, email_forward_id)

expect(WebMock).to have_requested(:delete, "https://api.dnsimple.test/v2/#{account_id}/domains/#{domain_id}/email_forwards/#{email_forward_id}")
.with(headers: { 'Accept' => 'application/json' })
end

it "returns nothing" do
response = subject.delete_email_forward(account_id, domain_id, email_forward_id)
expect(response).to be_a(Dnsimple::Response)

result = response.data
expect(result).to be_nil
end

context "when the email forward does not exist" do
it "raises NotFoundError" do
stub_request(:delete, %r[/v2])
.to_return(read_http_fixture("notfound-emailforward.http"))

expect {
subject.delete_email_forward(account_id, domain_id, email_forward_id)
}.to raise_error(Dnsimple::NotFoundError)
end
end
end

end
17 changes: 17 additions & 0 deletions spec/fixtures.http/createEmailForward/created.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
HTTP/1.1 201 Created
Server: nginx
Date: Thu, 04 Feb 2016 14:26:51 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 201 Created
X-RateLimit-Limit: 4000
X-RateLimit-Remaining: 3991
X-RateLimit-Reset: 1454596042
ETag: W/"10dd958c5a3a43eec0af1d8da655cab0"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: fca523a2-aad9-49e6-a828-a0e7711a8501
X-Runtime: 1.711621
Strict-Transport-Security: max-age=31536000

{"data":{"id":17706,"domain_id":228963,"from":"jim@a-domain.com","to":"jim@another.com","created_at":"2016-02-04T14:26:50.282Z","updated_at":"2016-02-04T14:26:50.282Z"}}
12 changes: 12 additions & 0 deletions spec/fixtures.http/deleteEmailForward/success.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
HTTP/1.1 204 No Content
Server: nginx
Date: Thu, 04 Feb 2016 17:14:52 GMT
Connection: keep-alive
Status: 204 No Content
X-RateLimit-Limit: 4000
X-RateLimit-Remaining: 3999
X-RateLimit-Reset: 1454609692
Cache-Control: no-cache
X-Request-Id: 716d181c-495d-47ab-ab79-391a70e8abe1
X-Runtime: 0.145208
Strict-Transport-Security: max-age=31536000
17 changes: 17 additions & 0 deletions spec/fixtures.http/getEmailForward/success.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 04 Feb 2016 14:42:46 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 200 OK
X-RateLimit-Limit: 4000
X-RateLimit-Remaining: 3999
X-RateLimit-Reset: 1454600566
ETag: W/"10dd958c5a3a43eec0af1d8da655cab0"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: fde15363-9332-4b91-bd8f-00b144eb8081
X-Runtime: 0.022117
Strict-Transport-Security: max-age=31536000

{"data":{"id":17706,"domain_id":228963,"from":"jim@a-domain.com","to":"jim@another.com","created_at":"2016-02-04T14:26:50.282Z","updated_at":"2016-02-04T14:26:50.282Z"}}
Loading

0 comments on commit beb9260

Please sign in to comment.