spambox 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/spambox.rb +41 -30
  3. metadata +16 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 45fbd091608fd08f1bb429828cf6dcd81c88b294
4
- data.tar.gz: 2f3230af01f1ec27ac9e2449cfed2c4d24508ba6
3
+ metadata.gz: 7538f0fdf33dd4c140ecad13748399982c733556
4
+ data.tar.gz: ce640a122f6ef847dab35a1047759824c8c58593
5
5
  SHA512:
6
- metadata.gz: c6025360f8a0312d3330ff9842095721ebe1d5d5437f294c8174ebc0f3dada9d6418bb6779da5123164f79826948704febbcf06548cbeed5bd43be5a53d16704
7
- data.tar.gz: da398dd530a974a36bef8c2bb4d44bf8e7e0de17f2e9d27f48238d11618c50471681fb7e9cc57d7dd621a891100304ba648eb23855a8bc77b4557b0358e5bc71
6
+ metadata.gz: 3c5ad784eb07a7ea22e23389f42b5b739007be09173fd57d322589144bcd9e096c6b022d8d18a56347c3db2b5a739ebd841f3dd3d60a0fe45565efcee654bdc7
7
+ data.tar.gz: 636a4d87c4361b4f33e9e33505b89e81fc91416b54797752c4f1dc81f1bc7c8dc1db3ef0ddbcd30b1ff26de216059b605eb0994125e00a1d41ec26bf99a8171f
@@ -1,45 +1,58 @@
1
1
  require "json"
2
- require 'open-uri'
2
+ require "open-uri"
3
+ require "sanitize"
3
4
 
4
5
  class Spambox
5
- attr_accessor :object
6
- attr_reader :blacklist
7
-
8
6
  FORMBOX_BLACKLIST_URL = "https://formbox.es/spam-keywords.json"
9
7
 
10
- def initialize(object)
8
+ def initialize(object, options = { allow_html: false })
11
9
  @object = object
12
- @occurences = 0
13
- @total_words = 0
14
- @blacklist = blacklist
15
-
16
- process(object)
10
+ @options = options
17
11
  end
18
12
 
19
13
  def spam_score
20
- (@occurences.to_f / @total_words.to_f * 100).round
14
+ return 100 if @options[:allow_html] == false && contains_html?
15
+ return 0 if (string_length = sanitized_string.split.size.to_f) == 0
16
+ (count_occurences.to_f / string_length * 100).round
17
+ end
18
+
19
+ def update_blacklist
20
+ request = open(FORMBOX_BLACKLIST_URL)
21
+
22
+ Dir.mkdir("tmp") unless Dir.exists?("tmp")
23
+ File.open("tmp/spam_triggers.json", 'w') { |f| f.write(request.read) }
21
24
  end
22
25
 
23
26
  private
24
27
 
25
- def process(object)
26
- if object.is_a? String
27
- @occurences = check(object)
28
- @total_words = object.split.size
29
- elsif object.is_a? ActiveRecord::Base
30
- object.class.column_names.each do |column_name|
31
- attribute = object.send(column_name).to_s
32
- @occurences += check(attribute)
33
- @total_words += attribute.split.size
34
- end
28
+ def sanitized_string
29
+ Sanitize.fragment(flat_string)
30
+ end
31
+
32
+ def flat_string
33
+ case @object
34
+ when String
35
+ return @object
36
+ when Array
37
+ return @object.join(" ")
38
+ when Hash
39
+ return flatten_hash(h).values.join(" ")
40
+ when ActiveRecord::Base
41
+ return @object.class.column_names.map do |column|
42
+ @object.send(column).to_s
43
+ end.join(" ")
35
44
  else
36
- raise ArgumentError, "SpamFilter only supports String- or ActiveRecord objects, got #{object.class}."
45
+ raise ArgumentError, "SpamFilter only supports Array-, String- or ActiveRecord objects, got #{@object.class}."
37
46
  end
38
47
  end
39
48
 
40
- def check(attribute)
41
- @blacklist.map { |s|
42
- attribute.to_s.downcase.scan(s.downcase).count * s.split.size
49
+ def contains_html?
50
+ return true if sanitized_string != flat_string
51
+ end
52
+
53
+ def count_occurences
54
+ blacklist.map { |s|
55
+ sanitized_string.downcase.scan(s.downcase).count * s.split.size
43
56
  }.inject(0, :+)
44
57
  end
45
58
 
@@ -57,10 +70,8 @@ class Spambox
57
70
  "{}"
58
71
  end
59
72
 
60
- def update_blacklist
61
- request = open(FORMBOX_BLACKLIST_URL)
62
-
63
- Dir.mkdir("tmp") unless Dir.exists?("tmp")
64
- File.open("tmp/spam_triggers.json", 'w') { |f| f.write(request.read) }
73
+ def flatten_hash(h)
74
+ return { [] => h } unless h.is_a?(Hash)
75
+ Hash[h.map { |a,v1| flatten_hash(v1).map { |b,v2| [[a] + b, v2] } }.flatten(1)]
65
76
  end
66
77
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spambox
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Jansen
@@ -9,7 +9,21 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2015-12-04 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sanitize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
13
27
  description: Spambox uses Formbox.es API to check for spam.
14
28
  email:
15
29
  - joshuajansen88@gmail.com