Sha256: 85e61a619ea17c75cb68b615890ba9213d2a56dcfd8ac6768a80926671a9a661

Contents?: true

Size: 975 Bytes

Versions: 5

Compression:

Stored size: 975 Bytes

Contents

# frozen_string_literal: true

require "json"
require "http"

module DoHClient
  module Client
    class Base
      def resolve(name, options)
        query = build_query(name, options)
        validate(query)
        Request.get(endpoint, query)
      end

      def self.resolve(name, options)
        new.resolve(name, options)
      end

      def endpoint
        raise NotImplementedError, "You must implement #{self.class}##{__method__}"
      end

      def build_query
        raise NotImplementedError, "You must implement #{self.class}##{__method__}"
      end

      def validate(query)
        raise ArgumentError, "name is a required parameter" if query[:name].nil?
        raise ArgumentError, "type is a required parameter" if query[:type].nil?
        raise ArgumentError, "cd must be a boolean value" if query[:cd] && !boolean?(query[:cd])
      end

      private

      def boolean?(param)
        [true, false].include? param
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
doh_client-0.2.2 lib/doh_client/client/base.rb
doh_client-0.2.1 lib/doh_client/client/base.rb
doh_client-0.2.0 lib/doh_client/client/base.rb
doh_client-0.1.1 lib/doh_client/client/base.rb
doh_client-0.1.0 lib/doh_client/client/base.rb