Sha256: 029fa8b8e2e677f03a06a851dc92a1f3f77b36e0747f338cc3a873958248c543

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

require 'active_support/concern'

module ActiveGraph
  module Core
    # Containing the logic for dealing with adaptors which use URIs
    module HasUri
      extend ActiveSupport::Concern

      included do
        %w[scheme user password host port].each do |method|
          define_method(method) do
            (@uri && @uri.send(method)) || (self.class.default_uri && self.class.default_uri.send(method))
          end
        end
      end

      module ClassMethods
        attr_reader :default_uri

        def default_url(default_url)
          @default_uri = uri_from_url!(default_url)
        end

        def validate_uri(&block)
          @uri_validator = block
        end

        def uri_from_url!(url)
          validate_url!(url)

          @uri = url.nil? ? @default_uri : URI(url)

          fail ArgumentError, "Invalid URL: #{url.inspect}" if uri_valid?(@uri)

          @uri
        end

        private

        def validate_url!(url)
          fail ArgumentError, "Invalid URL: #{url.inspect}" if !(url.is_a?(String) || url.nil?)
          fail ArgumentError, 'No URL or default URL specified' if url.nil? && @default_uri.nil?
        end

        def uri_valid?(uri)
          @uri_validator && !@uri_validator.call(uri)
        end
      end

      def url
        @uri.to_s
      end

      def url=(url)
        @uri = self.class.uri_from_url!(url)
      end

      def url_without_password
        @url_without_password ||= "#{scheme}://#{user + ':...@' if user}#{host}:#{port}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activegraph-10.0.0.pre.beta.1 lib/active_graph/core/has_uri.rb