spambox 0.1

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 +7 -0
  2. data/lib/spambox.rb +66 -0
  3. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 45fbd091608fd08f1bb429828cf6dcd81c88b294
4
+ data.tar.gz: 2f3230af01f1ec27ac9e2449cfed2c4d24508ba6
5
+ SHA512:
6
+ metadata.gz: c6025360f8a0312d3330ff9842095721ebe1d5d5437f294c8174ebc0f3dada9d6418bb6779da5123164f79826948704febbcf06548cbeed5bd43be5a53d16704
7
+ data.tar.gz: da398dd530a974a36bef8c2bb4d44bf8e7e0de17f2e9d27f48238d11618c50471681fb7e9cc57d7dd621a891100304ba648eb23855a8bc77b4557b0358e5bc71
data/lib/spambox.rb ADDED
@@ -0,0 +1,66 @@
1
+ require "json"
2
+ require 'open-uri'
3
+
4
+ class Spambox
5
+ attr_accessor :object
6
+ attr_reader :blacklist
7
+
8
+ FORMBOX_BLACKLIST_URL = "https://formbox.es/spam-keywords.json"
9
+
10
+ def initialize(object)
11
+ @object = object
12
+ @occurences = 0
13
+ @total_words = 0
14
+ @blacklist = blacklist
15
+
16
+ process(object)
17
+ end
18
+
19
+ def spam_score
20
+ (@occurences.to_f / @total_words.to_f * 100).round
21
+ end
22
+
23
+ private
24
+
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
35
+ else
36
+ raise ArgumentError, "SpamFilter only supports String- or ActiveRecord objects, got #{object.class}."
37
+ end
38
+ end
39
+
40
+ def check(attribute)
41
+ @blacklist.map { |s|
42
+ attribute.to_s.downcase.scan(s.downcase).count * s.split.size
43
+ }.inject(0, :+)
44
+ end
45
+
46
+ def blacklist
47
+ JSON.parse(spam_triggers)
48
+ end
49
+
50
+ def spam_triggers
51
+ tmp_filename = "tmp/spam_triggers.json"
52
+
53
+ update_blacklist unless File.exists?(tmp_filename) && File.mtime(tmp_filename) > Time.now - (60 * 24 * 7)
54
+
55
+ File.read(tmp_filename)
56
+ rescue OpenURI::HTTPError, Errno::ECONNREFUSED
57
+ "{}"
58
+ end
59
+
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) }
65
+ end
66
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spambox
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Joshua Jansen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Spambox uses Formbox.es API to check for spam.
14
+ email:
15
+ - joshuajansen88@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/spambox.rb
21
+ homepage: https://github.com/joshuajansen/spambox
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.4.5.1
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Spambox is a gem that checks your objects for unsafe keywords and returns
45
+ a spam score.
46
+ test_files: []