Skip to content

Commit

Permalink
Add getContact
Browse files Browse the repository at this point in the history
  • Loading branch information
weppos committed Jan 19, 2016
1 parent a5d0ec4 commit de1268f
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
17 changes: 17 additions & 0 deletions lib/dnsimple/client/contacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ def create_contact(account_id, attributes = {}, options = {})
end
alias :create :create_contact

# Gets a contact from the account.
#
# @see https://developer.dnsimple.com/v2/contacts/#get
#
# @param [Fixnum, Dnsimple::Client::WILDCARD_ACCOUNT] account_id the account ID or wildcard
# @param [#to_s] contact_id The contact.
# @param [Hash] options
# @return [Dnsimple::Response<Dnsimple::Struct::Contact>]
#
# @raise [Dnsimple::NotFoundError]
# @raise [Dnsimple::RequestError]
def contact(account_id, contact_id, options = {})
response = client.get(Client.versioned("/%s/contacts/%s" % [account_id, contact_id]), options)

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

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

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

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

it "builds the correct request" do
subject.contact(account_id, contact = 1)

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

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

result = response.data
expect(result).to be_a(Dnsimple::Struct::Contact)
expect(result.id).to eq(1)
expect(result.account_id).to eq(1010)
expect(result.created_at).to eq("2016-01-19T20:50:26.066Z")
expect(result.updated_at).to eq("2016-01-19T20:50:26.066Z")
end

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

expect {
subject.contact(account_id, 0)
}.to raise_error(Dnsimple::NotFoundError)
end
end
end

end
2 changes: 1 addition & 1 deletion spec/fixtures.http/createContact/created.http
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ 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"}}
{"data":{"id":1,"account_id":1010,"label":"Default","first_name":"First","last_name":"User","job_title":"CEO","organization_name":"Awesome Company","email_address":"first@example.com","phone":"+18001234567","fax":"+18011234567","address1":"Italian Street, 10","address2":"","city":"Roma","state_province":"RM","postal_code":"00100","country":"IT","created_at":"2016-01-19T20:50:26.066Z","updated_at":"2016-01-19T20:50:26.066Z"}}
17 changes: 17 additions & 0 deletions spec/fixtures.http/getContact/success.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 19 Jan 2016 20:57:38 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 200 OK
X-RateLimit-Limit: 4000
X-RateLimit-Remaining: 3996
X-RateLimit-Reset: 1453239045
ETag: W/"165299b0ea3e5c1c80f1ae622146626f"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 4c0679ed-5c79-41bf-84cb-0dc2250a07ce
X-Runtime: 0.127802
Strict-Transport-Security: max-age=31536000

{"data":{"id":1,"account_id":1010,"label":"Default","first_name":"First","last_name":"User","job_title":"CEO","organization_name":"Awesome Company","email_address":"first@example.com","phone":"+18001234567","fax":"+18011234567","address1":"Italian Street, 10","address2":"","city":"Roma","state_province":"RM","postal_code":"00100","country":"IT","created_at":"2016-01-19T20:50:26.066Z","updated_at":"2016-01-19T20:50:26.066Z"}}
12 changes: 12 additions & 0 deletions spec/fixtures.http/notfound-contact.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
HTTP/1.1 404 Not Found
Server: nginx
Date: Tue, 19 Jan 2016 21:04:48 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 404 Not Found
Cache-Control: no-cache
X-Request-Id: a57d4e23-3155-477e-8469-e897b27c03e5
X-Runtime: 0.014159

{"message":"Contact `0` not found"}

0 comments on commit de1268f

Please sign in to comment.