Class: Rspider::SiteLocker
- Inherits:
-
Object
- Object
- Rspider::SiteLocker
- 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
-
#max ⇒ Object
Returns the value of attribute max.
-
#site ⇒ Object
Returns the value of attribute site.
-
#time ⇒ Object
Returns the value of attribute time.
Instance Method Summary collapse
-
#canVisitSite? ⇒ Boolean
If we can visit the site again?.
-
#initialize(max, site = "www.example.com") ⇒ SiteLocker
constructor
initialization subprocess.
-
#to_s ⇒ Object
dump the data struct to string.
-
#visitedSite ⇒ Object
we visit a site ,we log this.
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
#max ⇒ Object
Returns the value of attribute max.
13 14 15 |
# File 'lib/rspider/SiteLocker.rb', line 13 def max @max end |
#site ⇒ Object
Returns the value of attribute site.
13 14 15 |
# File 'lib/rspider/SiteLocker.rb', line 13 def site @site end |
#time ⇒ Object
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?
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_s ⇒ Object
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 |
#visitedSite ⇒ Object
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 |