browsing_history 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/browsing_history/storages/redis.rb +9 -6
- data/lib/browsing_history/version.rb +1 -1
- data/spec/browsing_history/browser_spec.rb +20 -15
- data/spec/browsing_history/history_spec.rb +13 -14
- data/spec/browsing_history/storages/redis_spec.rb +54 -29
- data/spec/spec_helper.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 44240900447de64eee1d917a42c10dc18b72fe26
|
4
|
+
data.tar.gz: 3c110cb5e9e00d6e7904bcf8083296297bf0dd2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2bf2adee9b2b6a105b181e70786952ce483696491b654aad786b594dc8cb453d45b2c16e75ac60b8e30fd6458efb227b8a63dc978b8d176527dec7c3d9f68c9a
|
7
|
+
data.tar.gz: 890e06144a100e76889787f04834942fcedcc1f231da633e95121aadd8ed52d052c792e3ece8c4ace9bbc21be266108ce794d951c36b6b6de22e4ab3f839e248
|
data/Gemfile.lock
CHANGED
@@ -59,9 +59,9 @@ class BrowsingHistory::Storages::Redis < BrowsingHistory::Storages::Base
|
|
59
59
|
end
|
60
60
|
|
61
61
|
def fetch_between(opts)
|
62
|
-
|
62
|
+
from, to = parse_between(opts.delete(:between))
|
63
63
|
|
64
|
-
history.revrangebyscore(
|
64
|
+
history.revrangebyscore(to, from, **opts)
|
65
65
|
end
|
66
66
|
|
67
67
|
def clear_expiration(opts)
|
@@ -75,17 +75,20 @@ class BrowsingHistory::Storages::Redis < BrowsingHistory::Storages::Base
|
|
75
75
|
end
|
76
76
|
|
77
77
|
def count_between(opts)
|
78
|
-
|
78
|
+
from, to = parse_between(opts.delete(:between))
|
79
79
|
|
80
|
-
history.range_size(
|
80
|
+
history.range_size(from, to)
|
81
81
|
end
|
82
82
|
|
83
83
|
def score(time = nil)
|
84
|
-
time ? time.to_f : Time.now.to_f
|
84
|
+
time ? time.to_f : Time.zone.now.to_f
|
85
85
|
end
|
86
86
|
|
87
87
|
def parse_between(range)
|
88
|
-
[range.first.to_f, range.last.to_f]
|
88
|
+
from = [range.first.to_f, range.last.to_f].min
|
89
|
+
to = [range.first.to_f, range.last.to_f].max
|
90
|
+
|
91
|
+
[from, to]
|
89
92
|
end
|
90
93
|
|
91
94
|
class << self
|
@@ -3,12 +3,10 @@ require 'fixtures/user'
|
|
3
3
|
require 'fixtures/article'
|
4
4
|
|
5
5
|
describe BrowsingHistory::Browser do
|
6
|
-
articles_size = 50
|
7
|
-
|
8
6
|
let(:user) { User.new }
|
9
|
-
let(:article) { Article.new }
|
7
|
+
let(:article) { Article.new(100) }
|
10
8
|
let(:articles) do
|
11
|
-
|
9
|
+
Array.new(5) { |i| Article.new(i) }
|
12
10
|
end
|
13
11
|
let(:histories) { user.browsing_histories }
|
14
12
|
|
@@ -76,27 +74,34 @@ describe BrowsingHistory::Browser do
|
|
76
74
|
describe '#previous' do
|
77
75
|
context 'default' do
|
78
76
|
it 'should default empty' do
|
79
|
-
expect(histories.previous(article, Time.now..1.day.ago)).to be_empty
|
77
|
+
expect(histories.previous(article, Time.zone.now..1.day.ago)).to be_empty
|
80
78
|
end
|
81
79
|
end
|
82
80
|
|
83
81
|
context 'when histories exist' do
|
84
|
-
before do
|
85
|
-
|
82
|
+
before(:each) do
|
83
|
+
Timecop.travel(1.day.ago) do
|
84
|
+
articles.each { |article| histories << article }
|
85
|
+
end
|
86
|
+
|
87
|
+
histories << article
|
86
88
|
end
|
87
89
|
|
88
|
-
let(:recent_article)
|
90
|
+
let(:recent_article) do
|
91
|
+
histories.previous(article, Time.zone.now..23.hours.ago)
|
92
|
+
end
|
89
93
|
let(:previous_article) do
|
90
|
-
histories.previous(article,
|
94
|
+
histories.previous(article, 23.hours.ago..25.hours.ago)
|
91
95
|
end
|
92
96
|
|
93
|
-
it 'should return
|
94
|
-
|
95
|
-
|
97
|
+
it 'should return recent historizable' do
|
98
|
+
expect(recent_article.size).to eq(1)
|
99
|
+
expect(recent_article.first.id).to eq(article.id)
|
100
|
+
expect(recent_article.first.title).to eq(article.title)
|
101
|
+
end
|
96
102
|
|
97
|
-
|
98
|
-
expect(previous_article.
|
99
|
-
expect(previous_article.first.title).to eq(recent_article.title)
|
103
|
+
it 'should return previous historizables' do
|
104
|
+
expect(previous_article.size).to eq(articles.size)
|
100
105
|
end
|
101
106
|
end
|
102
107
|
end
|
@@ -5,7 +5,7 @@ require 'fixtures/article'
|
|
5
5
|
describe BrowsingHistory::History do
|
6
6
|
let(:user) { User.new }
|
7
7
|
let(:article) { Article.new }
|
8
|
-
let(:articles) { Array.new(
|
8
|
+
let(:articles) { Array.new(5) { |i| Article.new(i) } }
|
9
9
|
let(:history) { described_class.new(browser: user, historizable: article) }
|
10
10
|
let(:invalid_object) { Struct.new(:_).new }
|
11
11
|
|
@@ -208,9 +208,9 @@ describe BrowsingHistory::History do
|
|
208
208
|
end
|
209
209
|
|
210
210
|
context 'with limit option' do
|
211
|
-
before do
|
212
|
-
|
213
|
-
described_class.create(browser: user, historizable:
|
211
|
+
before(:each) do
|
212
|
+
articles.each do |article|
|
213
|
+
described_class.create(browser: user, historizable: article)
|
214
214
|
end
|
215
215
|
end
|
216
216
|
|
@@ -234,24 +234,23 @@ describe BrowsingHistory::History do
|
|
234
234
|
it { expect(historizables.size).to eq(3) }
|
235
235
|
|
236
236
|
it { expect(historizables_excess_limit).to be_a(Array) }
|
237
|
-
it { expect(historizables_excess_limit.size).to eq(
|
237
|
+
it { expect(historizables_excess_limit.size).to eq(articles.size) }
|
238
238
|
end
|
239
239
|
|
240
240
|
context 'with between option' do
|
241
|
-
before do
|
242
|
-
|
243
|
-
|
241
|
+
before(:each) do
|
242
|
+
Timecop.travel(1.day.ago) do
|
243
|
+
articles.each do |article|
|
244
|
+
described_class.create(browser: user, historizable: article)
|
245
|
+
end
|
244
246
|
end
|
245
|
-
|
246
|
-
sleep(6)
|
247
|
-
described_class.create(browser: user, historizable: Article.new(100))
|
248
247
|
end
|
249
248
|
|
250
249
|
let(:historizables) do
|
251
250
|
described_class.where(
|
252
251
|
browser: user,
|
253
252
|
historizable: article,
|
254
|
-
between:
|
253
|
+
between: 23.hours.ago..25.hours.ago
|
255
254
|
)
|
256
255
|
end
|
257
256
|
|
@@ -259,12 +258,12 @@ describe BrowsingHistory::History do
|
|
259
258
|
described_class.where(
|
260
259
|
browser: user,
|
261
260
|
historizable: article,
|
262
|
-
between:
|
261
|
+
between: Time.zone.now..23.hours.ago
|
263
262
|
)
|
264
263
|
end
|
265
264
|
|
266
265
|
it { expect(historizables).to be_a(Array) }
|
267
|
-
it { expect(historizables.size).to eq(
|
266
|
+
it { expect(historizables.size).to eq(articles.size) }
|
268
267
|
|
269
268
|
it { expect(historizables_unhit).to be_a(Array) }
|
270
269
|
it { expect(historizables_unhit.size).to eq(0) }
|
@@ -3,11 +3,9 @@ require 'fixtures/user'
|
|
3
3
|
require 'fixtures/article'
|
4
4
|
|
5
5
|
describe BrowsingHistory::Storages::Redis do
|
6
|
-
|
7
|
-
|
8
|
-
let(:user) { User.new }
|
6
|
+
let(:user) { User.new }
|
9
7
|
let(:articles) do
|
10
|
-
|
8
|
+
Array.new(5) { |i| Article.new(i) }
|
11
9
|
end
|
12
10
|
let(:article) { articles.first }
|
13
11
|
|
@@ -17,6 +15,7 @@ describe BrowsingHistory::Storages::Redis do
|
|
17
15
|
attach_storage :redis
|
18
16
|
end.current_storage
|
19
17
|
end
|
18
|
+
let(:redis) { storage.redis }
|
20
19
|
|
21
20
|
describe 'method_defined?' do
|
22
21
|
describe 'public_instance_methods' do
|
@@ -59,11 +58,11 @@ describe BrowsingHistory::Storages::Redis do
|
|
59
58
|
end
|
60
59
|
end
|
61
60
|
|
62
|
-
context
|
61
|
+
context 'when add articles' do
|
63
62
|
it 'should return articles' do
|
64
63
|
expect do
|
65
64
|
articles.each { |article| storage.add(user, article) }
|
66
|
-
end.to change { storage.fetch(user, article).size }.by(
|
65
|
+
end.to change { storage.fetch(user, article).size }.by(articles.size)
|
67
66
|
end
|
68
67
|
end
|
69
68
|
end
|
@@ -87,47 +86,73 @@ describe BrowsingHistory::Storages::Redis do
|
|
87
86
|
end
|
88
87
|
|
89
88
|
describe '#clear' do
|
90
|
-
|
89
|
+
context 'default' do
|
90
|
+
before(:each) { articles.each { |article| storage.add(user, article) } }
|
91
91
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
92
|
+
it 'should clear 1 historizable' do
|
93
|
+
expect { storage.clear(user, article, article) }
|
94
|
+
.to change { storage.fetch(user, article).size }.by(-1)
|
95
|
+
end
|
96
96
|
|
97
|
-
|
98
|
-
|
99
|
-
|
97
|
+
it 'should celar all historizables' do
|
98
|
+
expect { storage.clear(user, article, all: true) }
|
99
|
+
.to change { storage.fetch(user, article).size }.to(0)
|
100
|
+
end
|
100
101
|
end
|
101
102
|
|
102
|
-
context '
|
103
|
-
before do
|
104
|
-
|
105
|
-
|
103
|
+
context 'added some histoirizable 1 day ago and add one now' do
|
104
|
+
before(:each) do
|
105
|
+
Timecop.travel(1.day.ago) do
|
106
|
+
articles.each { |article| storage.add(user, article) }
|
107
|
+
end
|
108
|
+
|
109
|
+
storage.add(user, Article.new(101))
|
106
110
|
end
|
107
111
|
|
108
|
-
it 'should
|
109
|
-
expect { storage.clear(user, article, expiration:
|
110
|
-
.to change { storage.
|
112
|
+
it 'should clear 1 historizable from now to 23 hours ago' do
|
113
|
+
expect { storage.clear(user, article, expiration: 23.hours.ago) }
|
114
|
+
.to change { storage.count(user, article) }
|
115
|
+
.from(articles.size + 1).to(1)
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'should clear all historizables from now' do
|
119
|
+
expect { storage.clear(user, article, expiration: Time.zone.now) }
|
120
|
+
.to change { storage.count(user, article) }
|
121
|
+
.from(articles.size + 1).to(0)
|
111
122
|
end
|
112
123
|
end
|
113
124
|
end
|
114
125
|
|
115
126
|
describe '#count' do
|
116
|
-
|
127
|
+
context 'default' do
|
128
|
+
before { articles.each { |article| storage.add(user, article) } }
|
117
129
|
|
118
|
-
|
119
|
-
|
120
|
-
|
130
|
+
context 'when exist historizables' do
|
131
|
+
subject { storage.count(user, article) }
|
132
|
+
it { is_expected.to be(articles.size) }
|
133
|
+
end
|
121
134
|
end
|
122
135
|
|
123
|
-
context 'and add 1
|
136
|
+
context 'and add some historizables 1 day ago and add one now' do
|
124
137
|
before do
|
125
|
-
|
138
|
+
Timecop.travel(1.day.ago) do
|
139
|
+
articles.each { |article| storage.add(user, article) }
|
140
|
+
end
|
141
|
+
|
126
142
|
storage.add(user, Article.new(100))
|
127
143
|
end
|
128
144
|
|
129
|
-
|
130
|
-
|
145
|
+
it do
|
146
|
+
expect(
|
147
|
+
storage.count(user, article, between: Time.zone.now..23.hours.ago)
|
148
|
+
).to be(1)
|
149
|
+
end
|
150
|
+
|
151
|
+
it do
|
152
|
+
expect(
|
153
|
+
storage.count(user, article, between: Time.zone.now..25.hours.ago)
|
154
|
+
).to be(articles.size + 1)
|
155
|
+
end
|
131
156
|
end
|
132
157
|
end
|
133
158
|
|
data/spec/spec_helper.rb
CHANGED
@@ -5,6 +5,7 @@ require 'pry'
|
|
5
5
|
require 'rails/all'
|
6
6
|
require 'rspec/rails'
|
7
7
|
require 'database_cleaner'
|
8
|
+
require 'timecop'
|
8
9
|
|
9
10
|
require 'browsing_history'
|
10
11
|
|
@@ -30,7 +31,6 @@ RSpec.configure do |config|
|
|
30
31
|
end
|
31
32
|
|
32
33
|
config.after(:each) do
|
33
|
-
DatabaseCleaner.clean
|
34
34
|
redis.redis.quit
|
35
35
|
end
|
36
36
|
end
|