Class: Kudzu::Config
- Inherits:
-
Object
show all
- Defined in:
- lib/kudzu/config.rb,
lib/kudzu/config/filter.rb
Defined Under Namespace
Classes: Delegator, Filter
Constant Summary
collapse
- SIMPLE_CONFIGS =
[:config_file,
:user_agent, :thread_num, :open_timeout, :read_timeout, :keep_alive,
:max_connection, :max_redirect, :max_depth, :default_request_header,
:politeness_delay, :handle_cookie,
:respect_robots_txt, :respect_nofollow, :respect_noindex,
:filters]
- DEFAULT_CONFIG =
{ user_agent: "Kudzu/#{Kudzu::VERSION}",
open_timeout: 10,
read_timeout: 10,
keep_alive: 5,
thread_num: 1,
max_connection: 10,
max_redirect: 3,
politeness_delay: 0.5,
handle_cookie: true,
respect_robots_txt: true,
respect_nofollow: true,
respect_noindex: true }
Instance Method Summary
collapse
Constructor Details
#initialize(config = {}, &block) ⇒ Config
Returns a new instance of Config.
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/kudzu/config.rb', line 27
def initialize(config = {}, &block)
self.filters = {}
DEFAULT_CONFIG.merge(config).each do |key, value|
send("#{key}=", value)
end
if config_file || block
delegator = Delegator.new(self)
delegator.instance_eval(File.read(config_file)) if config_file
delegator.instance_eval(&block) if block
end
end
|
Instance Method Details
#add_filter(base_url = nil, config = {}, &block) ⇒ Object
39
40
41
42
43
44
45
|
# File 'lib/kudzu/config.rb', line 39
def add_filter(base_url = nil, config = {}, &block)
base_uri = Addressable::URI.parse(base_url || '*')
host = base_uri.host.presence || '*'
path = base_uri.path.presence || '*'
filters[host] ||= []
filters[host] << Filter.new(path, config, &block)
end
|
#find_filter(uri) ⇒ Object
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/kudzu/config.rb', line 47
def find_filter(uri)
uri = Addressable::URI.parse(uri) if uri.is_a?(String)
filters.each do |host, filters|
next unless Kudzu::Common.match?(uri.host, host)
filters.each do |filter|
return filter if Kudzu::Common.match?(uri.path, filter.path)
end
end
nil
end
|