Class: Burnspam::PointTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/burnspam/point_tracker.rb

Overview

PointTracker holds all the statistics of the name and comment inside @name and @content

The name and content are passed into the relevant Strategy (Strategy Design Pattern).

The checker interchanges these strategies. This allows slightly different processing for the name and content as well as a shared method made available through the GeneralStrategy class.

The Checker returns the results to @name and @content

Version

0.1.1

Date

2011/12/18

@author

Brian Burns, x10205284

@reference

snook.ca/archives/other/effective_blog_comment_spam_blocker

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email, name, content) ⇒ PointTracker

Gather all statistics from name and content



25
26
27
28
# File 'lib/burnspam/point_tracker.rb', line 25

def initialize(email, name, content)
  @name = Checker.new(NameCheckerStrategy.new(name))
  @content = Checker.new(ContentCheckerStrategy.new(content))
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



22
23
24
# File 'lib/burnspam/point_tracker.rb', line 22

def content
  @content
end

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/burnspam/point_tracker.rb', line 22

def name
  @name
end

Instance Method Details

#body_length(size, counturls) ⇒ Object

Comment less than 20 char, loose 1 point



61
62
63
64
65
66
67
68
69
# File 'lib/burnspam/point_tracker.rb', line 61

def body_length(size, counturls)
  if size < 20
    -1
  elsif (size > 20) && (counturls == 0)
    2
  else
    0
  end
end

#body_urls(count) ⇒ Object

  • Gain 2 points for 1 URL

  • 0 points for 2 URLS

  • Loost 1 point for more than 2 URLS



45
46
47
48
49
50
51
52
53
# File 'lib/burnspam/point_tracker.rb', line 45

def body_urls(count)
  if count < 2
    2
  elsif count > 2
    count * -1
  else
    0
  end
end

#duplicate?(post) ⇒ Boolean

If the post is duplicate, the comment looses 5 points.

Returns:

  • (Boolean)


89
90
91
# File 'lib/burnspam/point_tracker.rb', line 89

def duplicate?(post)
  post ? -5  :  0
end

#keywordsearch(count) ⇒ Object

For every bad keyword loose 1 point.



72
73
74
# File 'lib/burnspam/point_tracker.rb', line 72

def keywordsearch (count)
  count * -1
end

#name_urls(count) ⇒ Object

URL in name? Loose 2 points



56
57
58
# File 'lib/burnspam/point_tracker.rb', line 56

def name_urls(count)
  count > 0 ? -2 : 0
end

#spaminessObject

Return spaminess based on gathered statistics



31
32
33
34
35
36
37
38
39
40
# File 'lib/burnspam/point_tracker.rb', line 31

def spaminess
  @total = 0
  @total += body_urls(@content.count_urls)
  @total += name_urls(@name.count_urls)
  @total += body_length(@content.length, @content.count_urls)
  @total += keywordsearch(@content.keyword_count)
  @total += urlength(@content.url_length)
  @total += startswith?(@content.starts_with)
  @total += duplicate?(@content.duplicate)
end

#startswith?(word) ⇒ Boolean

If the comment starts with specific bad keywords they loose substantial (10) points This is because spammy comments often start like this.

Returns:

  • (Boolean)


84
85
86
# File 'lib/burnspam/point_tracker.rb', line 84

def startswith?(word)
  word  ?  -10 : 0
end

#urlength(size) ⇒ Object

1 point penalty if average URL length is long



77
78
79
# File 'lib/burnspam/point_tracker.rb', line 77

def urlength(size)
  size > 30? -1  :  0
end