Module: ValidatesAgainstStopForumSpam::ClassMethods

Defined in:
lib/validates_against_stopforumspam.rb

Instance Method Summary collapse

Instance Method Details

#validates_against_stopforumspam(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/validates_against_stopforumspam.rb', line 8

def validates_against_stopforumspam(options = {})
	attr_names = {}
	[ :username, :email, :ip, :comment].each do |param|
		attr_names[param] = options.delete(param) || param
	end
	validate options do |comment|
		query_options = []
		attr_names.each do |param,attr_name|
			if comment.respond_to? attr_name
				value = comment.send attr_name
				query_options << "#{param}=#{URI.escape(value)}" unless value.nil?
			end
		end
		return if query_options.empty?

		url = "http://www.stopforumspam.com/api?" + query_options.join('&')
      if Rails.env.production?
        logger.info "Querying StopForumSpam: #{url}"
        begin
          response = Hash.from_xml(Net::HTTP.get(URI.parse(url)))
          logger.debug response.inspect
          errors.add :base, :spam_according_to_stopforumspam if [ response["response"]["appears"] ].flatten.include?("yes")
        rescue StandardError => e
          logger.warn "Couldn't validate against StopForumSpam: + #{e.message}"
        end
      else
        logger.info "Would query StopForumSpam: #{url} in production mode"
      end
	end
end