class Connoisseur::Service

Attributes

key[R]
user_agent[R]

Public Class Methods

new(key:, user_agent:) click to toggle source

Internal: Initialize a Connoisseur service.

key - An Akismet API key, obtained from akismet.com. user_agent - The String value to provide in the User-Agent header when issuing

HTTP requests to the Akismet API.

Raises ArgumentError if the key is nil or blank.

# File lib/connoisseur/service.rb, line 11
def initialize(key:, user_agent:)
  @key, @user_agent = key, user_agent

  require_usable_key
end

Public Instance Methods

check(comment) click to toggle source

Internal: Determine whether a comment is spam or ham.

comment - A Hash of POST parameters describing the comment.

Returns a Connoisseur::Result. Raises Connoisseur::Result::InvalidError if the Akismet API provides an unexpected response.

# File lib/connoisseur/service.rb, line 23
def check(comment)
  Connoisseur::Result.new(post("comment-check", body: comment)).validated
end
ham!(comment) click to toggle source

Internal: Inform Akismet that a comment should have been marked ham.

comment - A Hash of POST parameters describing the comment.

Returns nothing.

# File lib/connoisseur/service.rb, line 41
def ham!(comment)
  post "submit-ham", body: comment
end
spam!(comment) click to toggle source

Internal: Inform Akismet that a comment should have been marked spam.

comment - A Hash of POST parameters describing the comment.

Returns nothing.

# File lib/connoisseur/service.rb, line 32
def spam!(comment)
  post "submit-spam", body: comment
end
verify_key_for(blog:) click to toggle source

Internal: Verify the service's Akismet API key.

blog - The URL of the blog associated with the key.

Returns true or false indicating whether the key is valid for the given blog.

# File lib/connoisseur/service.rb, line 50
def verify_key_for(blog:)
  post_without_subdomain("verify-key", body: { key: key, blog: blog }).body == "valid"
end

Private Instance Methods

headers() click to toggle source
# File lib/connoisseur/service.rb, line 73
def headers
  { "User-Agent" => user_agent }
end
post(endpoint, body:) click to toggle source
# File lib/connoisseur/service.rb, line 63
def post(endpoint, body:)
  Net::HTTP.post URI("https://#{key}.rest.akismet.com/1.1/#{endpoint}"),
    URI.encode_www_form(body), headers
end
post_without_subdomain(endpoint, body:) click to toggle source
# File lib/connoisseur/service.rb, line 68
def post_without_subdomain(endpoint, body:)
  Net::HTTP.post URI("https://rest.akismet.com/1.1/#{endpoint}"),
    URI.encode_www_form(body), headers
end
require_usable_key() click to toggle source
# File lib/connoisseur/service.rb, line 58
def require_usable_key
  raise ArgumentError, "Expected Akismet API key, got #{key.inspect}" if !key || key =~ /\A[[:space:]]*\z/
end