diff --git a/lib/cleantalk.rb b/lib/cleantalk.rb index 45ecbd7..a547ae8 100644 --- a/lib/cleantalk.rb +++ b/lib/cleantalk.rb @@ -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' \ No newline at end of file diff --git a/lib/cleantalk/request.rb b/lib/cleantalk/request.rb index f9fc2ca..abb04b1 100644 --- a/lib/cleantalk/request.rb +++ b/lib/cleantalk/request.rb @@ -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 diff --git a/lib/cleantalk/result.rb b/lib/cleantalk/result.rb index 55a5401..ff4dad5 100644 --- a/lib/cleantalk/result.rb +++ b/lib/cleantalk/result.rb @@ -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 diff --git a/lib/cleantalk/spam_check.rb b/lib/cleantalk/spam_check.rb new file mode 100644 index 0000000..8b02dee --- /dev/null +++ b/lib/cleantalk/spam_check.rb @@ -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 diff --git a/lib/cleantalk/spam_check_result.rb b/lib/cleantalk/spam_check_result.rb new file mode 100644 index 0000000..b1bf1dd --- /dev/null +++ b/lib/cleantalk/spam_check_result.rb @@ -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 diff --git a/spec/spam_check_result_spec.rb b/spec/spam_check_result_spec.rb new file mode 100644 index 0000000..a048573 --- /dev/null +++ b/spec/spam_check_result_spec.rb @@ -0,0 +1,64 @@ +require 'spec_helper' + +describe Cleantalk::SpamCheckResult do + let :clean_init_values do + {"data" => + { + "test@example.org" => + { + "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 diff --git a/spec/spam_check_spec.rb b/spec/spam_check_spec.rb new file mode 100644 index 0000000..e6fcb61 --- /dev/null +++ b/spec/spam_check_spec.rb @@ -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" => + { + "test@example.org" => + { + "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" => + { + "test@example.org" => + { + "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: 'test@example.org', 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('test@example.org') + 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=test@example.org&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?('test@example.org')).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=test@example.org&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?('test@example.org')).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