Skip to content

Commit

Permalink
Merge pull request #6 from VladCleantalk/master
Browse files Browse the repository at this point in the history
fixed errors
  • Loading branch information
CleanTalk committed Oct 17, 2015
2 parents a177e22 + 405184b commit 875f4bd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
50 changes: 29 additions & 21 deletions cleantalk.class.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'net/http'
require 'json'

class CleantalkResponse {
class CleantalkResponse

#
# Is stop words
Expand Down Expand Up @@ -93,7 +93,7 @@ def initialize(response = nil, obl = nil)
@errno = obj.errno
@errstr = obj.errstr

@errstr = @errstr.gsub(/.+(\*\*\*.+\*\*\*).+/, \1, @errstr)
#@errstr = @errstr.gsub(/.+(\*\*\*.+\*\*\*).+/, \1, @errstr)

obj.stop_words.empty ? @stop_words = obj.stop_words : @stop_words = nil
obj.comment.empty ? @comment = obj.comment : @comment = nil
Expand All @@ -107,16 +107,19 @@ def initialize(response = nil, obl = nil)
obj.inactive.empty ? @inactive = obj.inactive : @inactive = 0
obj.account_status.empty ? @account_status = obj.account_status : @account_status = -1

if (@errno !== 0 && @errstr !== nil && @comment === nil)
if (@errno != 0 && @errstr != nil && @comment == nil)
@comment = '*** ' + @errstr + ' Antispam service cleantalk.org #**'
end
end
end
end


#
# Request class
#
class CleantalkRequest {
class CleantalkRequest
attr_accessor :all_headers, :last_error_no, :last_error_time, :last_error_text, :message, :example, :auth_key, :agent, :stoplist_check, :response_lang, :sender_ip, :sender_email, :sender_nickname, :sender_info, :post_info, :allow_links, :submit_time, :js_on, :tz, :feedback, :phone

#
# All http request headers
Expand Down Expand Up @@ -200,7 +203,7 @@ class CleantalkRequest {

#
# Sender info JSON string
# @var string
# @var hash
#
@sender_info = nil;

Expand All @@ -225,14 +228,14 @@ class CleantalkRequest {

#
# Is enable Java Script,
# valid are 0|1|2
# valid are 0|1
# Status:
# nil - JS html code not inserted into phpBB templates
# 0 - JS disabled at the client browser
# 1 - JS enabled at the client broswer
# @var int
#
@js_on = nil;
@js_on = 0;

#
# user time zone
Expand All @@ -258,7 +261,7 @@ class CleantalkRequest {
# @param type $params
#
def initialize(params = nil)
if (params.length>0)
if (params!=nil && params.length>0)
params.each_index{|elem|@elem = params.at(elem)}
end
end
Expand All @@ -267,7 +270,7 @@ def initialize(params = nil)
#
# Cleantalk class create request
#
class Cleantalk {
class Cleantalk

#
# Debug level
Expand All @@ -285,25 +288,25 @@ class Cleantalk {
# Cleantalk server url
# @var string
#
@server_url = null;
@server_url = nil;

#
# Last work url
# @var string
#
@work_url = null;
@work_url = nil;

#
# WOrk url ttl
# @var int
#
@server_ttl = null;
@server_ttl = nil;

#
# Time wotk_url changer
# @var int
#
@server_changed = null;
@server_changed = nil;

#
# Flag is change server url
Expand All @@ -315,7 +318,7 @@ class Cleantalk {
# Codepage of the data
# @var bool
#
@data_codepage = null;
@data_codepage = nil;

#
# API version to use
Expand Down Expand Up @@ -354,14 +357,19 @@ def isAllowUser(request)
end

def httpRequest(method_name, request)
uri = URI 'https://moderate.cleantalk.org/api2.0'
uri = URI 'http://moderate.cleantalk.org/api2.0'
connection = Net::HTTP.new uri.host, uri.port
http_request = Net::HTTP::Post.new uri
form_data = Array.new
request.each_index{|elem|form_data.elem = params.at(elem)}
form_data{:method_name => method_name}
http_request.set_form form_data, 'multipart/form-data'
response = connection.request http_request
form_data={}
attrs=request.instance_variables
attrs.each{|elem|form_data[elem.to_s.sub('@','')] = request.instance_variable_get(elem)}
form_data['method_name'] = method_name

req = Net::HTTP::Post.new(uri, initheader = {'Content-Type' =>'application/json'})
req.body = form_data.to_json
response = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
return JSON.parse(response.entity)
end
end
end
17 changes: 17 additions & 0 deletions example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require './cleantalk.class.rb'

sender_info = {cms_lang:'en_US'} #here put locale for your language
request = CleantalkRequest.new
request.auth_key = 'your_key'
request.message = 'test message' #don't use this field for registration check
request.sender_email = '[email protected]'
request.sender_nickname = 'test nickname'
request.sender_ip = '127.0.0.1'
request.js_on = 1 #you should check for JavaScript by yourself
request.submit_time = 11 # you should calculate time for submitting form by youself
request.sender_info = sender_info

ct = Cleantalk.new
result = ct.isAllowMessage(request) # for message checking
#result = ct.isAllowUser(request) # for registration checking
puts result

0 comments on commit 875f4bd

Please sign in to comment.