spam_guardian 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 244a0e9f817a4d939b9df187ba1cfb49e11a2f9a55c25e6eead4d00c55693be5
|
4
|
+
data.tar.gz: 6a8a827cb4d869a80d2ad32ad2bef8b32ecb033e76ca8a80c8b4a05237526121
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a776c797a65f38d63b49a394c7f4d80302af6dc48b0325f6ad3b717cf09af8bfe0058b96b4d251f4eb1b961044dc498c37d82150fbaf61a135e31ec67b1c741
|
7
|
+
data.tar.gz: 4d264062fe35f4a9844d2a0cbd42d06d5c9ec10412768cd185a35cd813c2b54c99bfbfad422b8c0a6232c28055265cdb677c6a3cd8d9630bfbbd15bbf2c3849c
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
require 'spam_guardian/email_check'
|
4
|
+
require 'spam_guardian/ip_check'
|
5
|
+
require 'spam_guardian/value_check'
|
6
|
+
module SpamGuardian
|
7
|
+
class ParamsBuilder < ActiveSupport::HashWithIndifferentAccess
|
8
|
+
# Support string
|
9
|
+
# support nth array
|
10
|
+
# Support nth array of string
|
11
|
+
# Support nth array of hash of string
|
12
|
+
# Support nth array of hash of array
|
13
|
+
# Support hash of string
|
14
|
+
# Support hash of array of string
|
15
|
+
ALLOWED_KEYS = %i(email ip username).freeze
|
16
|
+
attr_reader :temp_params
|
17
|
+
def initialize(params={})
|
18
|
+
super Hash.new { |hash, key| hash[key] = [] }
|
19
|
+
case params
|
20
|
+
when String
|
21
|
+
self[parse_key(params)] << params
|
22
|
+
when Array
|
23
|
+
parse_array(params)
|
24
|
+
when Hash
|
25
|
+
parse_hash(params)
|
26
|
+
else
|
27
|
+
raise FormatError.new("Invalid format.")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
def validate_hash_keys!(hash)
|
33
|
+
hash.each_key do |key|
|
34
|
+
raise FormatError.new("#{key}: invalid key.") unless key.to_sym.in?(ALLOWED_KEYS)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def parse_key(string)
|
39
|
+
if EmailCheck.new(string).valid?
|
40
|
+
'email'
|
41
|
+
elsif IpCheck.new(string).valid?
|
42
|
+
'ip'
|
43
|
+
else
|
44
|
+
'username'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def parse_array(array)
|
49
|
+
array.flatten.each do |value|
|
50
|
+
case value
|
51
|
+
when String
|
52
|
+
self[parse_key(value)] << value
|
53
|
+
when Hash
|
54
|
+
parse_hash(value)
|
55
|
+
else
|
56
|
+
raise FormatError.new("Invalid format.")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def parse_hash(hash)
|
62
|
+
validate_hash_keys!(hash)
|
63
|
+
hash.each do |key, value|
|
64
|
+
case value
|
65
|
+
when String
|
66
|
+
self[key] << value
|
67
|
+
when Array
|
68
|
+
raise FormatError.new("Invalid format.") unless ValueCheck.new(Array).sub_class(String).check(value)
|
69
|
+
self[key].concat(value)
|
70
|
+
else
|
71
|
+
raise FormatError.new("Invalid format.")
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -5,35 +5,40 @@ require 'json'
|
|
5
5
|
require 'spam_guardian/email_check'
|
6
6
|
require 'spam_guardian/ip_check'
|
7
7
|
require 'spam_guardian/client'
|
8
|
+
require 'spam_guardian/value_check'
|
9
|
+
require 'spam_guardian/params_builder'
|
8
10
|
module SpamGuardian
|
9
|
-
Validator
|
11
|
+
class Validator
|
12
|
+
attr_reader :params, :spams, :response
|
13
|
+
def initialize(object)
|
14
|
+
@params = ParamsBuilder.new(object)
|
15
|
+
@spams = []
|
16
|
+
end
|
10
17
|
|
11
18
|
def valid?
|
12
|
-
return unless ip_or_email.is_a?(String) && key.present?
|
13
19
|
validate!
|
14
|
-
|
20
|
+
spams.count.zero?
|
15
21
|
end
|
16
22
|
|
17
23
|
def validate!
|
18
24
|
return if @validated
|
19
25
|
@response = Client.new.get(params)
|
20
|
-
|
26
|
+
params.each_key do |key|
|
27
|
+
case response[key]
|
28
|
+
when Hash
|
29
|
+
validate_response(response[key])
|
30
|
+
when Array
|
31
|
+
response[key].each do |hash|
|
32
|
+
validate_response(hash)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
21
36
|
@validated = true
|
22
37
|
end
|
23
38
|
|
24
|
-
def
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
def key
|
29
|
-
@key ||= begin
|
30
|
-
if EmailCheck.new(ip_or_email).valid?
|
31
|
-
'email'
|
32
|
-
elsif IpCheck.new(ip_or_email).valid?
|
33
|
-
'ip'
|
34
|
-
else
|
35
|
-
'username'
|
36
|
-
end
|
39
|
+
def validate_response(response)
|
40
|
+
unless response.fetch('frequency', 0).to_i.zero?
|
41
|
+
spams << response.fetch('value', '')
|
37
42
|
end
|
38
43
|
end
|
39
44
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
module SpamGuardian
|
4
|
+
class ValueCheck
|
5
|
+
attr_reader :klass
|
6
|
+
def initialize(klass)
|
7
|
+
raise FormatError("'#{klass}';Should be a class.") unless klass.is_a?(Class)
|
8
|
+
@klass = klass
|
9
|
+
end
|
10
|
+
|
11
|
+
def sub_class(klass)
|
12
|
+
raise FormatError("'#{klass}';Should be a class.") unless klass.is_a?(Class)
|
13
|
+
dup.tap do |checker|
|
14
|
+
checker.instance_eval do
|
15
|
+
if @sub_checker.is_a?(ValueCheck)
|
16
|
+
@sub_checker = @sub_checker.sub_class(klass)
|
17
|
+
else
|
18
|
+
@sub_checker = ValueCheck.new(klass)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def check(value)
|
25
|
+
return false unless value.is_a?(klass)
|
26
|
+
case value
|
27
|
+
when Array, Hash
|
28
|
+
return true unless @sub_checker.is_a?(ValueCheck)
|
29
|
+
value.all? do |(*args)|
|
30
|
+
@sub_checker.check(args.last)
|
31
|
+
end
|
32
|
+
else
|
33
|
+
true
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spam_guardian
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- frank0224
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-12-
|
11
|
+
date: 2021-12-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -86,8 +86,11 @@ files:
|
|
86
86
|
- lib/spam_guardian.rb
|
87
87
|
- lib/spam_guardian/client.rb
|
88
88
|
- lib/spam_guardian/email_check.rb
|
89
|
+
- lib/spam_guardian/format_error.rb
|
89
90
|
- lib/spam_guardian/ip_check.rb
|
91
|
+
- lib/spam_guardian/params_builder.rb
|
90
92
|
- lib/spam_guardian/validator.rb
|
93
|
+
- lib/spam_guardian/value_check.rb
|
91
94
|
- lib/spam_guardian/version.rb
|
92
95
|
- spam_guardian.gemspec
|
93
96
|
homepage: https://github.com/koten0224/spam_guardian
|
@@ -113,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
113
116
|
- !ruby/object:Gem::Version
|
114
117
|
version: '0'
|
115
118
|
requirements: []
|
116
|
-
rubygems_version: 3.
|
119
|
+
rubygems_version: 3.1.4
|
117
120
|
signing_key:
|
118
121
|
specification_version: 4
|
119
122
|
summary: Spam Guardian
|