Class: Akismet::Client
- Inherits:
-
Object
- Object
- Akismet::Client
- Defined in:
- lib/akismet/client.rb
Instance Attribute Summary collapse
-
#api_key ⇒ String
readonly
The API key obtained at akismet.com.
-
#app_name ⇒ String
readonly
The name of the application making the request.
-
#app_url ⇒ String
readonly
A URL that identifies the application making the request.
-
#app_version ⇒ String
readonly
The version of the application making the request.
Constructors collapse
-
#initialize(api_key, app_url, options = {}) ⇒ Client
constructor
A new instance of Client.
Managing Connections collapse
-
.open(api_key, app_url, options = {}) {|client| ... } ⇒ Object
Initializes a client, opens it, yields it to the given block, and closes it when the block returns.
-
#close ⇒ self
Closes the Client.
-
#open ⇒ Object
Opens the client, creating a new TCP connection.
-
#open? ⇒ Boolean
Whether the Client is open.
Verifying Keys collapse
-
#verify_key ⇒ Boolean
Checks the validity of the API key.
Checking collapse
-
#check(user_ip, user_agent, params = {}) ⇒ (Boolean, Boolean)
(also: #comment_check)
Checks whether a comment is spam and whether it is “blatant.”.
-
#spam?(user_ip, user_agent, params = {}) ⇒ Boolean
Checks whether a comment is spam.
Reporting collapse
-
#ham(user_ip, user_agent, params = {}) ⇒ void
(also: #submit_ham)
Submits a comment that has been identified as not-spam (ham).
-
#spam(user_ip, user_agent, params = {}) ⇒ void
(also: #submit_spam)
Submits a comment that has been identified as spam.
Constructor Details
#initialize(api_key, app_url, options = {}) ⇒ Client
Returns a new instance of Client.
39 40 41 42 43 44 45 |
# File 'lib/akismet/client.rb', line 39 def initialize(api_key, app_url, = {}) @api_key = api_key @app_url = app_url @app_name = [ :app_name ] @app_version = [ :app_version ] @http_session = nil end |
Instance Attribute Details
#api_key ⇒ String (readonly)
The API key obtained at akismet.com
11 12 13 |
# File 'lib/akismet/client.rb', line 11 def api_key @api_key end |
#app_name ⇒ String (readonly)
The name of the application making the request
19 20 21 |
# File 'lib/akismet/client.rb', line 19 def app_name @app_name end |
#app_url ⇒ String (readonly)
A URL that identifies the application making the request
15 16 17 |
# File 'lib/akismet/client.rb', line 15 def app_url @app_url end |
#app_version ⇒ String (readonly)
The version of the application making the request
23 24 25 |
# File 'lib/akismet/client.rb', line 23 def app_version @app_version end |
Class Method Details
.open(api_key, app_url, options = {}) {|client| ... } ⇒ Object
Initializes a client, opens it, yields it to the given block, and closes it when the block returns. Allows you to perform several operations over a single TCP connection.
59 60 61 62 63 |
# File 'lib/akismet/client.rb', line 59 def self.open(api_key, app_url, = {}) raise "Block required" unless block_given? client = new(api_key, app_url, ) client.open { yield client } end |
Instance Method Details
#check(user_ip, user_agent, params = {}) ⇒ (Boolean, Boolean) Also known as: comment_check
Checks whether a comment is spam and whether it is “blatant.”
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/akismet/client.rb', line 192 def check(user_ip, user_agent, params = {}) response = invoke_comment_method('comment-check', user_ip, user_agent, params) unless %w{ true false }.include?(response.body) raise_with_response response end [ response.body == 'true', response['X-akismet-pro-tip'] == 'discard' ] end |
#close ⇒ self
Closes the Client.
112 113 114 115 116 |
# File 'lib/akismet/client.rb', line 112 def close @http_session.finish if open? @http_session = nil self end |
#ham(user_ip, user_agent, params = {}) ⇒ void Also known as: submit_ham
This method returns an undefined value.
Submits a comment that has been identified as not-spam (ham).
223 224 225 226 227 228 229 230 231 232 |
# File 'lib/akismet/client.rb', line 223 def ham(user_ip, user_agent, params = {}) response = invoke_comment_method('submit-ham', user_ip, user_agent, params) unless response.body == 'Thanks for making the web a better place.' raise_with_response response end end |
#open { ... } ⇒ Object #open ⇒ self
Opens the client, creating a new TCP connection.
If a block is given, yields to the block, closes the client when the block returns, and returns the return value of the block. If a block is not given, returns self and leaves the client open, relying on the caller to close the client with #close.
Note that opening and closing the client is only required if you want to make several calls over one TCP connection. Otherwise, you can simply call #spam?, #check, #ham, or #spam, which call #open for you if necessary.
Due to a peculiarity of the Akismet API, #verify_key always creates its own connection.
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/akismet/client.rb', line 94 def open raise "Already open" if open? @http_session = Net::HTTP.new("#{ api_key }.rest.akismet.com", Net::HTTP.https_default_port) @http_session.use_ssl = true begin @http_session.start block_given? ? yield : self ensure close if block_given? end end |
#open? ⇒ Boolean
Whether the Client is open.
121 122 123 |
# File 'lib/akismet/client.rb', line 121 def open? @http_session && @http_session.started? end |
#spam(user_ip, user_agent, params = {}) ⇒ void Also known as: submit_spam
This method returns an undefined value.
Submits a comment that has been identified as spam.
239 240 241 242 243 244 245 246 247 248 |
# File 'lib/akismet/client.rb', line 239 def spam(user_ip, user_agent, params = {}) response = invoke_comment_method('submit-spam', user_ip, user_agent, params) unless response.body == 'Thanks for making the web a better place.' raise_with_response response end end |
#spam?(user_ip, user_agent, params = {}) ⇒ Boolean
Checks whether a comment is spam.
213 214 215 |
# File 'lib/akismet/client.rb', line 213 def spam?(user_ip, user_agent, params = {}) check(user_ip, user_agent, params)[0] end |
#verify_key ⇒ Boolean
Checks the validity of the API key.
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/akismet/client.rb', line 130 def verify_key response = Net::HTTP.start('rest.akismet.com', use_ssl: true) do |session| invoke session, 'verify-key', blog: app_url, key: api_key end unless %w{ valid invalid }.include?(response.body) raise_with_response response end response.body == 'valid' end |