Skip to content

Commit

Permalink
Merge 5dd2d13 into 54ad1a4
Browse files Browse the repository at this point in the history
  • Loading branch information
duduribeiro committed Apr 13, 2021
2 parents 54ad1a4 + 5dd2d13 commit e95d59e
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

This project uses [Semantic Versioning 2.0.0](http://semver.org/).

## main

- NEW: Added `registrar.get_domain_prices` to retrieve whether a domain is premium and the prices to register, transfer, and renew. (dnsimple/dnsimple-ruby#230)

## 5.2.0

- CHANGED: `Certificate#expires_on` (date only) is deprecated in favor of `Certificate#expires_at` (timestamp). (dnsimple/dnsimple-ruby#190)
Expand Down
20 changes: 20 additions & 0 deletions lib/dnsimple/client/registrar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ def domain_premium_price(account_id, domain_name, options = {})
Dnsimple::Response.new(response, Struct::DomainPremiumPrice.new(response["data"]))
end

# Get prices for a domain.
# @see https://developer.dnsimple.com/v2/registrar/#getDomainPrices
#
# @example Check prices for example.com:
# client.registrar.get_domain_prices(1010, "example.com")
#
# @param [Integer] account_id the Account id
# @param [String] domain_name the domain name to find the prices
# @param [Hash] options
#
# @return [Struct::DomainPrice]
#
# @raise [RequestError] When the request fails.
def get_domain_prices(account_id, domain_name, options = {})
endpoint = Client.versioned("/%s/registrar/domains/%s/prices" % [account_id, domain_name])
response = client.get(endpoint, options)

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

# Registers a domain.
#
# @see https://developer.dnsimple.com/v2/registrar/#register
Expand Down
1 change: 1 addition & 0 deletions lib/dnsimple/struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def initialize(attributes = {})
require_relative 'struct/domain'
require_relative 'struct/domain_check'
require_relative 'struct/domain_premium_price'
require_relative 'struct/domain_price'
require_relative 'struct/domain_push'
require_relative 'struct/domain_registration'
require_relative 'struct/domain_transfer'
Expand Down
24 changes: 24 additions & 0 deletions lib/dnsimple/struct/domain_price.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Dnsimple
module Struct

class DomainPrice < Base
# @return [String] The domain name
attr_accessor :domain

# @return [Boolean] Whether the domain is premium.
attr_accessor :premium

# @return [Float] The price for registration
attr_accessor :registration_price

# @return [Float] The price for renewal
attr_accessor :renewal_price

# @return [Float] The price for transfer
attr_accessor :transfer_price
end

end
end
43 changes: 43 additions & 0 deletions spec/dnsimple/client/registrar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,49 @@
end
end

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

before do
stub_request(:get, %r{/v2/#{account_id}/registrar/domains/bingo.pizza/prices$})
.to_return(read_http_fixture("getDomainPrices/success.http"))
end

it "builds the correct request" do
subject.get_domain_prices(account_id, "bingo.pizza")

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

it "returns the prices" do
response = subject.get_domain_prices(account_id, "bingo.pizza")
expect(response).to be_a(Dnsimple::Response)

result = response.data

expect(result).to be_a(Dnsimple::Struct::DomainPrice)
expect(result.domain).to eq("bingo.pizza")
expect(result.premium).to be(true)
expect(result.registration_price).to eq(20.0)
expect(result.renewal_price).to eq(20.0)
expect(result.transfer_price).to eq(20.0)
end

context "when the TLD is not supported" do
before do
stub_request(:get, %r{/v2/#{account_id}/registrar/domains/bingo.pineapple/prices$})
.to_return(read_http_fixture("getDomainPrices/failure.http"))
end

it "raises error" do
expect {
subject.get_domain_prices(account_id, "bingo.pineapple")
}.to raise_error(Dnsimple::RequestError, "TLD .PINEAPPLE is not supported")
end
end
end

describe "#register_domain" do
let(:account_id) { 1010 }
let(:attributes) { { registrant_id: "10" } }
Expand Down
20 changes: 20 additions & 0 deletions spec/fixtures.http/getDomainPrices/failure.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
HTTP/1.1 400 Bad Request
Server: nginx
Date: Mon, 08 Mar 2021 14:35:58 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: identity
Connection: keep-alive
X-RateLimit-Limit: 2400
X-RateLimit-Remaining: 2396
X-RateLimit-Reset: 1615217645
Cache-Control: no-cache
X-Request-Id: e414a674-63bb-4e54-b714-db5b516bb190
X-Runtime: 0.009579
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none
Content-Security-Policy: frame-ancestors 'none'

{"message":"TLD .PINEAPPLE is not supported"}
22 changes: 22 additions & 0 deletions spec/fixtures.http/getDomainPrices/success.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 08 Mar 2021 14:35:26 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: identity
Connection: keep-alive
X-RateLimit-Limit: 2400
X-RateLimit-Remaining: 2397
X-RateLimit-Reset: 1615217645
ETag: W/"2104f27f2877f429295359cfc409f9f7"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: b0d9e000-58a6-4254-af43-8735d26e12d9
X-Runtime: 9.129301
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none
Content-Security-Policy: frame-ancestors 'none'
Strict-Transport-Security: max-age=31536000

{"data":{"domain":"bingo.pizza","premium":true,"registration_price":20.0,"renewal_price":20.0,"transfer_price":20.0}}

0 comments on commit e95d59e

Please sign in to comment.