Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Optional use
2. By default users will get just 1 free click, however by setting `FirstClickFree.free_clicks` in an initializer you can allow n free clicks to content.
3. A count of users' free clicks are available in request.env["first_click_free_count"].

4. throw exception option. `FirstClickFree.raise_exception` in an initializer.

#### Registered Users

If you have registered users that should always be allowed through (they shouldn't be affected by any first click free rules), then you can override the `user_for_first_click_free` method in `ApplicationController`, or any of your controllers using `allow_first_click_free`. This method should return either a falsy value if no-one is signed in, or the current user.
Expand Down
6 changes: 5 additions & 1 deletion lib/first_click_free.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class << self

require 'yaml'

attr_accessor :test_mode, :permitted_paths, :free_clicks
attr_accessor :test_mode, :permitted_paths, :free_clicks, :raise_exception

def root
File.expand_path(File.join(File.dirname(__FILE__), '..'))
Expand All @@ -27,6 +27,10 @@ def free_clicks
@free_clicks || 1 # default is 1 click free
end

def raise_exception?
@raise_exception || false # default is false
end

def test_mode
@test_mode || false
end
Expand Down
11 changes: 10 additions & 1 deletion lib/first_click_free/concerns/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def user_for_first_click_free
nil
end

# allow or deny the request for content access.
def allow_first_click_free?
@first_click_free
end

# Public: Either record a first click free request, or reject
# the request for a subsequent content access.
Expand All @@ -60,6 +64,8 @@ def user_for_first_click_free
# Returns true if the referrer User agent is GoogleBot, or
# if this is the first click recorded for this session.
def record_or_reject_first_click_free!
@first_click_free = true

# Always allow requests from Googlebot
return true if googlebot?

Expand All @@ -86,6 +92,9 @@ def record_or_reject_first_click_free!
end
request.env["first_click_free_count"] = session[:first_click].length
return true
rescue FirstClickFree::Exceptions::SubsequentAccessException => e
@first_click_free = false
raise FirstClickFree::Exceptions::SubsequentAccessException if FirstClickFree.raise_exception?
end

private
Expand All @@ -105,4 +114,4 @@ def checksum(url)
end
end
end
end
end
7 changes: 6 additions & 1 deletion spec/concerns/controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ def checksum(url)
end

context "subsequent visit to different page" do
before { session[:first_click] = [ checksum("http://test.host/another-page") ] }
before do
session[:first_click] = [ checksum("http://test.host/another-page") ]
end

FirstClickFree.raise_exception = true
it { expect { get :index }.to raise_error FirstClickFree::Exceptions::SubsequentAccessException }
end

Expand All @@ -55,6 +58,7 @@ def checksum(url)
FirstClickFree.free_clicks = 3
end

FirstClickFree.raise_exception = true
it { get :index; request.env["first_click_free_count"].should eq 3 }
it { expect { get :index }.not_to raise_error }
end
Expand All @@ -67,6 +71,7 @@ def checksum(url)
FirstClickFree.free_clicks = 3
end

FirstClickFree.raise_exception = true
it { expect { get :index }.to raise_error FirstClickFree::Exceptions::SubsequentAccessException }
end

Expand Down