Skip to content

Support for spam check API #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/cleantalk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def self.auth_key= value
require 'cleantalk/request'
require 'cleantalk/check_newuser'
require 'cleantalk/check_message'
require 'cleantalk/spam_check'
require 'cleantalk/result'
require 'cleantalk/check_newuser_result'
require 'cleantalk/check_message_result'
require 'cleantalk/spam_check_result'
35 changes: 26 additions & 9 deletions lib/cleantalk/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,35 @@ def initialize(params = {})
end
end

def http_request_without_parse
def http_request_without_parse(api = nil, request_type = 'post')
valid?
form_data = self.instance_variables.inject({}) do |params, var_name|
param_key = var_name.to_s.sub('@','')
params[param_key] = send(param_key)
params
if api.nil?
form_data = self.instance_variables.inject({}) do |params, var_name|
param_key = var_name.to_s.sub('@', '')
params[param_key] = send(param_key)
params
end
else
uri = URI.parse(api)
self.instance_variables.inject({}) do |params, var_name|
param_key = var_name.to_s.sub('@', '')
new_query_ar = URI.decode_www_form(uri.query || '') << [param_key, send(param_key)]
uri.query = URI.encode_www_form(new_query_ar)
end
end

req = Net::HTTP::Post.new(API_URI, API_HEADERS)
req.body = JSON.generate(form_data)
response = Net::HTTP.start(API_URI.hostname, API_URI.port, use_ssl: true) do |http|
http.request(req)
case request_type
when 'post'
req = Net::HTTP::Post.new(API_URI, API_HEADERS)
req.body = JSON.generate(form_data)
response = Net::HTTP.start(API_URI.hostname, API_URI.port, use_ssl: true) do |http|
http.request(req)
end
when 'get'
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
req = Net::HTTP::Get.new(uri)
response = http.request(req)
end

response.entity
Expand Down
14 changes: 13 additions & 1 deletion lib/cleantalk/result.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
class Cleantalk::Result
attr_reader :id, :version, :inactive, :js_disabled, :blacklisted,
:comment, :codes,:fast_submit, :account_status, :allow
:comment, :codes, :fast_submit, :account_status, :allow,
:error_no, :error_message

def initialize body
body = body.is_a?(String) ? JSON.parse(body) : body
body.each do |meth, value|
instance_variable_set("@#{meth}", value) if respond_to? meth
end
raise_error?
end

private

def raise_error?
if send(:error_no).present? || send(:error_message).present?
raise Cleantalk::Result::ApiErrors, "Error no: #{send(:error_no)}, Error Message: #{send(:error_message)}"
end
end

class Cleantalk::Result::ApiErrors < StandardError; end
end
19 changes: 19 additions & 0 deletions lib/cleantalk/spam_check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'net/http'
require 'json'

class Cleantalk::SpamCheck < Cleantalk::Request
attr_accessor :ip, :email, :date

def result
@result ||= Cleantalk::SpamCheckResult.new(http_request_without_parse(SPAMCHECK_API, REQUEST_TYPE))
end

def blacklist?(email_or_ip)
return nil if !self.result.data.empty? && self.result.data[email_or_ip].nil?
self.result.data[email_or_ip]['appears'] == 1
end

SPAMCHECK_API = 'https://api.cleantalk.org'.freeze
REQUEST_TYPE = 'get'.freeze
METHOD = 'spam_check'.freeze
end
6 changes: 6 additions & 0 deletions lib/cleantalk/spam_check_result.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Cleantalk::SpamCheckResult < Cleantalk::Result
attr_reader :data
# :id, :appears, :sha256, :network_type,
# :spam_rate, :frequency, :frequency_time_10m,
# :frequency_time_1h, :frequency_time_24h
end
64 changes: 64 additions & 0 deletions spec/spam_check_result_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'spec_helper'

describe Cleantalk::SpamCheckResult do
let :clean_init_values do
{"data" =>
{
"[email protected]" =>
{
"appears" => 0,
"sha256" => '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08',
"network_type" => 'public',
"spam_rate" => '3',
"frequency" => '5',
"frequency_time_10m" => '1',
"frequency_time_1h" => '2',
"frequency_time_24h" => '4'
},
"127.0.0.1" =>
{
"appears" => 0,
"sha256" => '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08',
"network_type" => 'hosting',
"spam_rate" => '4',
"frequency" => '1',
"frequency_time_10m" => '3',
"frequency_time_1h" => '2',
"frequency_time_24h" => '1'
}
}

}
end
let :init_values do
clean_init_values.merge({
"lalilulelo" => "patriot"
})
end

