0
- mattr_accessor :config_file
0
- self.config_file = RAILS_ROOT + '/config/defensio.yml'
0
- # Add Defensio magic for managing a comment or something that
0
- # can be marked as Spam or Ham.
0
- # By default those following fields are maped to the column of the
0
- # same name, if the column exists:
0
- # author, content, title, author_email, author_url,
0
- # user_logged_in, trusted_user, article.permalink
0
- # See +acts_as_defensio+ for more details.
0
- def acts_as_defensio_comment(options={})
0
- acts_as_defensio :comment, options
0
- # Add Defensio magic for managing an article or something that
0
- # can be commented (spamed) about.
0
- # By default those following fields are maped to the column of the
0
- # same name, if the column exists:
0
- # author, author_email, title, content, permalink
0
- # By default the article will be published after creation. You can
0
- # override this behaviour with the :announce_when option. Specified
0
- # a method returning if the article needs to be announced to the
0
- # Defensio server. If you're using the :announce_when option, you'll
0
- # also need to create an announced column as this will be set to
0
- # +true+ when the article is published.
0
- # See +acts_as_defensio+ for more details.
0
- def acts_as_defensio_article(options={})
0
- acts_as_defensio :article, options
0
- # Add Defensio magic to an ActiveRecord class.
0
- # Options are the same as +Defensio::Client#new+ plus the <tt>:fields</tt>
0
- # option that allows to customize the fields that will be sent to
0
- # Defensio to help the classification of a comment.
0
- # For example if the content of a comment is stored in the comment
0
- # column set the <tt>:fields</tt> option to <tt>{ :content => :comment }</tt>.
0
- # Try to map as much column as possible as this helps Defensio to
0
- # classify one comment effectively.
0
- # You can also set the type of comment with the :comment_type option.
0
- # acts_as_defensio_comment :owner_url => 'http://code.macournoyer.com/svn/plugins/defensio',
0
- # :fields => { :content => :comment }
0
- # You must specify the <tt>:api_key</tt> and <tt>:owner_url</tt> option.
0
- # Other options are optional.
0
- # All options can be specified in a YAML config file by default in
0
- # RAILS_ROOT/config/defensio.yml.
0
- def acts_as_defensio(type, options={})
0
- include InstanceMethods
0
- if options.has_key? :announce_when
0
- after_save :announce_article
0
- after_create :announce_article!
0
- after_create :audit_comment
0
- self.defensio_options = options
0
- @defensio = Defensio::Client.new(@defensio_options)
0
- unless defensio_options.has_key?(:validate_key) && !defensio_options[:validate_key]
0
- raise Defensio::InvalidAPIKey unless @defensio.validate_key.success?
0
- def defensio_options=(options)
0
- @defensio_options = {}
0
- @defensio_options.merge! File.open(Defensio.config_file) { |file| YAML.load(file) }[ENV['RAILS_ENV']] if File.exists?(Defensio.config_file)
0
- @defensio_options.merge! options
0
- @defensio_options.symbolize_keys!
0
- def defensio_fields(field)
0
- (@defensio_options[:fields] || {})[field] || field
0
- @defensio_options[:comment_type] || 'comment'
0
- module InstanceMethods
0
- def self.included(base)
0
- alias_method :report_as_spam, :report_as_false_negative
0
- alias_method :report_as_ham, :report_as_false_positive
0
- raise Defensio::Error,
0
- "You have to pass the current request environement:\n\t@comment.env = request.env" unless @env
0
- article_field = self.class.defensio_fields(:article)
0
- raise Defensio::Error,
0
- "You must specify an assiociated object which acts_as_defensio_article" unless respond_to? article_field
0
- article = send article_field
0
- fields = { :user_ip => @env['REMOTE_ADDR'],
0
- :referrer => @env['HTTP_REFERER'],
0
- :article_date => convert_to_defensio_date(article.created_at),
0
- :comment_type => self.class.comment_type }
0
- fields.merge! extract_optional_fields_value(self, :author, :content, :title, :author_email, :author_url, :prefix => 'comment_')
0
- fields.merge! extract_optional_fields_value(self, :user_logged_in, :trusted_user)
0
- fields.merge! extract_optional_fields_value(article, :permalink)
0
- log_and_ignore_error do
0
- response = self.class.defensio.audit_comment fields
0
- raise Defensio::Error, response.message unless response.success?
0
- self.signature = response.signature
0
- self.spam = response.spam
0
- self.spaminess = response.spaminess
0
- announce_when = self.class.defensio_options[:announce_when]
0
- announced_field = self.class.defensio_fields(:announced)
0
- raise Defensio::Error, "announced field not found" unless respond_to? announced_field
0
- return unless send(announce_when) && !send(announced_field)
0
- fields.merge! extract_optional_fields_value(self, :author, :author_email, :title, :content, :prefix => 'article_')
0
- fields.merge! extract_optional_fields_value(self, :permalink)
0
- log_and_ignore_error do
0
- response = self.class.defensio.announce_article fields
0
- raise Defensio::Error, response.message unless response.success?
0
- def report_as_false_negative
0
- log_and_ignore_error do
0
- self.class.defensio.report_false_negatives :signatures => self.signature
0
- def report_as_false_positive
0
- log_and_ignore_error do
0
- self.class.defensio.report_false_positives :signatures => self.signature
0
- def convert_to_defensio_date(date)
0
- [date.strftime('%Y'), date.month, date.day] * '/'
0
- def extract_optional_fields_value(object, *fields)
0
- options = fields.last.is_a?(Hash) ? fields.pop : {}
0
- prefix = options.delete(:prefix)
0
- fields.inject({}) do |hash, field|
0
- field_name = object.class.defensio_fields(field)
0
- hash[:"#{prefix}#{field}"] = object.send(field_name) if object.respond_to? field_name
0
- def log_and_ignore_error
0
- RAILS_DEFAULT_LOGGER.error "[DEFENSIO] Could not contact server : #{e}"
0
-ActiveRecord::Base.extend Defensio::ActsAs::ClassMethods