diff --git a/README.md b/README.md index 0498068..a85386d 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/first_click_free.rb b/lib/first_click_free.rb index 378b88f..292e3a5 100644 --- a/lib/first_click_free.rb +++ b/lib/first_click_free.rb @@ -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__), '..')) @@ -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 diff --git a/lib/first_click_free/concerns/controller.rb b/lib/first_click_free/concerns/controller.rb index b297d8f..1e2943f 100644 --- a/lib/first_click_free/concerns/controller.rb +++ b/lib/first_click_free/concerns/controller.rb @@ -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. @@ -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? @@ -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 @@ -105,4 +114,4 @@ def checksum(url) end end end -end \ No newline at end of file +end diff --git a/spec/concerns/controller_spec.rb b/spec/concerns/controller_spec.rb index d21bc76..8aa9cc8 100644 --- a/spec/concerns/controller_spec.rb +++ b/spec/concerns/controller_spec.rb @@ -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 @@ -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 @@ -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