From f6191824cdfb2d5c968dc04b3bf9354119b8ffd9 Mon Sep 17 00:00:00 2001 From: Nikita Bulaj Date: Fri, 8 Dec 2017 17:48:45 +0300 Subject: [PATCH] Fix some errors, update README --- README.md | 13 ++++++++++++ .../document/adapters/abstract_adapter.rb | 2 +- lib/proxy_fetcher/exceptions.rb | 8 ++----- proxy_fetcher.gemspec | 2 +- spec/proxy_fetcher/configuration_spec.rb | 21 +++++++++++++++++-- 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 38eaf83..0bafcf3 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ validating proxy lists from the different providers. [Checkout examples](#standa ## Table of Contents +- [Dependencies](#dependencies) - [Installation](#installation) - [Example of usage](#example-of-usage) - [In Ruby application](#in-ruby-application) @@ -29,6 +30,18 @@ validating proxy lists from the different providers. [Checkout examples](#standa - [Contributing](#contributing) - [License](#license) +## Dependencies + +ProxyFetcher gem itself requires only Ruby `>= 2.0.0`. + +However, it requires an adapter to parse HTML. If you do not specify any specific adapter, then it will use +default one - [Nokogiri](https://github.com/sparklemotion/nokogiri). It's OK for any Ruby on Rails project +(because they uses it by default). + +But if you want to use some specific adapter (for example your Ruby application uses [Oga](https://gitlab.com/yorickpeterse/oga), +then you need to manually add your dependencies to your project and configure ProxyFetcher to use another adapter. Moreover, +you can implement your own adapter if it your use-case. Take a look at the [Configuration](#configuration) section for more details. + ## Installation If using bundler, first add 'proxy_fetcher' to your Gemfile: diff --git a/lib/proxy_fetcher/document/adapters/abstract_adapter.rb b/lib/proxy_fetcher/document/adapters/abstract_adapter.rb index f5b7a6e..b4af4f6 100644 --- a/lib/proxy_fetcher/document/adapters/abstract_adapter.rb +++ b/lib/proxy_fetcher/document/adapters/abstract_adapter.rb @@ -24,7 +24,7 @@ def proxy_node def self.setup!(*args) install_requirements!(*args) rescue LoadError => error - raise Exceptions::AdapterSetupError.new(self.class.name, error.message) + raise Exceptions::AdapterSetupError.new(name, error.message) end end end diff --git a/lib/proxy_fetcher/exceptions.rb b/lib/proxy_fetcher/exceptions.rb index 6a89e4d..27324ed 100644 --- a/lib/proxy_fetcher/exceptions.rb +++ b/lib/proxy_fetcher/exceptions.rb @@ -51,7 +51,7 @@ def initialize(*) class AdapterSetupError < Error def initialize(adapter_name, reason) - adapter = demodulize(adapter_name.remove('Adapter')) + adapter = demodulize(adapter_name.gsub('Adapter', '')) super("can't setup '#{adapter}' adapter during the following error:\n\t#{reason}'") end @@ -62,11 +62,7 @@ def demodulize(path) path = path.to_s index = path.rindex('::') - if index - path[(index + 2)..-1] - else - path - end + index ? path[(index + 2)..-1] : path end end end diff --git a/proxy_fetcher.gemspec b/proxy_fetcher.gemspec index a53640d..2b497d4 100644 --- a/proxy_fetcher.gemspec +++ b/proxy_fetcher.gemspec @@ -5,7 +5,7 @@ require 'proxy_fetcher/version' Gem::Specification.new do |gem| gem.name = 'proxy_fetcher' gem.version = ProxyFetcher.gem_version - gem.date = '2017-12-05' + gem.date = '2017-12-08' gem.summary = 'Ruby gem for dealing with proxy lists from different providers' gem.description = 'This gem can help your Ruby application to make HTTP(S) requests ' \ 'using proxies by fetching and validating proxy lists from the different providers.' diff --git a/spec/proxy_fetcher/configuration_spec.rb b/spec/proxy_fetcher/configuration_spec.rb index ae8e077..8e39ef7 100644 --- a/spec/proxy_fetcher/configuration_spec.rb +++ b/spec/proxy_fetcher/configuration_spec.rb @@ -43,16 +43,33 @@ def self.connectable?(*) end context 'custom provider' do - it 'failed on registration if provider class already registered' do + it 'fails on registration if provider class already registered' do expect { ProxyFetcher::Configuration.register_provider(:xroxy, Class.new) } .to raise_error(ProxyFetcher::Exceptions::RegisteredProvider) end - it "failed on proxy list fetching if provider doesn't registered" do + it "fails on proxy list fetching if provider doesn't registered" do ProxyFetcher.config.provider = :not_existing_provider expect { ProxyFetcher::Manager.new } .to raise_error(ProxyFetcher::Exceptions::UnknownProvider) end end + + context 'custom HTML parsing adapter' do + it "fails if adapter can't be installed" do + old_config = ProxyFetcher.config.dup + + class CustomAdapter < ProxyFetcher::Document::AbstractAdapter + def self.install_requirements! + require 'not_existing_gem' + end + end + + expect { ProxyFetcher.config.adapter = CustomAdapter } + .to raise_error(ProxyFetcher::Exceptions::AdapterSetupError) + + ProxyFetcher.instance_variable_set('@config', old_config) + end + end end