counter 0.0.5 → 0.5.1

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.5.1
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{counter}
8
- s.version = "0.0.5"
8
+ s.version = "0.5.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ben Koski"]
@@ -67,17 +67,21 @@ class MovingCount < ActiveRecord::Base
67
67
  # Yields a Counter instance, all standard methods apply.
68
68
  # Raises an exception if the timestamp would fall too soon under the <tt>sample_interval</tt>.
69
69
  def self.record_counts timestamp=Time.now, &block
70
+ timestamp = Time.at(timestamp) if timestamp.is_a?(Integer)
70
71
  c = Counter.new
71
72
  yield(c)
72
73
 
73
74
  self.transaction do
74
75
  check_sample_valid(timestamp)
76
+
77
+ unless c.counts.empty?
78
+ q = "INSERT INTO #{self.table_name} (sample_time, category, count) VALUES "
79
+ c.counts.each { |key, count| q += self.sanitize_sql(['(?, ?, ?),', timestamp, key, count]) }
80
+ q.chop!
75
81
 
76
- q = "INSERT INTO #{self.table_name} (sample_time, category, count) VALUES "
77
- c.counts.each { |key, count| q += self.sanitize_sql(['(?, ?, ?),', timestamp, key, count]) }
78
- q.chop!
79
-
80
- self.connection.execute(q)
82
+ self.connection.execute(q)
83
+ end
84
+
81
85
  self.delete_all(['sample_time < ?', (timestamp - history_to_keep)])
82
86
  end
83
87
 
@@ -20,6 +20,11 @@ class MovingCountTest < Test::Unit::TestCase
20
20
  assert_equal 1, PageView.find_by_category('http://www.nytimes.com/article.html').count
21
21
  end
22
22
 
23
+ should "not save if no data was recorded" do
24
+ assert_nothing_raised { PageView.record_counts { |c| } }
25
+ assert_equal 0, PageView.count
26
+ end
27
+
23
28
  context "sample interval" do
24
29
  should "raise a RuntimeError to enforce sample interval" do
25
30
  setup_existing_counts(59)
@@ -63,6 +68,17 @@ class MovingCountTest < Test::Unit::TestCase
63
68
 
64
69
  assert_equal 2, PageView.count(:conditions => {:sample_time => Time.now})
65
70
  end
71
+
72
+ should "use allow timestamp to be specified as a UNIX stamp" do
73
+ Timecop.freeze
74
+
75
+ PageView.record_counts(Time.now.to_i) do |c|
76
+ c.saw('http://www.nytimes.com')
77
+ c.saw('http://www.nytimes.com/article.html')
78
+ end
79
+
80
+ assert_equal 2, PageView.count(:conditions => {:sample_time => Time.now})
81
+ end
66
82
  end
67
83
 
68
84
  context "purge" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: counter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Koski