Sha256: 3fb27f27b300232afc6b1c0fece871a25d54a627afadfda4f9dd7b59418e374a

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module ProxyFetcher
  class Document
    # ProxyFetcher HTML parser adapters.
    #
    # ProxyFetcher default supported adapters are:
    #
    # * Nokogiri
    # * Oga
    #
    # Any custom adapter can be used and must be inherited from
    # <code>ProxyFetcher::Document::AbstractAdapter</code>.
    class Adapters
      # Adapters class name suffix
      ADAPTER = 'Adapter'.freeze
      private_constant :ADAPTER

      class << self
        # Returns HTML parser adapter by it's name or class.
        # If name is provided, then it looks for predefined classes
        # in <code>ProxyFetcher::Document</code> namespace. Otherwise
        # it just returns the passed class.
        #
        # @param name_or_class [String, Class]
        #   Adapter name or class
        #
        def lookup(name_or_class)
          raise Exceptions::BlankAdapter if name_or_class.nil? || name_or_class.to_s.empty?

          case name_or_class
          when Symbol, String
            adapter_name = "#{name_or_class.to_s.capitalize}#{ADAPTER}"
            ProxyFetcher::Document.const_get(adapter_name)
          else
            name_or_class
          end
        rescue NameError
          raise Exceptions::UnknownAdapter, name_or_class
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
proxy_fetcher-0.10.2 lib/proxy_fetcher/document/adapters.rb
proxy_fetcher-0.10.1 lib/proxy_fetcher/document/adapters.rb