Skip to content

Commit

Permalink
Add register()
Browse files Browse the repository at this point in the history
  • Loading branch information
weppos committed Jan 16, 2016
1 parent 578d4d1 commit ae795bd
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/dnsimple/client/clients.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
module Dnsimple
class Client

# @return [Dnsimple::Client::AuthService] The authentication-related API proxy.
def auth
@services[:auth] ||= Client::AuthService.new(self)
end

# @return [Dnsimple::Client::DomainsService] The domain-related API proxy.
def domains
@services[:domains] ||= Client::DomainsService.new(self)
end

# @return [Dnsimple::Client::AuthService] The authentication-methods API proxy.
def auth
@services[:auth] ||= Client::AuthService.new(self)
# @return [Dnsimple::Client::RegistrarService] The registrar-related API proxy.
def registrar
@services[:registrar] ||= Client::RegistrarService.new(self)
end

# @return [Dnsimple::Client::ZonesService] The zone-related API proxy.
Expand Down Expand Up @@ -48,17 +53,24 @@ def paginate(method, *args)
end


require_relative 'auth'

class AuthService < ClientService
include Client::Auth
end


require_relative 'domains'

class DomainsService < ClientService
include Client::Domains
end


require_relative 'auth'
require_relative 'registrar'

class AuthService < ClientService
include Client::Auth
class RegistrarService < ClientService
include Client::Registrar
end


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

# Registers a domain.
#
# @see https://developer.dnsimple.com/v2/registrar/#register
#
# @param [Fixnum] account_id the account ID
# @param [#to_s] domain_name The domain name to register.
# @param [Hash] attributes
# @param [Hash] options
# @return [Struct::Domain]
#
# @raise [RequestError] When the request fails.
def register(account_id, domain_name, attributes = {}, options = {})
Extra.validate_mandatory_attributes(attributes, [:registrant_id])
options = options.merge(attributes)
response = client.post(Client.versioned("/%s/registrar/%s/registration" % [account_id, domain_name]), options)

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

end
end
end
36 changes: 36 additions & 0 deletions spec/dnsimple/client/registrar_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'spec_helper'

describe Dnsimple::Client, ".registrar" do

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


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

before do
stub_request(:post, %r[/v2/#{account_id}/registrar/.+/registration$])
.to_return(read_http_fixture("register/success.http"))
end

let(:attributes) { { registrant_id: "10" } }

it "builds the correct request" do
subject.register(account_id, domain_name = "example.com", attributes)

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

it "returns the domain" do
response = subject.register(account_id, "example.com", attributes)
expect(response).to be_a(Dnsimple::Response)

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

end
17 changes: 17 additions & 0 deletions spec/fixtures.http/register/success.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
HTTP/1.1 201 Created
Server: nginx
Date: Sat, 16 Jan 2016 16:09:02 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 201 Created
X-RateLimit-Limit: 4000
X-RateLimit-Remaining: 3965
X-RateLimit-Reset: 1452960541
ETag: W/"e034ef9fb32487cf4dc034050ba846c5"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 8b378ee7-d8fa-4459-a1b0-a18bfd2354bb
X-Runtime: 13.365767
Strict-Transport-Security: max-age=31536000

{"data":{"id":1,"account_id":1010,"registrant_id":2,"name":"example.weppos","unicode_name":"example.weppos","token":"cc8h1jP8bDLw5rXycL16k8BivcGiT6K9","state":"registered","auto_renew":false,"private_whois":false,"expires_on":"2017-01-16","created_at":"2016-01-16T16:08:50.649Z","updated_at":"2016-01-16T16:09:01.161Z"}}

0 comments on commit ae795bd

Please sign in to comment.