Class: Akismet

Inherits:
Object
  • Object
show all
Defined in:
lib/akismet.rb

Constant Summary collapse

HOST =
'rest.akismet.com'
PORT =
80
TIMEOUT_THRESHOLD =
10
VALID_RESPONSES =
Set.new(['false', ''])
NORMAL_RESPONSES =
VALID_RESPONSES.dup << 'true'
STANDARD_HEADERS =
{
	'User-Agent'   => "Akismet Checker",
	'Content-Type' => 'application/x-www-form-urlencoded'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Akismet

Create a new instance of the Akismet class

Arguments

Arguments are provided in the form of a Hash with the following keys (as Symbols) available:

api_key

your Akismet API key

blog

the blog associated with your api key

The following keys are available and are entirely optional. They are available incase communication with Akismet’s servers requires a proxy port and/or host:

  • proxy_port

  • proxy_host



34
35
36
37
# File 'lib/akismet.rb', line 34

def initialize(options)
	@options = options
	self.verified_key = false
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



7
8
9
# File 'lib/akismet.rb', line 7

def host
  @host
end

#normal_responsesObject

Returns the value of attribute normal_responses.



7
8
9
# File 'lib/akismet.rb', line 7

def normal_responses
  @normal_responses
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/akismet.rb', line 7

def options
  @options
end

#portObject

Returns the value of attribute port.



7
8
9
# File 'lib/akismet.rb', line 7

def port
  @port
end

#standard_headersObject

Returns the value of attribute standard_headers.



7
8
9
# File 'lib/akismet.rb', line 7

def standard_headers
  @standard_headers
end

#valid_responsesObject

Returns the value of attribute valid_responses.



7
8
9
# File 'lib/akismet.rb', line 7

def valid_responses
  @valid_responses
end

Class Method Details

.url(action) ⇒ Object

Returns the URL for an Akismet request

Arguments

action <~to_s>

a valid Akismet function name

Returns

String



84
85
86
# File 'lib/akismet.rb', line 84

def self.url(action)
	"/1.1/#{action}"
end

Instance Method Details

#check_comment(options = {}) ⇒ Object



48
49
50
51
52
# File 'lib/akismet.rb', line 48

def check_comment(options={})
	return false if invalid_options?
	message = call_akismet('comment-check', options)
	{:spam => !VALID_RESPONSES.include?(message), :message => message}
end

#invalid_options?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/akismet.rb', line 44

def invalid_options?
	false
end

#mark_as_ham(options = {}) ⇒ Object

This call is intended for the marking of false positives, things that were incorrectly marked as spam. It takes identical arguments as check_comment and mark_as_spam.



72
73
74
75
# File 'lib/akismet.rb', line 72

def mark_as_ham(options={})
	return false if invalid_options?
	{:message => call_akismet('submit-ham', options)}
end

#mark_as_spam(options = {}) ⇒ Object

This call is for submitting comments that weren’t marked as spam but should have been (i.e. false negatives). It takes identical arguments as check_comment.



64
65
66
67
# File 'lib/akismet.rb', line 64

def mark_as_spam(options={})
	return false if invalid_options?
	{:message => call_akismet('submit-spam', options)}
end

#spam?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
# File 'lib/akismet.rb', line 54

def spam?(options = {})
	if resp = check_comment(options)
		return resp[:spam]
	end
	false
end

#verified?Boolean

Returns true if the API key has been verified, false otherwise

Returns:

  • (Boolean)


40
41
42
# File 'lib/akismet.rb', line 40

def verified?
	(@verified_key ||= verify_api_key) != :false
end