spambox 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/spambox.rb +41 -30
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7538f0fdf33dd4c140ecad13748399982c733556
|
4
|
+
data.tar.gz: ce640a122f6ef847dab35a1047759824c8c58593
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c5ad784eb07a7ea22e23389f42b5b739007be09173fd57d322589144bcd9e096c6b022d8d18a56347c3db2b5a739ebd841f3dd3d60a0fe45565efcee654bdc7
|
7
|
+
data.tar.gz: 636a4d87c4361b4f33e9e33505b89e81fc91416b54797752c4f1dc81f1bc7c8dc1db3ef0ddbcd30b1ff26de216059b605eb0994125e00a1d41ec26bf99a8171f
|
data/lib/spambox.rb
CHANGED
@@ -1,45 +1,58 @@
|
|
1
1
|
require "json"
|
2
|
-
require
|
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
|
-
@
|
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
|
-
|
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
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
41
|
-
|
42
|
-
|
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
|
61
|
-
|
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.
|
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
|