Skip to content

Commit

Permalink
Implement Templates.update_template
Browse files Browse the repository at this point in the history
  • Loading branch information
jacegu committed Apr 15, 2016
1 parent 2c0c9d6 commit bcbd5d7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/dnsimple/client/templates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,28 @@ def template(account_id, template_id, options = {})
Dnsimple::Response.new(response, Struct::Template.new(response["data"]))
end

# Updates template with specified ID with provided data.
#
# @see https://developer.dnsimple.com/v2/templates/#update
#
# @example Change the name of template 1 in account 1010:
# client.templates.update_template(1010, 1, name: "New name")
#
# @param [Fixnum] account_iduthe account ID
# @param [Fixnum] template_id the template ID
# @param [Hash] attributes
# @param [Hash] options
# @return [Dnsimple::Response<Dnsimple::Struct::Template>]
#
# @raise [RequestError] When the request fails.
def update_template(account_id, template_id, attributes, options = {})
endpoint = Client.versioned("/%s/templates/%s" % [account_id, template_id])
response = client.put(endpoint, attributes, options)

Dnsimple::Response.new(response, Struct::Template.new(response["data"]))
end
alias update update_template

# Deletes a template from the account.
#
# WARNING: this cannot be undone.
Expand Down
32 changes: 32 additions & 0 deletions spec/dnsimple/client/templates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,38 @@
end
end

describe "#update_template" do
let(:account_id) { 1010 }
let(:template_id) { 1 }

before do
stub_request(:put, %r{/v2/#{account_id}/templates/#{template_id}$}).
to_return(read_http_fixture("updateTemplate/success.http"))
end

let(:attributes) { { name: "Alpha", short_name: "alpha", description: "An alpha template." } }

it "builds the correct request" do
subject.update_template(account_id, template_id, attributes)

expect(WebMock).to have_requested(:put, "https://api.dnsimple.test/v2/#{account_id}/templates/#{template_id}").
with(headers: { "Accept" => "application/json" })
end

it "returns the list of templates" do
response = subject.update_template(account_id, template_id, attributes)
expect(response).to be_a(Dnsimple::Response)

template = response.data
expect(template).to be_a(Dnsimple::Struct::Template)
expect(template.id).to eq(1)
expect(template.account_id).to eq(1010)
expect(template.name).to eq("Alpha")
expect(template.short_name).to eq("alpha")
expect(template.description).to eq("An alpha template.")
end
end

describe "#delete_template" do
let(:account_id) { 1010 }
let(:template_id) { 5410 }
Expand Down

0 comments on commit bcbd5d7

Please sign in to comment.