class Fog::DNS::Dnsimple::Real

Public Class Methods

new(options = {}) click to toggle source
# File lib/fog/dnsimple/dns.rb, line 56
def initialize(options = {})
  @dnsimple_token = options[:dnsimple_token]
  @dnsimple_account = options[:dnsimple_account]

  if options[:dnsimple_url]
    uri = URI.parse(options[:dnsimple_url])
    options[:host]    = uri.host
    options[:port]    = uri.port
    options[:scheme]  = uri.scheme
  end

  connection_options = options[:connection_options] || {}
  connection_options[:headers] ||= {}
  connection_options[:headers]["User-Agent"] = "#{Fog::Core::Connection.user_agents} fog-dnsimple/#{Fog::Dnsimple::VERSION}"

  host       = options[:host]        || "api.dnsimple.com"
  persistent = options[:persistent]  || false
  port       = options[:port]        || 443
  scheme     = options[:scheme]      || "https"
  @connection = Fog::Core::Connection.new("#{scheme}://#{host}:#{port}", persistent, connection_options)
end

Public Instance Methods

create_domain(zone_name) click to toggle source

Create a single domain in DNSimple in your account.

Parameters

  • zone_name<~String> - zone name to host (ie example.com)

Returns

  • response<~Excon::Response>:

    • body<~Hash>:

      • “data”<~Hash> The representation of the domain.

# File lib/fog/dnsimple/requests/dns/create_domain.rb, line 14
def create_domain(zone_name)
  body = {
    "name" => zone_name
  }

  request(
    body:     Fog::JSON.encode(body),
    expects:  201,
    method:   "POST",
    path:     "/#{@dnsimple_account}/domains"
  )
end
create_record(zone_name, name, type, content, options = {}) click to toggle source

Create a new host in the specified zone

Parameters

  • zone_name<~String> - zone name

  • name<~String>

  • type<~String>

  • content<~String>

  • options<~Hash> - optional

    • priority<~Integer>

    • ttl<~Integer>

Returns

  • response<~Excon::Response>:

    • body<~Hash>:

      • 'record'<~Hash> The representation of the record.

# File lib/fog/dnsimple/requests/dns/create_record.rb, line 20
def create_record(zone_name, name, type, content, options = {})
  body = {
    "name" => name,
    "type" => type,
    "content" => content
  }
  body.merge!(options)

  request(
    body:     Fog::JSON.encode(body),
    expects:  201,
    method:   "POST",
    path:     "/#{@dnsimple_account}/zones/#{zone_name}/records"
  )
end
delete_domain(zone_name) click to toggle source

Delete the given domain from your account. You may use either the domain ID or the domain name.

Please note that for domains which are registered with DNSimple this will not delete the domain from the registry.

Parameters

  • account_id<~String> - the account the domain belong to

  • zone_name<~String> - zone name

# File lib/fog/dnsimple/requests/dns/delete_domain.rb, line 15
def delete_domain(zone_name)
  request(
    expects:  204,
    method:   "DELETE",
    path:     "/#{@dnsimple_account}/domains/#{zone_name}"
  )
end
delete_record(zone_name, record_id) click to toggle source

Delete the record with the given ID for the given domain.

Parameters

  • zone_name<~String> - zone name

  • record_id<~String>

# File lib/fog/dnsimple/requests/dns/delete_record.rb, line 10
def delete_record(zone_name, record_id)
  request(
    expects:  204,
    method:   "DELETE",
    path:     "/#{@dnsimple_account}/zones/#{zone_name}/records/#{record_id}"
  )
end
get_domain(zone_name) click to toggle source

Get the details for a specific domain in your account. You may pass either the domain numeric ID or the domain name itself.

Parameters

  • zone_name<~String> - zone name

Returns

  • response<~Excon::Response>:

    • body<~Hash>:

      • “data”<~Hash> The representation of the domain.

# File lib/fog/dnsimple/requests/dns/get_domain.rb, line 16
def get_domain(zone_name)
  request(
    expects:  200,
    method:   "GET",
    path:     "/#{@dnsimple_account}/domains/#{zone_name}"
  )
end
get_record(zone_name, record_id) click to toggle source

Gets record from given domain.

Parameters

  • zone_name<~String> - zone name

  • record_id<~String>

Returns

  • response<~Excon::Response>:

    • body<~Hash>:

      • “data”<~Hash> The representation of the record.

# File lib/fog/dnsimple/requests/dns/get_record.rb, line 15
def get_record(zone_name, record_id)
  request(
    expects:  200,
    method:   "GET",
    path:     "/#{@dnsimple_account}/zones/#{zone_name}/records/#{record_id}"
  )
end
list_all_domains(query: {}) click to toggle source

Get the list of ALL domains in the account.

This method is similar to list_domains, but instead of returning the results of a specific page it iterates all the pages and returns the entire collection.

