Skip to content

Commit

Permalink
Add createContact
Browse files Browse the repository at this point in the history
  • Loading branch information
weppos committed Jan 19, 2016
1 parent 7265238 commit a5d0ec4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/dnsimple/client/contacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ def all_contacts(account_id, options = {})
end
alias :all :all_contacts

# Creates a contact in the account.
#
# @see https://developer.dnsimple.com/v2/contacts/#create
#
# @param [Fixnum] account_id the account ID
# @param [Hash] attributes
# @param [Hash] options
# @return [Dnsimple::Response<Dnsimple::Struct::Contact>]
#
# @raise [Dnsimple::RequestError]
def create_contact(account_id, attributes = {}, options = {})
Extra.validate_mandatory_attributes(attributes, [:first_name, :last_name, :address1, :city, :state_province, :postal_code, :country, :phone, :email_address])
options = options.merge(attributes)
response = client.post(Client.versioned("/%s/contacts" % [account_id]), options)

Dnsimple::Response.new(response, Struct::Contact.new(response["data"]))
end
alias :create :create_contact

end
end
end
28 changes: 28 additions & 0 deletions spec/dnsimple/client/contacts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,32 @@
end
end

describe "#create_contact" do
let(:account_id) { 1010 }

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

let(:attributes) { { first_name: "Simone", last_name: "Carletti", address1: "Italian Street", city: "Rome", state_province: "RM", postal_code: "00171", country: "IT", email_address: "example@example.com", phone: "+393391234567" } }

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

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

it "returns the contact" do
response = subject.create_contact(account_id, attributes)
expect(response).to be_a(Dnsimple::Response)

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

end
17 changes: 17 additions & 0 deletions spec/fixtures.http/createContact/created.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
HTTP/1.1 201 Created
Server: nginx
Date: Tue, 19 Jan 2016 20:50:26 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 201 Created
X-RateLimit-Limit: 4000
X-RateLimit-Remaining: 3997
X-RateLimit-Reset: 1453239045
ETag: W/"165299b0ea3e5c1c80f1ae622146626f"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 9f577b9e-5bc4-4a8f-adfb-09dbb1992b0e
X-Runtime: 0.061482
Strict-Transport-Security: max-age=31536000

{"data":{"id":595,"account_id":24,"label":null,"first_name":"foo","last_name":"foo","job_title":null,"organization_name":null,"email_address":"foo@example.com","phone":"+393392551831","fax":null,"address1":"foo","address2":null,"city":"foo","state_province":"RM","postal_code":"00171","country":"00171","created_at":"2016-01-19T20:50:26.066Z","updated_at":"2016-01-19T20:50:26.066Z"}}

0 comments on commit a5d0ec4

Please sign in to comment.