Skip to content

Commit

Permalink
Adding an annoyance gem so the server can fight back against spammers!
Browse files Browse the repository at this point in the history
Includes:
* introduced new gem with new build
* split up tests for teaser engine in requests and non-requests to increase stability
  • Loading branch information
shageman committed Sep 17, 2012
1 parent adf08ed commit 7c18f65
Show file tree
Hide file tree
Showing 29 changed files with 380 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source "https://rubygems.org"
gem "rails"

gem "teaser", path: "engines/teaser"

gem "annoyance", path: "gems/annoyance"

group :test, :development do
gem "rspec-rails"
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ PATH
remote: engines/teaser
specs:
teaser (0.0.1)
annoyance
haml-rails
jquery-rails
rails (~> 3.2.8)
sass-rails

PATH
remote: gems/annoyance
specs:
annoyance (0.0.1)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -160,6 +166,7 @@ PLATFORMS
ruby

DEPENDENCIES
annoyance!
capybara
jasmine
poltergeist
Expand Down
21 changes: 18 additions & 3 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,29 @@ bundle install > /dev/null
bundle exec rake db:migrate db:test:prepare rspec
result=$?

echo "*** Running teaser engine specs"

cd engines/teaser
echo "*** Running teaser engine non-request specs"
bundle install > /dev/null
bundle exec rake db:migrate app:db:test:prepare rspec
bundle exec rake db:migrate app:db:test:prepare
rspec spec/models spec/controllers
result+=$?

echo "*** Running teaser engine request specs"
rspec spec/reqests
result+=$?

echo "*** Running teaser engine javascript specs"
bundle exec rake app:jasmine:ci
result+=$?

exit $result

cd ../..
cd gems/annoyance
echo "*** Running annoyance gem specs"
bundle install > /dev/null
bundle exec rspec spec
result+=$?


exit $result
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120915205848) do
ActiveRecord::Schema.define(:version => 20120917081547) do

create_table "teaser_entries", :force => true do |t|
t.string "email"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "tries"
end

end
2 changes: 2 additions & 0 deletions engines/teaser/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ gem "shoulda-matchers"
gem "sqlite3"
gem "poltergeist"
gem "jasmine"

gem "annoyance", path: "../../gems/annoyance"
7 changes: 7 additions & 0 deletions engines/teaser/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ PATH
remote: .
specs:
teaser (0.0.1)
annoyance
haml-rails
jquery-rails
rails (~> 3.2.8)
sass-rails

PATH
remote: ../../gems/annoyance
specs:
annoyance (0.0.1)

GEM
remote: http://rubygems.org/
specs:
Expand Down Expand Up @@ -160,6 +166,7 @@ PLATFORMS
ruby

DEPENDENCIES
annoyance!
capybara
jasmine
poltergeist
Expand Down
12 changes: 8 additions & 4 deletions engines/teaser/app/controllers/teaser/tease_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ def new
end

def create
@annoyance_meter = Annoyance::Meter.new(20)

email = params[:new_sign_up_entry].presence
render text: "Hey! Please call this right... I need a new signUp entry!", status: 400 and return unless email

similar_exisiting_entry = Teaser::Entry.find_by_email(email)
render text: "Hm... something went wrong. Did you already sign up?", status: 400 and return if similar_exisiting_entry

if Teaser::Entry.create(email: email)
if similar_exisiting_entry = Teaser::Entry.find_by_email(email)
similar_exisiting_entry.update_attribute(:tries, similar_exisiting_entry.tries + 1)
render(
text: @annoyance_meter.annoyance_adjusted("Hm... Did you already sign up?", similar_exisiting_entry.tries),
status: 400) and return
elsif Teaser::Entry.create(email: email, tries: 0)
render text: "Thanks for signing up!", status: 200
else
render text: "Hm... something went seriously wrong.", status: 500
Expand Down
2 changes: 1 addition & 1 deletion engines/teaser/app/models/teaser/entry.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Teaser
class Entry < ActiveRecord::Base
attr_accessible :email
attr_accessible :email, :tries
validates :email, presence: true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddTriesToTeaserEntries < ActiveRecord::Migration
def change
add_column :teaser_entries, :tries, :integer, default: 0
end
end
1 change: 1 addition & 0 deletions engines/teaser/lib/teaser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "rails/all"
require "haml"
require "jquery-rails"
require "annoyance"

require File.expand_path("../monkey_patches/engine.rb", __FILE__)

Expand Down
5 changes: 3 additions & 2 deletions engines/teaser/spec/controllers/tease_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ module Teaser
end

it "should fail if the value given in the new_sign_up_entry parameter already exists in the database" do
Teaser::Entry.create!(email: "adam")
entry = Teaser::Entry.create!(email: "adam", tries: 0)