Please use this method carefully, as fetching the entire collection will increase the number of requests you send to the API server and you may eventually be rate-limited.

@see developer.dnsimple.com/v2/domains/#list @see github.com/dnsimple/dnsimple-developer/tree/master/fixtures/v2/listDomains

@param query [Hash] @return [Excon::Response]

# File lib/fog/dnsimple/requests/dns/list_all_domains.rb, line 18
def list_all_domains(query: {})
  paginate(query: query) do |current_query|
    list_domains(query: current_query)
  end
end
list_all_records(zone_name, query: {}) click to toggle source

Get the list of ALL records for the specific zone.

This method is similar to list_domains, but instead of returning the results of a specific page it iterates all the pages and returns the entire collection.

Please use this method carefully, as fetching the entire collection will increase the number of requests you send to the API server and you may eventually be rate-limited.

@see developer.dnsimple.com/v2/zones/records/#list @see github.com/dnsimple/dnsimple-developer/tree/master/fixtures/v2/listZoneRecords

@param zone_name [String] @param query [Hash] @return [Excon::Response]

# File lib/fog/dnsimple/requests/dns/list_all_records.rb, line 19
def list_all_records(zone_name, query: {})
  paginate(query: query) do |current_query|
    list_records(zone_name, query: current_query)
  end
end
list_domains(query: {}) click to toggle source

Get the paginated list of domains in the account.

@see developer.dnsimple.com/v2/domains/#list @see github.com/dnsimple/dnsimple-developer/tree/master/fixtures/v2/listDomains

@param query [Hash] @return [Excon::Response]

# File lib/fog/dnsimple/requests/dns/list_domains.rb, line 12
def list_domains(query: {})
  request(
    expects: 200,
    method: "GET",
    path: "/#{@dnsimple_account}/domains",
    query: query
  )
end
list_records(zone_name, query: {}) click to toggle source

Get the paginated list of records for the specific zone.

@see developer.dnsimple.com/v2/zones/records/#list @see github.com/dnsimple/dnsimple-developer/tree/master/fixtures/v2/listZoneRecords

@param zone_name [String] @param query [Hash] @return [Excon::Response]

# File lib/fog/dnsimple/requests/dns/list_records.rb, line 13
def list_records(zone_name, query: {})
  request(
    expects: 200,
    method: "GET",
    path: "/#{@dnsimple_account}/zones/#{zone_name}/records",
    query: query
  )
end
reload() click to toggle source
# File lib/fog/dnsimple/dns.rb, line 78
def reload
  @connection.reset
end
request(params) click to toggle source
# File lib/fog/dnsimple/dns.rb, line 82
def request(params)
  params[:headers] ||= {}

  if @dnsimple_token && @dnsimple_account
    params[:headers].merge!("Authorization" => "Bearer #{@dnsimple_token}")
  else
    raise ArgumentError.new("Insufficient credentials to properly authenticate!")
  end
  params[:headers].merge!(
      "Accept" => "application/json",
      "Content-Type" => "application/json"
  )

  version = params.delete(:version) || "v2"
  params[:path] = File.join("/", version, params[:path])

  response = @connection.request(params)

  unless response.body.empty?
    response.body = Fog::JSON.decode(response.body)
  end
  response
end
update_record(zone_name, record_id, options) click to toggle source

Update the given record for the given domain.

Parameters

  • zone_name<~String> - zone name

  • record_id<~String>

  • options<~Hash> - optional

    • type<~String>

    • content<~String>

    • priority<~Integer>

    • ttl<~Integer>

Returns

  • response<~Excon::Response>:

    • body<~Hash>:

      • “data”<~Hash> The representation of the record.

# File lib/fog/dnsimple/requests/dns/update_record.rb, line 20
def update_record(zone_name, record_id, options)
  body = options

  request(
    body:     Fog::JSON.encode(body),
    expects:  200,
    method:   "PATCH",
    path:     "/#{@dnsimple_account}/zones/#{zone_name}/records/#{record_id}"
  )
end

Private Instance Methods

paginate(query: {}) { |current_query| ... } click to toggle source
# File lib/fog/dnsimple/dns.rb, line 108
def paginate(query: {})
  current_page = 0
  total_pages = nil
  total_entries = nil
  collection = []
  response = nil

  begin
    current_page += 1
    current_query = query.merge({ page: current_page, per_page: 100 })

    response = yield(current_query)
    total_entries ||= response.body.dig("pagination", "total_entries")
    total_pages ||= response.body.dig("pagination", "total_pages")
    collection.concat(response.body["data"])
  end while current_page < total_pages

  total_entries == collection.size or
    raise(Fog::Errors::Error, "Expected `#{total_entries}`, fetched only `#{collection.size}`")

  response.body["data"] = collection
  response
end