spambox 0.2 → 0.3.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 +4 -4
  2. data/lib/spambox.rb +16 -10
  3. metadata +16 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7538f0fdf33dd4c140ecad13748399982c733556
4
- data.tar.gz: ce640a122f6ef847dab35a1047759824c8c58593
3
+ metadata.gz: 42b3aac0468aeff152521ab737e6fef1b1f673ad
4
+ data.tar.gz: 88ca40c356d3528442b5825ba6258b7e17090865
5
5
  SHA512:
6
- metadata.gz: 3c5ad784eb07a7ea22e23389f42b5b739007be09173fd57d322589144bcd9e096c6b022d8d18a56347c3db2b5a739ebd841f3dd3d60a0fe45565efcee654bdc7
7
- data.tar.gz: 636a4d87c4361b4f33e9e33505b89e81fc91416b54797752c4f1dc81f1bc7c8dc1db3ef0ddbcd30b1ff26de216059b605eb0994125e00a1d41ec26bf99a8171f
6
+ metadata.gz: 0e2bbed2ff8e574251ceed0ec8ce075f20b1f6ac1d376744560901cc5743587738c61a73d4716c3769b40a57c8f36093615c2482ef2a9a74939b12fa0bf248d0
7
+ data.tar.gz: c7dfa876d51fa0051bebacc54a69614a2f784cc2dcac70a3392d5c87788586536ec5f95a7091c9b935c4be25b214fdbacb93be6b69088a37fe3130056a9b7f1a
data/lib/spambox.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "json"
2
2
  require "open-uri"
3
3
  require "sanitize"
4
+ require "active_support/core_ext/string/filters"
4
5
 
5
6
  class Spambox
6
7
  FORMBOX_BLACKLIST_URL = "https://formbox.es/spam-keywords.json"
@@ -19,8 +20,8 @@ class Spambox
19
20
  def update_blacklist
20
21
  request = open(FORMBOX_BLACKLIST_URL)
21
22
 
22
- Dir.mkdir("tmp") unless Dir.exists?("tmp")
23
- File.open("tmp/spam_triggers.json", 'w') { |f| f.write(request.read) }
23
+ Dir.mkdir("tmp") unless Dir.exist?("tmp")
24
+ File.open("tmp/spam_triggers.json", "w") { |f| f.write(request.read) }
24
25
  end
25
26
 
26
27
  private
@@ -30,19 +31,24 @@ class Spambox
30
31
  end
31
32
 
32
33
  def flat_string
34
+ @flat_string ||= object_to_string.squish
35
+ end
36
+
37
+ def object_to_string
33
38
  case @object
34
39
  when String
35
40
  return @object
36
41
  when Array
37
42
  return @object.join(" ")
38
43
  when Hash
39
- return flatten_hash(h).values.join(" ")
44
+ return flatten_hash_values(h).values.join(" ")
40
45
  when ActiveRecord::Base
41
46
  return @object.class.column_names.map do |column|
42
47
  @object.send(column).to_s
43
48
  end.join(" ")
44
49
  else
45
- raise ArgumentError, "SpamFilter only supports Array-, String- or ActiveRecord objects, got #{@object.class}."
50
+ fail ArgumentError, "SpamFilter only supports Array-,
51
+ String- or ActiveRecord objects, got #{@object.class}."
46
52
  end
47
53
  end
48
54
 
@@ -51,9 +57,9 @@ class Spambox
51
57
  end
52
58
 
53
59
  def count_occurences
54
- blacklist.map { |s|
60
+ blacklist.map do |s|
55
61
  sanitized_string.downcase.scan(s.downcase).count * s.split.size
56
- }.inject(0, :+)
62
+ end.inject(0, :+)
57
63
  end
58
64
 
59
65
  def blacklist
@@ -63,15 +69,15 @@ class Spambox
63
69
  def spam_triggers
64
70
  tmp_filename = "tmp/spam_triggers.json"
65
71
 
66
- update_blacklist unless File.exists?(tmp_filename) && File.mtime(tmp_filename) > Time.now - (60 * 24 * 7)
72
+ update_blacklist unless File.exist?(tmp_filename) && File.mtime(tmp_filename) > Time.now - (60 * 24 * 7)
67
73
 
68
74
  File.read(tmp_filename)
69
75
  rescue OpenURI::HTTPError, Errno::ECONNREFUSED
70
76
  "{}"
71
77
  end
72
78
 
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)]
79
+ def flatten_hash_values(hash)
80
+ return hash if hash.is_a? String
81
+ hash.flat_map { |_, v| [*flatten_hash_values(v)] }
76
82
  end
77
83
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spambox
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Jansen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-04 00:00:00.000000000 Z
11
+ date: 2016-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sanitize
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.2'
27
41
  description: Spambox uses Formbox.es API to check for spam.
28
42
  email:
29
43
  - joshuajansen88@gmail.com