Class: Akismet::Client
- Inherits:
-
Object
- Object
- Akismet::Client
- Defined in:
- lib/akismet/client.rb
Overview
A Ruby client for the Akismet API.
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, e.g “jonahb.com”.
-
#app_version ⇒ String
readonly
The version of the application making the request, e.g.
-
#home_url ⇒ String
readonly
The URL of the home page of the application making the request.
Class Method Summary collapse
-
.open(api_key, home_url, options = {}) {|Akismet::Client| ... } ⇒ nil
Yields a new open Client, closing the Client when the block returns.
Instance Method Summary collapse
-
#close ⇒ self
Closes the Client.
-
#comment_check(user_ip, user_agent, params = {}) ⇒ boolean
Checks whether a comment is spam.
-
#initialize(api_key, home_url, options = {}) ⇒ Client
constructor
A new instance of Client.
-
#open ⇒ self
Opens the client.
-
#open? ⇒ boolean
Whether the Client is open.
-
#submit_ham(user_ip, user_agent, params = {}) ⇒ nil
Submits a comment that has been identified as not-spam (ham).
-
#submit_spam(user_ip, user_agent, params = {}) ⇒ nil
Submits a comment that has been identified as spam.
-
#verify_key ⇒ boolean
Checks the validity of the API key.
Constructor Details
#initialize(api_key, home_url, options = {}) ⇒ Client
Returns a new instance of Client.
109 110 111 112 113 114 |
# File 'lib/akismet/client.rb', line 109 def initialize( api_key, home_url, = {} ) @api_key = api_key @home_url = home_url @app_name = [ :app_name ] @app_version = [ :app_version ] end |
Instance Attribute Details
#api_key ⇒ String (readonly)
The API key obtained at akismet.com.
76 77 78 |
# File 'lib/akismet/client.rb', line 76 def api_key @api_key end |
#app_name ⇒ String (readonly)
The name of the application making the request, e.g “jonahb.com”.
88 89 90 |
# File 'lib/akismet/client.rb', line 88 def app_name @app_name end |
#app_version ⇒ String (readonly)
The version of the application making the request, e.g. “1.0”.
94 95 96 |
# File 'lib/akismet/client.rb', line 94 def app_version @app_version end |
#home_url ⇒ String (readonly)
The URL of the home page of the application making the request.
82 83 84 |
# File 'lib/akismet/client.rb', line 82 def home_url @home_url end |
Class Method Details
.open(api_key, home_url, options = {}) {|Akismet::Client| ... } ⇒ nil
Yields a new open Client, closing the Client when the block returns. Takes the same arguments as #initialize. Use for submitting a batch of requests using a single TCP and HTTP session.
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/akismet/client.rb', line 126 def self.open( api_key, home_url, = {} ) raise "Block required" unless block_given? client = nil begin client = new( api_key, home_url, ) client.open yield client ensure client.close if client end nil end |
Instance Method Details
#close ⇒ self
Closes the Client.
169 170 171 172 173 |
# File 'lib/akismet/client.rb', line 169 def close @http_session.finish if open? @http_session = nil self end |
#comment_check(user_ip, user_agent, params = {}) ⇒ boolean
Checks whether a comment is spam. You are encouraged the submit, in addition to the documented parameters, data about the client and the comment submission. For example, if the client is an HTTP server, include HTTP headers and environment variables.
If the Client is not open, opens it for the duration of the call.
232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/akismet/client.rb', line 232 def comment_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' end |
#open ⇒ self
Opens the client. You may use this method in combination with #close to submit a batch of requests using a single TCP and HTTP session.
If you don’t call this before calling #comment_check, #submit_ham, or #submit_spam, a session will be created and opened for the duration of the call.
Note that calls to #verify_key always create a session. This is due to a peculiarity of the Akismet API where #verify_key requests must be sent to a different host.
157 158 159 160 161 162 |
# File 'lib/akismet/client.rb', line 157 def open raise "Already open" if open? @http_session = Net::HTTP.new( "#{ api_key }.rest.akismet.com", 80 ) @http_session.start self end |
#open? ⇒ boolean
Whether the Client is open.
179 180 181 |
# File 'lib/akismet/client.rb', line 179 def open? @http_session && @http_session.started? end |
#submit_ham(user_ip, user_agent, params = {}) ⇒ nil
Submits a comment that has been identified as not-spam (ham). Takes the same arguments as #comment_check. If the Client is not open, opens it for the duration of the call.
254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/akismet/client.rb', line 254 def submit_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_from_response response end nil end |
#submit_spam(user_ip, user_agent, params = {}) ⇒ nil
Submits a comment that has been identified as spam. Takes the same arguments as #comment_check. If the Client is not open, opens it for the duration of the call.
276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/akismet/client.rb', line 276 def submit_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_from_response response end nil end |
#verify_key ⇒ boolean
Checks the validity of the API key.
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/akismet/client.rb', line 187 def verify_key response = Net::HTTP.start( 'rest.akismet.com', 80 ) do |session| invoke( session, 'verify-key', :blog => home_url, :key => api_key ) end unless %w{ valid invalid }.include?( response.body ) raise_with_response response end response.body == 'valid' end |