class StopForumSpam::Check

Public Class Methods

new(checks = []) click to toggle source
# File lib/stopforumspam.rb, line 7
def initialize(checks = [])
  @checks = parse_checks([*checks])
end

Public Instance Methods

result() click to toggle source
# File lib/stopforumspam.rb, line 11
def result
  @result ||= do_check
end
spammer?() click to toggle source
# File lib/stopforumspam.rb, line 15
def spammer?
  result.map{|type, checks| checks}.flatten.map{|check| check['appears']}.include? '1'
end

Private Instance Methods

check_type(check) click to toggle source
# File lib/stopforumspam.rb, line 47
def check_type(check)
  return 'email' if check =~ /.*@.*/

  begin
    check_ip = IPAddr.new(check)
    return 'ip' if check_ip.ipv4? or check_ip.ipv6?
  rescue IPAddr::InvalidAddressError
  end

  return 'username'
end
do_check() click to toggle source
# File lib/stopforumspam.rb, line 21
def do_check
  response = XmlSimple.xml_in(
    open(
      'http://api.stopforumspam.org/api?' + 
      @checks.map{|check| "#{check[:type]}[]=#{check[:check]}"}.join('&')
    ), 
    { 'ForceArray' => false }
  )

  raise "Unsuccesful response from API" unless response['success'] && (response.delete('success') == '1')

  return response
end
parse_checks(checks) click to toggle source
# File lib/stopforumspam.rb, line 35
def parse_checks(checks)
  raise ArgumentError, 'At least one argument required' if checks.length < 1
  return checks.map{|check|
    case check.class.to_s
    when 'String'
      {check: check, type: check_type(check)}
    when 'Hash'
      {check: check[:check], type: (check[:type] || check_type(check[:check]))}
    end
  }
end