Module: Defender::Spammable::InstanceMethods

Defined in:
lib/defender/spammable.rb

Overview

Public: Methods that will be included as instance methods when including Defender::Spammable into your model.

Instance Method Summary collapse

Instance Method Details

#defensio_data(data = {}) ⇒ Object

Public: Pass in more data to be sent to Defensio. You should use this for data you don’t want to save in the model, for instance HTTP headers.

This can be called several times, the new data will be merged into the existing data. If you use the same key twice, the new value will overwrite the old.

data - The Hash data to send to Defensio. Check the README for the

possible keys.

Examples

def create # A Rails controller action
  @comment = Comment.new(params[:comment])
  @comment.defensio_data(
    'http-headers' => request.env.map {|k,v| "#{k}: #{v}" }.join("\n")
  )
end

Returns the data to be sent.



158
159
160
161
162
# File 'lib/defender/spammable.rb', line 158

def defensio_data(data={})
  @_defensio_data ||= {}
  @_defensio_data.merge!(data)
  @_defensio_data
end

#false_negative!Object

Public: Report a false negative to Defensio and update the spam attribute.

A false negative is a spammy comment incorrectly marked as legitimate.

This must be done within 30 days of the comment originally being submitted. If you need to update this after that, just set the spam attribute on your model and save it.

Raises a Defender::DefenderError if Defensio returns an error.



130
131
132
133
134
135
136
# File 'lib/defender/spammable.rb', line 130

def false_negative!
  document = Defender.defensio.put_document(self.defensio_sig, {'allow' => 'false'}).last
  if document['status'] == 'failed'
    raise DefenderError, document['message']
  end
  update_attribute(:spam, true)
end

#false_positive!Object

Public: Report a false positive to Defensio and update the spam attribute.

A false positive is a legitimate comment incorrectly marked as spam.

This must be done within 30 days of the comment originally being submitted. If you need to update this after that, just set the spam attribute on your model and save it.

Raises a Defender::DefenderError if Defensio returns an error.



112
113
114
115
116
117
118
# File 'lib/defender/spammable.rb', line 112

def false_positive!
  document = Defender.defensio.put_document(self.defensio_sig, {'allow' => 'true'}).last
  if document['status'] == 'failed'
    raise DefenderError, document['message']
  end
  update_attribute(:spam, false)
end

#spam?Boolean

Public: Whether the comment is recognized a malicious comment or as spam.

Returns the Boolean value stored in the database, or nil if the comment

hasn't been submitted to Defensio yet.

Raises Defender::DefenderError if there is no spam attribute in the

model.


92
93
94
95
96
97
98
99
100
# File 'lib/defender/spammable.rb', line 92

def spam?
  if self.new_record?
    nil
  elsif self.respond_to?(:spam) && !self.spam.nil?
    return self.spam
  else
    raise Defender::DefenderError, 'You need to add a spam attribute to the model'
  end
end