xhr :post, :create, new_sign_up_entry: "adam", use_route: "teaser"
response.status.should == 400
response.body.should == "Hm... something went wrong. Did you already sign up?"
response.body.should == "Hm... Did you already sign up?Huh?"
entry.reload.tries.should == 1
end

it "should fail if the new entry cannot be saved" do
Expand Down
3 changes: 2 additions & 1 deletion engines/teaser/spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120915205848) do
ActiveRecord::Schema.define(:version => 20120917081547) do

create_table "teaser_entries", :force => true do |t|
t.string "email"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "tries"
end

end
18 changes: 18 additions & 0 deletions engines/teaser/spec/request_spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../dummy/config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'

require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist

Dir[Teaser::Engine.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
config.use_transactional_fixtures = false
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.include RequestSpecHelpers
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "spec_helper"
require "request_spec_helper"

feature "Signing up for tnbt updates", %q{
In order to stay informed about the next big thing
Expand Down
6 changes: 1 addition & 5 deletions engines/teaser/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
require 'rspec/autorun'
require 'capybara/rspec'

require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist

Dir[Teaser::Engine.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
#config.fixture_path = "#{::Rails.root}/spec/fixtures"
#config.use_transactional_fixtures = true
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.include RequestSpecHelpers
Expand Down
2 changes: 2 additions & 0 deletions engines/teaser/teaser.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ Gem::Specification.new do |s|
s.add_dependency "sass-rails"

s.add_dependency "jquery-rails"

s.add_dependency "annoyance"
end
7 changes: 7 additions & 0 deletions gems/annoyance/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.bundle/
log/*.log
pkg/
test/dummy/db/*.sqlite3
test/dummy/log/*.log
test/dummy/tmp/
test/dummy/.sass-cache
5 changes: 5 additions & 0 deletions gems/annoyance/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source "http://rubygems.org"

gemspec

gem "rspec"
24 changes: 24 additions & 0 deletions gems/annoyance/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
PATH
remote: .
specs:
annoyance (0.0.1)

GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.3)
rspec (2.11.0)
rspec-core (~> 2.11.0)
rspec-expectations (~> 2.11.0)
rspec-mocks (~> 2.11.0)
rspec-core (2.11.1)
rspec-expectations (2.11.3)
diff-lcs (~> 1.1.3)
rspec-mocks (2.11.2)

PLATFORMS
ruby

DEPENDENCIES
annoyance!
rspec
20 changes: 20 additions & 0 deletions gems/annoyance/MIT-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright 2012 YOURNAME

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3 changes: 3 additions & 0 deletions gems/annoyance/README.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
= Annoyance

This project rocks and uses MIT-LICENSE.
38 changes: 38 additions & 0 deletions gems/annoyance/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env rake
begin
require 'bundler/setup'
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end
begin
require 'rdoc/task'
rescue LoadError
require 'rdoc/rdoc'
require 'rake/rdoctask'
RDoc::Task = Rake::RDocTask
end

RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'AnnoyanceMeter'
rdoc.options << '--line-numbers'
rdoc.rdoc_files.include('README.rdoc')
rdoc.rdoc_files.include('lib/**/*.rb')
end




Bundler::GemHelper.install_tasks

require 'rake/testtask'

Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = false
end


task :default => :test
18 changes: 18 additions & 0 deletions gems/annoyance/annoyance.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
$:.push File.expand_path("../lib", __FILE__)

# Maintain your gem's version:
require "annoyance/version"

# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
s.name = "annoyance"
s.version = Annoyance::VERSION
s.authors = ["TODO: Your name"]
s.email = ["TODO: Your email"]
s.homepage = "TODO"
s.summary = "TODO: Summary of Annoyance."
s.description = "TODO: Description of Annoyance."

s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.rdoc"]
s.test_files = Dir["spec/**/*"]
end
5 changes: 5 additions & 0 deletions gems/annoyance/lib/annoyance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "annoyance/meter"
require "annoyance/levels"

module Annoyance
end
16 changes: 16 additions & 0 deletions gems/annoyance/lib/annoyance/levels.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Annoyance
def self.levels
[
"Huh?",
"What?",
"Dude!",
"Hello!?",
"Somebody home?",
"Hey, careful with you're typing!",
"Come on, this is nonsense!",
"I have almost had enough!",
"Really?",
%q{<img src="http://gifs.gifbin.com/3sw8sw94405.gif" alt="Hairdo" title="Hairdo" class="gif" style="margin: 0 10px;">}
]
end
end
Loading

0 comments on commit 7c18f65

Please sign in to comment.