spamster 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,5 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - jruby
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in spamster.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Brian Alexander
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,75 @@
1
+ # Spamster [![Build Status](https://secure.travis-ci.org/balexand/spamster.png)](http://travis-ci.org/balexand/spamster)
2
+
3
+ Simple spam filtering.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'spamster'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install spamster
20
+
21
+ ## Usage
22
+
23
+ ### Quick start
24
+
25
+ Start by signing up for an API key [here](https://akismet.com/signup/). Then configure Spamster like:
26
+
27
+ ```ruby
28
+ Spamster.blog = "http://yoursite.com/"
29
+ Spamster.key = "your-api-key"
30
+ ```
31
+
32
+ FIXME middleware
33
+ FIXME usage
34
+
35
+ ### Spamster methods
36
+
37
+ #### key_valid?
38
+
39
+ Checks if the key is valid using [verify-key](http://akismet.com/development/api/#verify-key).
40
+
41
+ ```ruby
42
+ Spamster.key_valid?
43
+ ```
44
+
45
+ #### spam?
46
+
47
+ Checks if a comment is spam using [comment-check](http://akismet.com/development/api/#comment-check)
48
+
49
+ ```ruby
50
+ Spamster.spam?(user_ip: "222.222.222.222", user_agent: "Mozilla", comment_author: "viagra-test-123")
51
+ ```
52
+
53
+ ### spam!
54
+
55
+ Reports a false negative using [submit-spam](http://akismet.com/development/api/#submit-spam)
56
+
57
+ ```ruby
58
+ Spamster.spam!(user_ip: "222.222.222.222", user_agent: "Mozilla", comment_author: "viagra-test-123")
59
+ ```
60
+
61
+ ### ham!
62
+
63
+ Reports a false positive using [submit-ham](http://akismet.com/development/api/#submit-ham)
64
+
65
+ ```ruby
66
+ Spamster.ham!(user_ip: "222.222.222.222", user_agent: "Mozilla", comment_author: "viagra-test-123")
67
+ ```
68
+
69
+ ## Contributing
70
+
71
+ 1. Fork it
72
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
73
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
74
+ 4. Push to the branch (`git push origin my-new-feature`)
75
+ 5. Create new Pull Request
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,54 @@
1
+ require 'net/http'
2
+ require 'spamster/version'
3
+
4
+ module Spamster
5
+ class <<self
6
+ attr_accessor :blog, :key
7
+
8
+ # see http://akismet.com/development/api/#verify-key
9
+ def key_valid?
10
+ check_config
11
+ params = {:blog => blog, :key => key}
12
+ response = Net::HTTP.post_form(URI("http://rest.akismet.com/1.1/verify-key"), params)
13
+ response.body == 'valid'
14
+ end
15
+
16
+ # see http://akismet.com/development/api/#comment-check
17
+ def spam?(params)
18
+ submit("comment-check", params) == 'true'
19
+ end
20
+
21
+ # see http://akismet.com/development/api/#submit-spam
22
+ def spam!(params)
23
+ submit("submit-spam", params)
24
+ end
25
+
26
+ # see http://akismet.com/development/api/#submit-ham
27
+ def ham!(params)
28
+ submit("submit-ham", params)
29
+ end
30
+
31
+ private
32
+ def check_config
33
+ raise "'Spamster.blog' must be set" unless blog
34
+ raise "'Spamster.key' must be set" unless key
35
+ end
36
+
37
+ def check_required_params(params)
38
+ # these params are required by spam?, spam!, and ham!
39
+ [:blog, :user_agent, :user_ip].each do |param|
40
+ raise "required param #{param.inspect} is missing" unless params[param]
41
+ end
42
+ end
43
+
44
+ def submit(method, params = {})
45
+ check_config
46
+ params = params.merge(:blog => blog)
47
+ check_required_params(params)
48
+ response = Net::HTTP.post_form(URI("http://#{key}.rest.akismet.com/1.1/#{method}"), params)
49
+ response.body
50
+ end
51
+ end
52
+ end
53
+
54
+ # FIXME User-Agent
@@ -0,0 +1,3 @@
1
+ module Spamster
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/spamster/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Brian Alexander"]
6
+ gem.email = ["balexand@gmail.com"]
7
+ gem.description = %q{Simple spam filtering that takes minutes to set up.}
8
+ gem.summary = %q{Simple spam filtering.}
9
+ gem.homepage = "https://github.com/balexand/spamster"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "spamster"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Spamster::VERSION
17
+
18
+ gem.add_development_dependency "rake"
19
+ gem.add_development_dependency "rspec"
20
+ gem.add_development_dependency "webmock"
21
+
22
+ gem.add_runtime_dependency "jruby-openssl" if RUBY_PLATFORM == "java"
23
+ end
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spamster do
4
+ before(:each) do
5
+ Spamster.blog = "http://example.com/"
6
+ Spamster.key = "123abc"
7
+ end
8
+
9
+ describe "submit" do
10
+ before(:each) do
11
+ stub_request(:post, "http://123abc.rest.akismet.com/1.1/comment-check")
12
+ end
13
+
14
+ it "should raise exception if :blog is not configured" do
15
+ Spamster.blog = nil
16
+ expect do
17
+ Spamster.send(:submit, "comment-check")
18
+ end.to raise_exception{ |e| e.message.should == "'Spamster.blog' must be set" }
19
+ end
20
+
21
+ it "should raise exception if :key is not configured" do
22
+ Spamster.key = nil
23
+ expect do
24
+ Spamster.send(:submit, "comment-check")
25
+ end.to raise_exception{ |e| e.message.should == "'Spamster.key' must be set" }
26
+ end
27
+ end
28
+
29
+ describe "key_valid?" do
30
+ it "should return true with valid response" do
31
+ stub_request(:post, "http://rest.akismet.com/1.1/verify-key").to_return(:body => "valid")
32
+ Spamster.key_valid?.should == true
33
+ assert_requested(:post, "http://rest.akismet.com/1.1/verify-key", :times => 1) do |req|
34
+ CGI.parse(req.body).should == {"blog" => ["http://example.com/"], "key" => ["123abc"]}
35
+ true
36
+ end
37
+ end
38
+
39
+ it "should return false with invalid response" do
40
+ stub_request(:post, "http://rest.akismet.com/1.1/verify-key").to_return(:body => "invalid")
41
+ Spamster.key_valid?.should == false
42
+ assert_requested(:post, "http://rest.akismet.com/1.1/verify-key", :times => 1) do |req|
43
+ CGI.parse(req.body).should == {"blog" => ["http://example.com/"], "key" => ["123abc"]}
44
+ true
45
+ end
46
+ end
47
+ end
48
+
49
+ describe "spam?" do
50
+ it "should return true if spam" do
51
+ stub_request(:post, "http://123abc.rest.akismet.com/1.1/comment-check").to_return(:body => "true")
52
+ Spamster.spam?(:user_ip => "222.222.222.222", :user_agent => "Mozilla", :comment_author => "viagra-test-123").should == true
53
+ assert_requested(:post, "http://123abc.rest.akismet.com/1.1/comment-check", :times => 1) do |req|
54
+ CGI.parse(req.body).should == {"user_ip"=>["222.222.222.222"], "user_agent"=>["Mozilla"], "comment_author"=>["viagra-test-123"], "blog"=>["http://example.com/"]}
55
+ true
56
+ end
57
+ end
58
+
59
+ it "should return false if not spam" do
60
+ stub_request(:post, "http://123abc.rest.akismet.com/1.1/comment-check").to_return(:body => "false")
61
+ Spamster.spam?(:user_ip => "222.222.222.222", :user_agent => "Mozilla").should == false
62
+ assert_requested(:post, "http://123abc.rest.akismet.com/1.1/comment-check", :times => 1) do |req|
63
+ CGI.parse(req.body).should == {"user_ip"=>["222.222.222.222"], "user_agent"=>["Mozilla"], "blog"=>["http://example.com/"]}
64
+ true
65
+ end
66
+ end
67
+
68
+ it "should raise exception if required param missing" do
69
+ expect do
70
+ Spamster.spam?(:user_ip => "222.222.222.222")
71
+ end.to raise_exception{ |e| e.message.should == "required param :user_agent is missing" }
72
+
73
+ expect do
74
+ Spamster.spam?(:user_agent => "Mozilla")
75
+ end.to raise_exception{ |e| e.message.should == "required param :user_ip is missing" }
76
+ end
77
+ end
78
+
79
+ describe "spam!" do
80
+ it "should submit request" do
81
+ stub_request(:post, "http://123abc.rest.akismet.com/1.1/submit-spam").to_return(:body => "Thanks for making the web a better place.")
82
+ Spamster.spam!(:user_ip => "222.222.222.222", :user_agent => "Mozilla", :comment_author => "viagra-test-123")
83
+ assert_requested(:post, "http://123abc.rest.akismet.com/1.1/submit-spam", :times => 1) do |req|
84
+ CGI.parse(req.body).should == {"user_ip"=>["222.222.222.222"], "user_agent"=>["Mozilla"], "comment_author"=>["viagra-test-123"], "blog"=>["http://example.com/"]}
85
+ true
86
+ end
87
+ end
88
+ end
89
+
90
+ describe "ham!" do
91
+ it "should submit request" do
92
+ stub_request(:post, "http://123abc.rest.akismet.com/1.1/submit-ham").to_return(:body => "Thanks for making the web a better place.")
93
+ Spamster.ham!(:user_ip => "222.222.222.222", :user_agent => "Mozilla", :comment_author => "viagra-test-123")
94
+ assert_requested(:post, "http://123abc.rest.akismet.com/1.1/submit-ham", :times => 1) do |req|
95
+ CGI.parse(req.body).should == {"user_ip"=>["222.222.222.222"], "user_agent"=>["Mozilla"], "comment_author"=>["viagra-test-123"], "blog"=>["http://example.com/"]}
96
+ true
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,8 @@
1
+ require 'cgi'
2
+ require 'rspec'
3
+ require 'webmock/rspec'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+
8
+ require 'spamster'
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spamster
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brian Alexander
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: webmock
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Simple spam filtering that takes minutes to set up.
63
+ email:
64
+ - balexand@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .travis.yml
71
+ - Gemfile
72
+ - LICENSE
73
+ - README.md
74
+ - Rakefile
75
+ - lib/spamster.rb
76
+ - lib/spamster/version.rb
77
+ - spamster.gemspec
78
+ - spec/spamster_spec.rb
79
+ - spec/spec_helper.rb
80
+ homepage: https://github.com/balexand/spamster
81
+ licenses: []
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.19
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Simple spam filtering.
104
+ test_files:
105
+ - spec/spamster_spec.rb
106
+ - spec/spec_helper.rb