monocle 0.2.1 → 0.2.2
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.
- data/lib/monocle.rb +2 -0
- data/lib/monocle/server.rb +10 -2
- data/lib/monocle/version.rb +1 -1
- data/spec/monocle_spec.rb +31 -12
- metadata +1 -1
data/lib/monocle.rb
CHANGED
@@ -85,6 +85,8 @@ module Monocle
|
|
85
85
|
self._monocle_view_types.keys.each_with_index do |view_type, i|
|
86
86
|
cache_view_count(view_type, results[i])
|
87
87
|
end
|
88
|
+
self.update_column(self._monocle_options[:cache_threshold_check_field].to_sym, Time.now) if respond_to?(:update_column)
|
89
|
+
self.set(self._monocle_options[:cache_threshold_check_field].to_sym, Time.now) if respond_to?(:set)
|
88
90
|
end
|
89
91
|
end
|
90
92
|
|
data/lib/monocle/server.rb
CHANGED
@@ -2,8 +2,16 @@ require 'sinatra/base'
|
|
2
2
|
|
3
3
|
module Monocle
|
4
4
|
class Server < Sinatra::Base
|
5
|
-
post '/:type/:id
|
6
|
-
|
5
|
+
post '/:type/:id.:format' do
|
6
|
+
view_object(params[:type], params[:id])
|
7
|
+
end
|
8
|
+
|
9
|
+
get '/:type/:id.:format' do
|
10
|
+
view_object(params[:type], params[:id])
|
11
|
+
end
|
12
|
+
|
13
|
+
def view_object(type, id)
|
14
|
+
if object = type.classify.constantize.find(id)
|
7
15
|
object.view!
|
8
16
|
'o_0 +1'
|
9
17
|
else
|
data/lib/monocle/version.rb
CHANGED
data/spec/monocle_spec.rb
CHANGED
@@ -16,6 +16,7 @@ class TestObject
|
|
16
16
|
hourly: -> { Time.now.beginning_of_hour }
|
17
17
|
|
18
18
|
attr_accessor :id
|
19
|
+
attr_accessor :updated_at
|
19
20
|
attr_accessor :overall_views, :yearly_views, :monthly_views
|
20
21
|
attr_accessor :weekly_views, :daily_views, :hourly_views
|
21
22
|
attr_accessor :quarterly_views
|
@@ -29,15 +30,12 @@ class TestObject
|
|
29
30
|
def initialize
|
30
31
|
@id = '12345'
|
31
32
|
@overall_views = 0
|
33
|
+
@updated_at = Time.now - 1.hour
|
32
34
|
end
|
33
35
|
|
34
36
|
def update_column(field, count)
|
35
37
|
self.send("#{field}=", count)
|
36
38
|
end
|
37
|
-
|
38
|
-
def updated_at
|
39
|
-
Time.now - 1.hour
|
40
|
-
end
|
41
39
|
end
|
42
40
|
|
43
41
|
def make_viewed_objects(number_of_objects_to_make)
|
@@ -116,17 +114,38 @@ describe Monocle do
|
|
116
114
|
end
|
117
115
|
end
|
118
116
|
|
119
|
-
|
120
|
-
|
121
|
-
|
117
|
+
context 'when cache time is over threshold' do
|
118
|
+
describe '#view!' do
|
119
|
+
before { object.stub(:updated_at).and_return(Time.now - 1.hour) }
|
120
|
+
before { 50.times { object.view! }}
|
121
|
+
after { object.destroy_views }
|
122
|
+
|
123
|
+
%w(overall yearly monthly weekly daily hourly quarterly).each do |view_type|
|
124
|
+
it "sets #{view_type} views count" do
|
125
|
+
object.send("#{view_type}_views_count").should == 50
|
126
|
+
end
|
122
127
|
|
123
|
-
|
124
|
-
|
125
|
-
|
128
|
+
it "updates cached #{view_type} views count" do
|
129
|
+
object.send("#{view_type}_views").should == 50
|
130
|
+
end
|
126
131
|
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
context 'when cache time is under threshold' do
|
136
|
+
describe '#view!' do
|
137
|
+
before { object.stub(:updated_at).and_return(Time.now) }
|
138
|
+
before { 50.times { object.view! }}
|
139
|
+
after { object.destroy_views }
|
127
140
|
|
128
|
-
|
129
|
-
|
141
|
+
%w(overall yearly monthly weekly daily hourly quarterly).each do |view_type|
|
142
|
+
it "sets #{view_type} views count" do
|
143
|
+
object.send("#{view_type}_views_count").should == 50
|
144
|
+
end
|
145
|
+
|
146
|
+
it "updates cached #{view_type} views count" do
|
147
|
+
object.send("#{view_type}_views").to_i.should == 0
|
148
|
+
end
|
130
149
|
end
|
131
150
|
end
|
132
151
|
end
|