cleantalk 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cleantalk.rb +17 -33
- data/lib/cleantalk/check_message.rb +9 -0
- data/lib/cleantalk/check_message_result.rb +3 -0
- data/lib/cleantalk/check_newuser.rb +7 -0
- data/lib/cleantalk/check_newuser_result.rb +2 -0
- data/lib/cleantalk/request.rb +72 -0
- data/lib/cleantalk/result.rb +11 -0
- metadata +9 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f2c0b40a43f70a7403492f512e744c5d10c2d752
|
4
|
+
data.tar.gz: 50def2748310bcb6b902bac7e2e35a6fdad87991
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8b162b23499da00a3db9154669bc4d0e248cab7c9c0b9d67c5782e6dd966d70d601b6f358814f5e1a92ccee5bc700b32a38f52d7edc85f0f1412e8945da3bbfb
|
7
|
+
data.tar.gz: fd9330e52a3ec636416cd62337d957d27ae34f770517836aee1da09e1827b717f775adda4fda6851ec1bdd8ba6a890d645a69ca1bb29de903ff45857c0bde4a8
|
data/lib/cleantalk.rb
CHANGED
@@ -1,31 +1,12 @@
|
|
1
|
-
require 'net/http'
|
2
|
-
require 'json'
|
3
|
-
|
4
1
|
class Cleantalk
|
5
|
-
class Request
|
6
|
-
attr_accessor :all_headers, :last_error_no, :last_error_time, \
|
7
|
-
:last_error_text, :message, :example, :auth_key, :agent, \
|
8
|
-
:stoplist_check, :response_lang, :sender_ip, :sender_email, \
|
9
|
-
:sender_nickname, :sender_info, :post_info, :allow_links, \
|
10
|
-
:submit_time, :js_on, :tz, :feedback, :phone
|
11
|
-
|
12
|
-
# Fill params with constructor
|
13
|
-
def initialize(params = nil)
|
14
|
-
unless params.nil?
|
15
|
-
params.each do |key, value|
|
16
|
-
instance_variable_set("@#{key}", value)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
2
|
#
|
23
3
|
# Function checks whether it is possible to publish the message
|
24
4
|
# @param CleantalkRequest $request
|
25
5
|
# @return type
|
26
6
|
#
|
27
7
|
def is_allowed_message(request)
|
28
|
-
|
8
|
+
request.method_name = 'check_message'
|
9
|
+
return request.http_request
|
29
10
|
end
|
30
11
|
|
31
12
|
#
|
@@ -34,21 +15,24 @@ class Cleantalk
|
|
34
15
|
# @return type
|
35
16
|
#
|
36
17
|
def is_allowed_user(request)
|
37
|
-
|
18
|
+
request.method_name = 'check_newuser'
|
19
|
+
return request.http_request
|
38
20
|
end
|
39
21
|
|
40
|
-
|
41
|
-
uri = URI 'https://moderate.cleantalk.org/api2.0'
|
22
|
+
@@auth_key = nil
|
42
23
|
|
43
|
-
|
44
|
-
|
45
|
-
|
24
|
+
def self.auth_key
|
25
|
+
@@auth_key
|
26
|
+
end
|
46
27
|
|
47
|
-
|
48
|
-
|
49
|
-
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
50
|
-
http.request(req)
|
51
|
-
end
|
52
|
-
return JSON.parse(response.entity)
|
28
|
+
def self.auth_key= value
|
29
|
+
@@auth_key = value
|
53
30
|
end
|
54
31
|
end
|
32
|
+
|
33
|
+
require 'cleantalk/request'
|
34
|
+
require 'cleantalk/check_newuser'
|
35
|
+
require 'cleantalk/check_message'
|
36
|
+
require 'cleantalk/result'
|
37
|
+
require 'cleantalk/check_newuser_result'
|
38
|
+
require 'cleantalk/check_message_result'
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
class Cleantalk::Request
|
5
|
+
attr_accessor :method_name, :auth_key, #required
|
6
|
+
:all_headers, :last_error_no, :last_error_time,
|
7
|
+
:last_error_text, :message, :example, :agent,
|
8
|
+
:stoplist_check, :response_lang, :sender_ip, :sender_email,
|
9
|
+
:sender_nickname, :sender_info, :post_info, :allow_links,
|
10
|
+
:submit_time, :js_on, :tz, :feedback, :phone
|
11
|
+
|
12
|
+
# Fill params with constructor
|
13
|
+
def initialize(params = {})
|
14
|
+
self.method_name, self.auth_key = params.delete(:method_name) || self.class::METHOD, nil
|
15
|
+
unless params.empty?
|
16
|
+
params.each do |key, value|
|
17
|
+
send("#{key}=", value)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def http_request_without_parse
|
23
|
+
valid?
|
24
|
+
form_data = self.instance_variables.inject({}) do |params, var_name|
|
25
|
+
param_key = var_name.to_s.sub('@','')
|
26
|
+
params[param_key] = send(param_key)
|
27
|
+
params
|
28
|
+
end
|
29
|
+
|
30
|
+
req = Net::HTTP::Post.new(API_URI, API_HEADERS)
|
31
|
+
req.body = JSON.generate(form_data)
|
32
|
+
response = Net::HTTP.start(API_URI.hostname, API_URI.port, use_ssl: true) do |http|
|
33
|
+
http.request(req)
|
34
|
+
end
|
35
|
+
|
36
|
+
response.entity
|
37
|
+
end
|
38
|
+
|
39
|
+
# Remote Call
|
40
|
+
def http_request
|
41
|
+
JSON.parse http_request_without_parse
|
42
|
+
end
|
43
|
+
|
44
|
+
def auth_key
|
45
|
+
@auth_key || Cleantalk.auth_key
|
46
|
+
end
|
47
|
+
|
48
|
+
def method_name= value
|
49
|
+
@method_name = self.class::METHOD || value
|
50
|
+
end
|
51
|
+
|
52
|
+
def result
|
53
|
+
@result ||= http_request
|
54
|
+
end
|
55
|
+
|
56
|
+
def allowed?
|
57
|
+
self.result.allow == 1
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def valid?
|
63
|
+
[:auth_key, :method_name].freeze.each do |required_param|
|
64
|
+
raise Cleantalk::Request::BadParameters, "params `#{required_param}` is required for #{self.class}" if send(required_param).nil?
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
API_URI = URI.parse('https://moderate.cleantalk.org/api2.0').freeze
|
69
|
+
API_HEADERS = {'Content-Type' =>'application/json'}.freeze
|
70
|
+
METHOD = nil
|
71
|
+
class Cleantalk::Request::BadParameters < StandardError; end
|
72
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class Cleantalk::Result
|
2
|
+
attr_reader :id, :version, :inactive, :js_disabled, :blacklisted,
|
3
|
+
:comment, :codes,:fast_submit, :account_status, :allow
|
4
|
+
|
5
|
+
def initialize body
|
6
|
+
body = body.is_a?(String) ? JSON.parse(body) : body
|
7
|
+
body.each do |meth, value|
|
8
|
+
instance_variable_set("@#{meth}", value) if respond_to? meth
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cleantalk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ''
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-09-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -45,7 +45,13 @@ extensions: []
|
|
45
45
|
extra_rdoc_files: []
|
46
46
|
files:
|
47
47
|
- lib/cleantalk.rb
|
48
|
-
|
48
|
+
- lib/cleantalk/check_message.rb
|
49
|
+
- lib/cleantalk/check_message_result.rb
|
50
|
+
- lib/cleantalk/check_newuser.rb
|
51
|
+
- lib/cleantalk/check_newuser_result.rb
|
52
|
+
- lib/cleantalk/request.rb
|
53
|
+
- lib/cleantalk/result.rb
|
54
|
+
homepage: https://github.com/CleanTalk/ruby-antispam
|
49
55
|
licenses:
|
50
56
|
- MIT
|
51
57
|
metadata: {}
|