class Splicer::DnsMadeEasy::Client

The client that talks to DnsMadeEasy

## Example Usage

client = Splicer::DnsMadeEasy::Client.new('key','secret')
client.get('dns/managed/123456')

client.post('dns/managed', {name: 'mydumbdomain.com'})

@author Matthew A. Johnston

Constants

DEFAULT_END_POINTS

Public Class Methods

new(key, secret, options = {}) click to toggle source

@param [String] key @param [String] secret

# File lib/splicer/dns_made_easy/client.rb, line 22
def initialize(key, secret, options = {})
  @key = key
  @secret = secret
  @api_mode = options[:api_mode] || :live
  @use_ssl = options[:use_ssl]
end

Public Instance Methods

delete(resource, payload={}) click to toggle source

@param [String] resource the resource path @param [Hash] payload the data you wish to send

@raise [Splicer::Errors::Error] when the request fails @return [String]

# File lib/splicer/dns_made_easy/client.rb, line 75
def delete(resource, payload={})
  execute({
    method: :delete,
    url: resource_url(resource),
    payload: process_payload(payload),
    headers: headers
  })
end
get(resource, params={}) click to toggle source

@param [String] resource the resource path

@raise [Splicer::Errors::Error] when the request fails @return [String]

# File lib/splicer/dns_made_easy/client.rb, line 33
def get(resource, params={})
  execute({
    method: :get,
    url: resource_url(resource),
    headers: headers('params' => params)
  })
end
post(resource, payload) click to toggle source

@param [String] resource the resource path @param [Hash] payload the data you wish to send

@raise [Splicer::Errors::Error] when the request fails @return [String]

# File lib/splicer/dns_made_easy/client.rb, line 47
def post(resource, payload)
  execute({
    method: :post,
    url: resource_url(resource),
    payload: process_payload(payload),
    headers: headers
  })
end
put(resource, payload) click to toggle source

@param [String] resource the resource path @param [Hash] payload the data you wish to send

@raise [Splicer::Errors::Error] when the request fails @return [String]

# File lib/splicer/dns_made_easy/client.rb, line 61
def put(resource, payload)
  execute({
    method: :put,
    url: resource_url(resource),
    payload: process_payload(payload),
    headers: headers
  })
end

Private Instance Methods

base_url() click to toggle source
# File lib/splicer/dns_made_easy/client.rb, line 142
def base_url
  if @use_ssl
    DEFAULT_END_POINTS[@api_mode]
  else
    DEFAULT_END_POINTS[@api_mode].sub('https', 'http')
  end
end
execute(args={}) click to toggle source

Wrapper around RestClient::Request.execute method

@param [Hash] args @raise [Splicer::Errors::Error] when the request fails @return [Hash]

# File lib/splicer/dns_made_easy/client.rb, line 91
def execute(args={})
  response = RestClient::Request.execute(args)
  Splicer.logger.debug "[SPLICER][DNSMADEEASY] method=#{args[:method]} url=#{args[:url]} headers=#{args[:headers]} payload=#{args[:payload]} response=#{response}"
  response
rescue RestClient::Exception => error
  request_params = { method: args[:method], url: args[:url], headers: args[:headers], payload: args[:payload] }
  if error.response
    Splicer.logger.debug "[SPLICER][DNSMADEEASY] method=#{args[:method]} url=#{args[:url]} request_headers=#{args[:headers]} payload=#{args[:payload]} response_headers=#{error.response.raw_headers}"
    raise Splicer::Errors::RequestError.new(error, {
      request: request_params,
      response: {
        message: error.response,
        headers: error.response.raw_headers
      }
    })
  else
    Splicer.logger.debug "[SPLICER][DNSMADEEASY] method=#{args[:method]} url=#{args[:url]} request_headers=#{args[:headers]} payload=#{args[:payload]}"
    raise Splicer::Errors::RequestError.new(error, { request: request_params })
  end
end
headers(params={}) click to toggle source

@param [Hash] params @return [Hash]

# File lib/splicer/dns_made_easy/client.rb, line 121
def headers(params={})
  @headers ||= {}
  @headers['x-dnsme-apiKey']      = @key
  @headers['x-dnsme-requestDate'] = Splicer::DnsMadeEasy::Time.now.to_date_header
  @headers['x-dnsme-hmac']        = signature
  @headers['Accept']              = 'application/json'
  @headers['Content-Type']        = 'application/json'
  @headers.merge!(params)
  @headers
end
process_payload(payload) click to toggle source

Processes the payload to see if it needs to be turned in to JSON

@return [String]

# File lib/splicer/dns_made_easy/client.rb, line 115
def process_payload(payload)
  payload.is_a?(String) ? payload : payload.to_json
end
resource_url(resource) click to toggle source
# File lib/splicer/dns_made_easy/client.rb, line 138
def resource_url(resource)
  [base_url, resource.gsub(/^\//,'')].join('/')
end
signature() click to toggle source

@param [Hash] params @return [String]

# File lib/splicer/dns_made_easy/client.rb, line 134
def signature
  OpenSSL::HMAC.hexdigest('sha1', @secret, @headers['x-dnsme-requestDate'])
end