describe "#initialize" do
it 'pass all result from an Hash' do
instance = described_class.new(init_values)

clean_init_values.each do |key, value|
expect(instance.send(key)).to eql(value)
end

expect do
instance.lalilulelo
end.to raise_error(NoMethodError)
end
end

it 'pass all result from JSON string' do
instance = described_class.new(JSON.fast_generate(init_values))

clean_init_values.each do |key, value|
expect(instance.send(key)).to eql(value)
end

expect do
instance.lalilulelo
end.to raise_error(NoMethodError)
end
end
132 changes: 132 additions & 0 deletions spec/spam_check_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
require 'spec_helper'

describe Cleantalk::SpamCheck do
# Testing values
let :headers do
{
'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type' => 'application/json', 'User-Agent' => 'Ruby', 'Host' => 'api.cleantalk.org'
}
end

let :res_body_spam do
{"data" =>
{
"[email protected]" =>
{
"appears" => 1,
"sha256" => '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08',
"network_type" => 'public',
"spam_rate" => '3',
"frequency" => '5',
"frequency_time_10m" => '1',
"frequency_time_1h" => '2',
"frequency_time_24h" => '4'
},
"127.0.0.1" =>
{
"appears" => 1,
"sha256" => '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08',
"network_type" => 'hosting',
"spam_rate" => '4',
"frequency" => '1',
"frequency_time_10m" => '3',
"frequency_time_1h" => '2',
"frequency_time_24h" => '1'
}
}

}
end

let :res_body_not_spam do
{"data" =>
{
"[email protected]" =>
{
"appears" => 0,
"sha256" => '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08',
"network_type" => 'hosting',
"spam_rate" => '0',
"frequency" => '0',
"frequency_time_10m" => '0',
"frequency_time_1h" => '0',
"frequency_time_24h" => '0'
},
"127.0.0.1" =>
{
"appears" => 0,
"sha256" => '9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08',
"network_type" => 'hosting',
"spam_rate" => '0',
"frequency" => '0',
"frequency_time_10m" => '0',
"frequency_time_1h" => '0',
"frequency_time_24h" => '0'
}
}
}
end

let :base_parameters do
{method_name: "spam_check", auth_key: 'test', email: '[email protected]', ip: '127.0.0.1'}
end

let :request do
described_class.new(base_parameters.merge({method_name: "unknow_method"}))
end

# Specs
it 'create a request' do
subject.auth_key = 'test'
expect(subject.auth_key).to eql('test')
expect(subject.is_a? Cleantalk::Request).to eql(true)
expect(subject.method_name).to eql("spam_check")
end

describe "#initialize" do
it 'can pass params definition' do
expect(request.auth_key).to eql('test')
expect(request.email).to eql('[email protected]')
expect(request.is_a? Cleantalk::Request).to eql(true)
expect(request.method_name).to eql("spam_check")
expect(request.ip).to eql(base_parameters[:ip])
end
end

describe "#blacklist?" do
it "call one time request and return true when blacklisted" do
stub_request(:get, "https://api.cleantalk.org/?auth_key=test&[email protected]&ip=127.0.0.1&method_name=spam_check").
with(
headers: {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Host' => 'api.cleantalk.org',
'User-Agent' => 'Ruby'
}).
to_return(status: 200, body: JSON.dump(res_body_spam), headers: {})

expect(request.blacklist?('[email protected]')).to eql(true)
expect_any_instance_of(described_class).to_not receive(:spam_check_http_request_without_parse)
expect(request.blacklist?('127.0.0.1')).to eql(true)
expect(request.blacklist?('randomtext')).to eql(nil)
end

it "call one time request and return false when not_blacklisted" do
stub_request(:get, "https://api.cleantalk.org/?auth_key=test&[email protected]&ip=127.0.0.1&method_name=spam_check").
with(
headers: {
'Accept' => '*/*',
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Host' => 'api.cleantalk.org',
'User-Agent' => 'Ruby'
}).
to_return(status: 200, body: JSON.dump(res_body_not_spam), headers: {})

expect(request.blacklist?('[email protected]')).to eql(false)
expect_any_instance_of(described_class).to_not receive(:spam_check_http_request_without_parse)
expect(request.blacklist?('127.0.0.1')).to eql(false)
expect(request.blacklist?('randomtext')).to eql(nil)
end
end
end