Sha256: 6c382564876611c1c9244fc6dad64688ee40954e0402652a085bd4816e9502b0

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 KB

Contents

require 'uri'

module Spidr
  #
  # The {Sanitizers} module adds methods to {Agent} which control the
  # sanitation of incoming links.
  #
  module Sanitizers
    # Specifies whether the Agent will strip URI fragments
    attr_accessor :strip_fragments

    # Specifies whether the Agent will strip URI queries
    attr_accessor :strip_query

    #
    # Sanitizes a URL based on filtering options.
    #
    # @param [URI::HTTP, URI::HTTPS, String] url
    #   The URL to be sanitized
    #
    # @return [URI::HTTP, URI::HTTPS]
    #   The new sanitized URL.
    #
    # @since 0.2.2
    #
    def sanitize_url(url)
      url = URI(url.to_s) unless url.kind_of?(URI)

      url.fragment = nil if @strip_fragments
      url.query    = nil if @strip_query

      return url
    end

    protected

    #
    # Initializes the Sanitizer rules.
    #
    # @param [Hash] options
    #   Additional options.
    #
    # @option options [Boolean] :strip_fragments (true)
    #   Specifies whether or not to strip the fragment component from URLs.
    #
    # @option options [Boolean] :strip_query (false)
    #   Specifies whether or not to strip the query component from URLs.
    #
    # @since 0.2.2
    #
    def initialize_sanitizers(options={})
      @strip_fragments = options.fetch(:strip_fragments,true)
      @strip_query     = options.fetch(:strip_query,false)
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
spidr_epg_gem-0.0.1 lib/spidr_epg/sanitizers.rb
spidr_epg_gem-0.0.0 lib/spidr_epg/sanitizers.rb
spidr_epg-1.0.0 lib/spidr/sanitizers.rb