Skip to content

Commit

Permalink
Refactor filters
Browse files Browse the repository at this point in the history
  • Loading branch information
nbulaj committed Aug 25, 2017
1 parent d76a99c commit 1ab3c4e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ manager.refresh_list! # or manager.fetch!
# @response_time=5217, @type="HTTP", @anonymity="High">, ... ]
```

You can use two methods to get the first proxy from the list:

* `get` or aliased `pop` (will return first proxy and move it to the end of the list)
* `get!` or aliased `pop!` (will return first **connectable** proxy and move it to the end of the list; all the proxies till the working one will be removed)

Or you can get just random proxy by calling `manager.random_proxy` or it's alias `manager.random`.

If you need to filter proxy list, for example, by country or response time and selected provider supports filtering with GET params,
then you can just pass your filters like a simple Ruby hash to the Manager instance:

Expand Down Expand Up @@ -132,14 +139,15 @@ manager.proxies
# => [...]
```

*NOTE*: not all the providers support filtering. Take a look at the provider class to see if it supports custom filters.

You can use two methods to get the first proxy from the list:
You can apply different filters every time you calling `#refresh_list!` (or `#fetch!`) method:

* `get` or aliased `pop` (will return first proxy and move it to the end of the list)
* `get!` or aliased `pop!` (will return first **connectable** proxy and move it to the end of the list; all the proxies till the working one will be removed)
```ruby
manager.refresh_list!(country: 'PL', maxtime: '500')

# => [...]
```

Or you can get just random proxy by calling `manager.random_proxy` or it's alias `manager.random`.
*NOTE*: not all the providers support filtering. Take a look at the provider classes to see if it supports custom filters.

### Standalone

Expand Down
12 changes: 6 additions & 6 deletions lib/proxy_fetcher/manager.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
module ProxyFetcher
class Manager
attr_reader :proxies, :filters
attr_reader :proxies

# refresh: true - load proxy list from the remote server on initialization
# refresh: false - just initialize the class, proxy list will be empty ([])
def initialize(refresh: true, filters: {})
@filters = filters

def initialize(refresh: true, validate: false, filters: {})
if refresh
refresh_list!
refresh_list!(filters)
else
@proxies = []
end

cleanup! if validate
end

# Update current proxy list from the provider
def refresh_list!
def refresh_list!(filters = nil)
@proxies = []

ProxyFetcher.config.providers.each do |provider_name|
Expand Down
1 change: 0 additions & 1 deletion spec/support/manager_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
expect(proxy.type).not_to be_empty
expect(proxy.country).not_to be_empty
expect(proxy.anonymity).not_to be_empty

expect(proxy.response_time).to be_nil.or(be_a_kind_of(Numeric))
end
end
Expand Down

0 comments on commit 1ab3c4e

Please sign in to comment.