browsing_history 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5579aa68af285099783e050c1f060c193d4e5761
4
- data.tar.gz: 55d5a56f2ddcc0e2294febb4aa2ff1175ca85d42
3
+ metadata.gz: 44240900447de64eee1d917a42c10dc18b72fe26
4
+ data.tar.gz: 3c110cb5e9e00d6e7904bcf8083296297bf0dd2a
5
5
  SHA512:
6
- metadata.gz: 665329c030a0ec2e17f6120516c315d864a5540169f720e0f50b75ad15f30e541f7cbeff4bbd237b03dddb9d2ef78a5015c460de51036841983965e4935c54fb
7
- data.tar.gz: 1d56e955901f795dd373eedde90853c2aa0234bcde80f70a593685338f09a9a5ebc7dc926421c7c27543a15c73394fafaa8a139401ccb789c1c472f3b5ee60e2
6
+ metadata.gz: 2bf2adee9b2b6a105b181e70786952ce483696491b654aad786b594dc8cb453d45b2c16e75ac60b8e30fd6458efb227b8a63dc978b8d176527dec7c3d9f68c9a
7
+ data.tar.gz: 890e06144a100e76889787f04834942fcedcc1f231da633e95121aadd8ed52d052c792e3ece8c4ace9bbc21be266108ce794d951c36b6b6de22e4ab3f839e248
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- browsing_history (0.0.2)
4
+ browsing_history (0.0.3)
5
5
  redis (>= 3.0.2)
6
6
  redis-namespace
7
7
  redis-objects
@@ -59,9 +59,9 @@ class BrowsingHistory::Storages::Redis < BrowsingHistory::Storages::Base
59
59
  end
60
60
 
61
61
  def fetch_between(opts)
62
- start_date, end_date = parse_between(opts.delete(:between))
62
+ from, to = parse_between(opts.delete(:between))
63
63
 
64
- history.revrangebyscore(start_date, end_date, **opts)
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
- end_date, start_date = parse_between(opts.delete(:between))
78
+ from, to = parse_between(opts.delete(:between))
79
79
 
80
- history.range_size(start_date, end_date)
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
@@ -1,3 +1,3 @@
1
1
  module BrowsingHistory
2
- VERSION = '0.0.2'.freeze
2
+ VERSION = '0.0.3'.freeze
3
3
  end
@@ -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
- articles_size.times.map.with_index(1) { |i| Article.new(i) }
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
- articles.each { |article| histories << article }
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) { Article.new(100) }
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, Time.now..5.seconds.ago)
94
+ histories.previous(article, 23.hours.ago..25.hours.ago)
91
95
  end
92
96
 
93
- it 'should return 1 historizable' do
94
- sleep(6)
95
- histories << recent_article
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
- expect(previous_article.size).to eq(1)
98
- expect(previous_article.first.id).to eq(recent_article.id)
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(50) { |i| Article.new(i) } }
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
- 5.times do |i|
213
- described_class.create(browser: user, historizable: Article.new(i))
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(5) }
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
- 5.times.map.with_index(1) do |i|
243
- described_class.create(browser: user, historizable: Article.new(i))
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: Time.now..5.seconds.ago
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: 1.day.ago..2.days.ago
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(1) }
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
- articles_size = 5
7
-
8
- let(:user) { User.new }
6
+ let(:user) { User.new }
9
7
  let(:articles) do
10
- articles_size.times.map.with_index(1) { |i| Article.new(i) }
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 "when add #{articles_size} object" do
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(articles_size)
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
- before(:each) { articles.each { |article| storage.add(user, article) } }
89
+ context 'default' do
90
+ before(:each) { articles.each { |article| storage.add(user, article) } }
91
91
 
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
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
- it 'should celar all historizables' do
98
- expect { storage.clear(user, article, all: true) }
99
- .to change { storage.fetch(user, article).size }.to(0)
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 'and add 1 historizable after 6 seconds' do
103
- before do
104
- sleep(6)
105
- storage.add(user, Article.new(100))
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 celar 5 historizables' do
109
- expect { storage.clear(user, article, expiration: 5.seconds.ago) }
110
- .to change { storage.fetch(user, article).size }.to(1)
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
- before(:each) { articles.each { |article| storage.add(user, article) } }
127
+ context 'default' do
128
+ before { articles.each { |article| storage.add(user, article) } }
117
129
 
118
- context "when exist #{articles_size} historizable" do
119
- subject { storage.count(user, article) }
120
- it { is_expected.to be(articles_size) }
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 historizable after 6 seconds' do
136
+ context 'and add some historizables 1 day ago and add one now' do
124
137
  before do
125
- sleep(6)
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
- subject { storage.count(user, article, between: Time.now..5.seconds.ago) }
130
- it { is_expected.to be(1) }
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: browsing_history
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - tkclimb