akismet 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,12 @@
1
+ require File.dirname( __FILE__ ) + '/akismet/version'
2
+ require File.dirname( __FILE__ ) + '/akismet/service'
3
+ require File.dirname( __FILE__ ) + '/akismet/client'
4
+
5
+ module Akismet
6
+
7
+ # convenience method
8
+ def self.verify_key( api_key, home_url )
9
+ Service.new.verify_key api_key, home_url
10
+ end
11
+
12
+ end
@@ -0,0 +1,41 @@
1
+ module Akismet
2
+ class Client
3
+
4
+ attr_accessor :api_key, :home_url
5
+
6
+ def initialize( api_key, home_url )
7
+ @api_key = api_key
8
+ @home_url = home_url
9
+ @service = Service.new
10
+ end
11
+
12
+ def verify_key
13
+ @service.verify_key @api_key, @home_url
14
+ end
15
+
16
+ def check( user_ip, user_agent, params = {} )
17
+ @service.comment_check @api_key,
18
+ @home_url,
19
+ user_ip,
20
+ user_agent,
21
+ params
22
+ end
23
+
24
+ def ham( user_ip, user_agent, params = {} )
25
+ @service.submit_ham @api_key,
26
+ @home_url,
27
+ user_ip,
28
+ user_agent,
29
+ params
30
+ end
31
+
32
+ def spam( user_ip, user_agent, params = {} )
33
+ @service.submit_spam @api_key,
34
+ @home_url,
35
+ user_ip,
36
+ user_agent,
37
+ params
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,87 @@
1
+ require 'net/http'
2
+ require 'cgi'
3
+
4
+ module Akismet
5
+
6
+ AKISMET_VERSION = 1.1
7
+ PORT = 80
8
+ HEADERS = { 'User-Agent' => "Ruby/Akismet #{ VERSION }" }
9
+
10
+ module CommentType
11
+ COMMENT = 'comment'
12
+ TRACKBACK = 'trackback'
13
+ PINGBACK = 'pingback'
14
+ end
15
+
16
+ # Parameters to +comment_check+, +submit_ham+, and +submit_spam+ may
17
+ # include:
18
+ #
19
+ # * content_type
20
+ # * comment_author
21
+ # * comment_author_email
22
+ # * comment_author_url
23
+ # * comment_content
24
+ # * others representing HTTP headers and server environment vars
25
+ #
26
+ # See http://akismet.com/development/api/#comment-check for more info.
27
+ #
28
+ class Service
29
+
30
+ def verify_key( api_key, home_url )
31
+ response = invoke( 'verify-key', 'rest.akismet.com', :blog => home_url,
32
+ :key => api_key )
33
+
34
+ response.is_a?( Net::HTTPOK ) && response.body == 'valid'
35
+ end
36
+
37
+ def comment_check( api_key, home_url, user_ip, user_agent, params = {} )
38
+ response = invoke_comment_method 'comment-check', api_key, home_url,
39
+ user_ip, user_agent, params
40
+
41
+ response.is_a?( Net::HTTPOK ) && response.body == 'true'
42
+ end
43
+
44
+ def submit_ham( api_key, home_url, user_ip, user_agent, params = {} )
45
+ invoke_comment_method 'submit-ham', api_key, home_url, user_ip,
46
+ user_agent, params
47
+
48
+ nil
49
+ end
50
+
51
+ def submit_spam( api_key, home_url, user_ip, user_agent, params = {} )
52
+ invoke_comment_method 'submit-spam', api_key, home_url, user_ip,
53
+ user_agent, params
54
+
55
+ nil
56
+ end
57
+
58
+ private
59
+
60
+ def invoke_comment_method( method_name, api_key, home_url, user_ip, user_agent, params = {} )
61
+ host = "#{ api_key }.rest.akismet.com"
62
+
63
+ params = params.merge :blog => home_url,
64
+ :user_ip => user_ip,
65
+ :user_agent => user_agent
66
+
67
+ invoke method_name, host, params
68
+ end
69
+
70
+ # returns an HTTP response
71
+ def invoke( method_name, host, params )
72
+ path = "/#{ AKISMET_VERSION }/#{ method_name }"
73
+ data = url_encode( params )
74
+
75
+ Net::HTTP.start( host, PORT ) do |http|
76
+ http.post( path, data, HEADERS )
77
+ end
78
+ end
79
+
80
+ def url_encode( hash = {} )
81
+ hash.collect do |k, v|
82
+ "#{ CGI.escape( k.to_s ) }=#{ CGI.escape( v.to_s ) }"
83
+ end.join( "&" )
84
+ end
85
+
86
+ end
87
+ end
@@ -0,0 +1,3 @@
1
+ module Akismet
2
+ VERSION = '0.1.2'
3
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: akismet
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 2
10
+ version: 0.1.2
11
+ platform: ruby
12
+ authors:
13
+ - Jonah Burke
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-01 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: A Ruby client for the Akismet API
23
+ email:
24
+ - jonah@bigthink.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - lib/akismet/client.rb
33
+ - lib/akismet/service.rb
34
+ - lib/akismet/version.rb
35
+ - lib/akismet.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/bigthink/akismet
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ~>
49
+ - !ruby/object:Gem::Version
50
+ hash: 59
51
+ segments:
52
+ - 1
53
+ - 8
54
+ - 6
55
+ version: 1.8.6
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 17
62
+ segments:
63
+ - 1
64
+ - 3
65
+ - 5
66
+ version: 1.3.5
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.7
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: A Ruby client for the Akismet API
74
+ test_files: []
75
+