Skip to content

Commit 1e713fb

Browse files
committed
setting validity period until one day
first click free update rule https://webmasters.googleblog.com/2015/09/first-click-free-update.html
1 parent 2f55c28 commit 1e713fb

File tree

4 files changed

+54
-19
lines changed

4 files changed

+54
-19
lines changed

lib/first_click_free.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module FirstClickFree
44
require 'first_click_free/helpers/path'
55
require 'first_click_free/helpers/referrer'
66
require 'first_click_free/concerns/controller'
7+
require 'first_click_free/sessions/first_click_free_session'
78

89
class << self
910

lib/first_click_free/concerns/controller.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,19 @@ def record_or_reject_first_click_free!
7373
# (new first click free will be set)
7474
reset_first_click_free! if permitted_domain?
7575

76-
if session[:first_click] && session[:first_click].include?(checksum(url_for))
76+
fcf_session = FirstClickFreeSession.new session
77+
if fcf_session.clicks.include?(checksum(url_for))
7778
# already visited, can visit again
78-
elsif session[:first_click] && session[:first_click].length < FirstClickFree.free_clicks
79+
elsif fcf_session.clicks.length < FirstClickFree.free_clicks
7980
# new page but within free click limit
80-
session[:first_click] << checksum(url_for)
81-
elsif session[:first_click] && session[:first_click].length == FirstClickFree.free_clicks
81+
fcf_session << checksum(url_for)
82+
elsif fcf_session.clicks.length == FirstClickFree.free_clicks
8283
raise FirstClickFree::Exceptions::SubsequentAccessException
8384
else
8485
# first click!
85-
session[:first_click] = [ checksum(url_for) ]
86+
fcf_session.clicks = [ checksum(url_for) ]
8687
end
87-
request.env["first_click_free_count"] = session[:first_click].length
88+
request.env["first_click_free_count"] = fcf_session.clicks.length
8889
return true
8990
end
9091

@@ -94,7 +95,7 @@ def record_or_reject_first_click_free!
9495
#
9596
# Returns the value set in the first click session (nil)
9697
def reset_first_click_free!
97-
session.delete(:first_click)
98+
session.delete(:first_click_free)
9899
end
99100

100101
# Private: Create a checksum string.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class FirstClickFreeSession
2+
def initialize session
3+
key = first_click_free_of_session_key
4+
if session["first_click_free"].blank? || session["first_click_free"][key].blank?
5+
session["first_click_free"] = {key => {}}
6+
end
7+
@clicks = session["first_click_free"][key]["first_click"] || []
8+
@session = session
9+
end
10+
11+
def clicks
12+
@clicks
13+
end
14+
15+
def clicks=(click)
16+
add click, -> c { @clicks = c }
17+
end
18+
19+
def <<(click)
20+
add click, -> c { @clicks << c }
21+
end
22+
23+
private
24+
def add click, operation
25+
key = first_click_free_of_session_key
26+
operation.call click
27+
@session["first_click_free"][key] = {"first_click" => @clicks}
28+
end
29+
30+
def first_click_free_of_session_key
31+
Date.today.strftime("%Y%m%d")
32+
end
33+
end

spec/concerns/controller_spec.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,28 @@ def checksum(url)
3030
context "first visit" do
3131
before { get :index, test: true }
3232

33-
it { session[:first_click].should include checksum(current_url) }
33+
it { expect(FirstClickFreeSession.new(session).clicks).to include checksum(current_url) }
3434
it { request.env["first_click_free_count"].should eq 1 }
3535
it { response.should be_success }
3636
end
3737

3838
context "subsequent visit to same page" do
39-
before { session[:first_click] = [ checksum(current_url) ] }
39+
before { FirstClickFreeSession.new(session).clicks = [ checksum(current_url) ] }
4040

4141
it { get :index; request.env["first_click_free_count"].should eq 1 }
4242
it { expect { get :index }.not_to raise_error }
4343
end
4444

4545
context "subsequent visit to different page" do
46-
before { session[:first_click] = [ checksum("http://test.host/another-page") ] }
46+
before { FirstClickFreeSession.new(session).clicks = [ checksum("http://test.host/another-page") ] }
4747

4848
it { expect { get :index }.to raise_error FirstClickFree::Exceptions::SubsequentAccessException }
4949
end
5050

5151
context "subsequent visit to different page with unused multiple clicks" do
5252
before do
53-
session[:first_click] = [ checksum("http://test.host/some-page"),
54-
checksum("http://test.host/some-other-page") ]
53+
FirstClickFreeSession.new(session).clicks = [ checksum("http://test.host/some-page"),
54+
checksum("http://test.host/some-other-page") ]
5555
FirstClickFree.free_clicks = 3
5656
end
5757

@@ -61,9 +61,9 @@ def checksum(url)
6161

6262
context "subsequent visit to different page with multiple clicks used up" do
6363
before do
64-
session[:first_click] = [ checksum("http://test.host/some-page"),
65-
checksum("http://test.host/some-other-page"),
66-
checksum("http://test.host/yet-another-page") ]
64+
FirstClickFreeSession.new(session).clicks = [ checksum("http://test.host/some-page"),
65+
checksum("http://test.host/some-other-page"),
66+
checksum("http://test.host/yet-another-page") ]
6767
FirstClickFree.free_clicks = 3
6868
end
6969

@@ -73,14 +73,14 @@ def checksum(url)
7373
context "googlebot visit" do
7474
before { controller.stub(:googlebot? => true); get :index }
7575

76-
it { session[:first_click].should be_nil }
76+
it { expect(FirstClickFreeSession.new(session).clicks).to be_empty }
7777
it { response.should be_success }
7878
end
7979

8080
context "registered user vist" do
8181
before { controller.stub(:user_for_first_click_free => true); get :index }
8282

83-
it { session[:first_click].should be_nil }
83+
it { expect(FirstClickFreeSession.new(session).clicks).to be_empty }
8484
it { response.should be_success }
8585
end
8686

@@ -102,7 +102,7 @@ def index
102102

103103
before { get :index }
104104

105-
it { session[:first_click].should be_nil }
105+
it { expect(FirstClickFreeSession.new(session).clicks).to be_empty }
106106
it { response.should be_success }
107107
end
108108

@@ -122,7 +122,7 @@ def index
122122

123123
before { get :index }
124124

125-
it { session[:first_click].should be_nil }
125+
it { expect(FirstClickFreeSession.new(session).clicks).to be_empty }
126126
it { response.should be_success }
127127
end
128128
end

0 commit comments

Comments
 (0)