Class: Rspider::SiteLocker

Inherits:
Object
  • Object
show all
Defined in:
lib/rspider/SiteLocker.rb

Overview

This class hold a site and avoid from visiting a site in heavy frequency.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max, site = "www.example.com") ⇒ SiteLocker

initialization subprocess



16
17
18
19
20
21
# File 'lib/rspider/SiteLocker.rb', line 16

def initialize(max,site="www.example.com")
	@visits=Hash.new
	@max=max
	@site=site
	@time=5
end

Instance Attribute Details

#maxObject

Returns the value of attribute max.



13
14
15
# File 'lib/rspider/SiteLocker.rb', line 13

def max
  @max
end

#siteObject

Returns the value of attribute site.



13
14
15
# File 'lib/rspider/SiteLocker.rb', line 13

def site
  @site
end

#timeObject

Returns the value of attribute time.



13
14
15
# File 'lib/rspider/SiteLocker.rb', line 13

def time
  @time
end

Instance Method Details

#canVisitSite?Boolean

If we can visit the site again?

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rspider/SiteLocker.rb', line 28

def canVisitSite?()
	t=Time.now.to_i-@time
	@visits.delete_if{|k,v|
		k<(t-@time)
	}
	values=0
	@visits.values.each{|v|
		values = values +v
	}
	return values<@max
end

#to_sObject

dump the data struct to string



40
41
42
43
# File 'lib/rspider/SiteLocker.rb', line 40

def to_s
	temp=@visits.collect{|k,v| "visits[#{k}]\t=>\t#{v}"}
	temp.join("\n")
end

#visitedSiteObject

we visit a site ,we log this



23
24
25
26
# File 'lib/rspider/SiteLocker.rb', line 23

def visitedSite()
	t=Time.now.to_i
	@visits[t]=@visits[t].to_i+1
end