diff --git a/README.md b/README.md index 92c2814..7567f24 100644 --- a/README.md +++ b/README.md @@ -27,10 +27,10 @@ By default Von will only bump a "total" counter for the given key. This is great ```ruby Von.configure do |config| # Keep daily stats for 30 days - config.counter 'downloads', :daily => 30 + config.counter 'downloads', daily: 30 # Keep monthly stats for 3 months and yearly stats for 2 years - config.counter 'uploads', :monthly => 3, :yearly => 2 + config.counter 'uploads', monthly: 3, yearly: 2 end ``` @@ -41,10 +41,10 @@ If just wanna track stats on the current minute/hour/day/week/etc, you can set t ```ruby Von.configure do |config| # Track downloads stats for the current hour - config.counter 'downloads', :current => :hour + config.counter 'downloads', current: :hour # Track page views for the current day and week - config.counter 'page-views', :current => [ :day, :week ] + config.counter 'page-views', current: [ :day, :week ] end ``` @@ -55,10 +55,10 @@ Time periods are pretty cool, but sometimes you wanna know when you did your bes ```ruby Von.configure do |config| # Track the best day for downloads - config.counter 'downloads', :best => :day + config.counter 'downloads', best: :day # Track the best hour and week for page views - config.counter 'page-views', :best => [ :hour, :week ] + config.counter 'page-views', best: [ :hour, :week ] end ``` @@ -79,7 +79,7 @@ Von.increment('page-views') Von.count('downloads').total #=> 4 # get the monthly counts (returns an Array of Hashes) -Von.count('uploads').per(:month) #=> [ { :timestamp => '2012-03', :count => 3 }, { :timestamp => '2013-04', :count => 1 }, { :timestamp => '2013-05', :count => 0 }] +Von.count('uploads').per(:month) #=> [ { timestamp: '2012-03', count: 3 }, { timestamp: '2013-04', count: 1 }, { timestamp: '2013-05', count: 0 }] # get the download stats for the hour Von.count('downloads').this(:hour) #=> 10 @@ -91,7 +91,7 @@ Von.count('page-views').today #=> 78 Von.count('page-views').current(:day) #=> 78 # get the best day for downloads (returns a Hash) -Von.count('downloads').best(:day) #=> { :timestamp => '2012-03-01', :count => 10 } +Von.count('downloads').best(:day) #=> { timestamp: '2012-03-01', count: 10 } ``` One nice thing to note, if you're counting a time period and there wasn't a value stored for the particular hour/day/week/etc, it'll be populated with a zero, this ensures that if you want 30 days of stats, you get 30 days of stats. @@ -105,7 +105,7 @@ Von.configure do |config| # set the Redis connection to an already existing connection config.redis = Redis.current # Initialize a new Redis connection given options - config.redis = { :host => 'localhost', :port => 6379 } + config.redis = { host: 'localhost', port: 6379 } # rescue Redis connection errors # if the connection fails, no errors are raised by default diff --git a/Rakefile b/Rakefile index 6c6d0a1..104619d 100755 --- a/Rakefile +++ b/Rakefile @@ -7,4 +7,4 @@ Rake::TestTask.new do |t| t.test_files = FileList['test/**/*_test.rb'] end -task :default => :test \ No newline at end of file +task default: :test \ No newline at end of file diff --git a/lib/von/counter.rb b/lib/von/counter.rb index 17f0a10..ac4f36d 100644 --- a/lib/von/counter.rb +++ b/lib/von/counter.rb @@ -26,10 +26,10 @@ def total def per(unit) periods = Von.config.periods[@field] - if !Period.time_unit_exists?(unit) - raise ArgumentError, "`#{unit}' is an unknown time unit" - else + if Period.time_unit_exists?(unit) Counters::Period.new(@field, periods).count(unit) + else + raise ArgumentError, "`#{unit}' is an unknown time unit" end rescue Redis::BaseError => e raise e if Von.config.raise_connection_errors @@ -38,10 +38,10 @@ def per(unit) def best(unit) periods = Von.config.bests[@field] - if !Period.time_unit_exists?(unit) - raise ArgumentError, "`#{unit}' is an unknown time unit" - else + if Period.time_unit_exists?(unit) Counters::Best.new(@field, periods).count(unit) + else + raise ArgumentError, "`#{unit}' is an unknown time unit" end rescue Redis::BaseError => e raise e if Von.config.raise_connection_errors @@ -50,10 +50,10 @@ def best(unit) def this(unit) periods = Von.config.currents[@field] - if !Period.time_unit_exists?(unit) - raise ArgumentError, "`#{unit}' is an unknown time unit" - else + if Period.time_unit_exists?(unit) Counters::Current.new(@field, periods).count(unit) + else + raise ArgumentError, "`#{unit}' is an unknown time unit" end rescue Redis::BaseError => e raise e if Von.config.raise_connection_errors diff --git a/lib/von/counters/best.rb b/lib/von/counters/best.rb index e319cc7..99a52d5 100644 --- a/lib/von/counters/best.rb +++ b/lib/von/counters/best.rb @@ -1,7 +1,7 @@ module Von module Counters class Best - include Von::Counters::Commands + include Commands def initialize(field, periods = nil) @field = field.to_sym @@ -57,9 +57,9 @@ def count(time_unit) _best_total = best_total(time_unit) if _current_total > _best_total - { :timestamp => _current_timestamp, :count => _current_total } + { timestamp: _current_timestamp, count: _current_total } else - { :timestamp => _best_timestamp, :count => _best_total } + { timestamp: _best_timestamp, count: _best_total } end end diff --git a/lib/von/counters/current.rb b/lib/von/counters/current.rb index 53c8f9e..b1ea8fb 100644 --- a/lib/von/counters/current.rb +++ b/lib/von/counters/current.rb @@ -1,7 +1,7 @@ module Von module Counters class Current - include Von::Counters::Commands + include Commands # Initialize a new Counter # diff --git a/lib/von/counters/period.rb b/lib/von/counters/period.rb index c5ab4ad..1aa222f 100644 --- a/lib/von/counters/period.rb +++ b/lib/von/counters/period.rb @@ -1,7 +1,7 @@ module Von module Counters class Period - include Von::Counters::Commands + include Commands def initialize(field, periods = nil) @field = field.to_sym @@ -44,9 +44,8 @@ def increment def count(time_unit) return if @periods.empty? - counts = [] - this_period = nil - _period = @periods.select { |p| p.time_unit == time_unit }.first + counts = [] + _period = @periods.select { |p| p.time_unit == time_unit }.first _period.length.times do |i| this_period = _period.prev(i) @@ -54,7 +53,7 @@ def count(time_unit) end keys = hgetall(hash_key(time_unit)) - counts.map { |date| { :timestamp => date, :count => keys.fetch(date, 0).to_i }} + counts.map { |date| { timestamp: date, count: keys.fetch(date, 0).to_i }} end end diff --git a/lib/von/counters/total.rb b/lib/von/counters/total.rb index 2606e8d..9d250ca 100644 --- a/lib/von/counters/total.rb +++ b/lib/von/counters/total.rb @@ -1,7 +1,8 @@ module Von module Counters class Total - include Von::Counters::Commands + include Commands + attr_reader :field # Initialize a new Counter diff --git a/lib/von/period.rb b/lib/von/period.rb index 9d3518b..a6fecbf 100644 --- a/lib/von/period.rb +++ b/lib/von/period.rb @@ -1,13 +1,14 @@ module Von class Period PERIOD_MAPPING = { - :minutely => :minute, - :hourly => :hour, - :daily => :day, - :weekly => :week, - :monthly => :month, - :yearly => :year + minutely: :minute, + hourly: :hour, + daily: :day, + weekly: :week, + monthly: :month, + yearly: :year } + AVAILABLE_PERIODS = PERIOD_MAPPING.keys AVAILABLE_TIME_UNITS = PERIOD_MAPPING.values @@ -50,7 +51,7 @@ def minutes? def beginning(time) if minutes? - time.change(:seconds => 0) + time.change(seconds: 0) else time.send(:"beginning_of_#{time_unit}") end diff --git a/test/config_test.rb b/test/config_test.rb index 3e45a5d..a9036e9 100644 --- a/test/config_test.rb +++ b/test/config_test.rb @@ -14,13 +14,12 @@ it 'initializes a config and overloads it with a block' do @config.namespace = 'something' - @config.namespace.must_equal 'something' end it "sets periods via counter method" do Von.configure do |config| - config.counter 'bar', :monthly => 3, :daily => 6 + config.counter 'bar', monthly: 3, daily: 6 end Von.config.periods[:bar].length.must_equal 2 @@ -32,8 +31,8 @@ it "sets bests via counter method" do Von.configure do |config| - config.counter 'bar', :best => :day - config.counter 'foo', :best => [ :month, :year ] + config.counter 'bar', best: :day + config.counter 'foo', best: [:month, :year] end Von.config.bests[:bar].first.must_be_instance_of Von::Period @@ -46,8 +45,8 @@ it "sets currents via counter method" do Von.configure do |config| - config.counter 'bar', :current => :day - config.counter 'foo', :current => [ :month, :year ] + config.counter 'bar', current: :day + config.counter 'foo', current: [:month, :year] end Von.config.currents[:bar].first.must_be_instance_of Von::Period diff --git a/test/counter_test.rb b/test/counter_test.rb index 066e284..b3bbece 100644 --- a/test/counter_test.rb +++ b/test/counter_test.rb @@ -21,10 +21,9 @@ Counter.new('foo:bar').total.must_equal 3 end - it "returns counts for a given period" do Von.configure do |config| - config.counter 'foo', :monthly => 2 + config.counter 'foo', monthly: 2 end Von.increment('foo') @@ -33,12 +32,12 @@ Timecop.freeze(Time.local(2013, 03)) Von.increment('foo') - Counter.new('foo').per(:month).must_equal [{:timestamp => "2013-02", :count => 1}, {:timestamp => "2013-03", :count => 1}] + Counter.new('foo').per(:month).must_equal [{ timestamp: "2013-02", count: 1 }, { timestamp: "2013-03", count: 1 }] end it "returns best count for a given period" do Von.configure do |config| - config.counter 'foo', :best => [:minute, :week] + config.counter 'foo', best: [:minute, :week] end Von.increment('foo') @@ -48,13 +47,13 @@ Timecop.freeze(Time.local(2013, 01, 20, 06, 10)) 3.times { Von.increment('foo') } - Counter.new('foo').best(:minute).must_equal({:timestamp => "2013-01-13 06:05", :count => 4}) - Counter.new('foo').best(:week).must_equal({:timestamp => "2013-01-07", :count => 4}) + Counter.new('foo').best(:minute).must_equal({ timestamp: "2013-01-13 06:05", count: 4 }) + Counter.new('foo').best(:week).must_equal({ timestamp: "2013-01-07", count: 4 }) end it "returns current count for a given period" do Von.configure do |config| - config.counter 'foo', :current => [:minute, :day] + config.counter 'foo', current: [:minute, :day] end 4.times { Von.increment('foo') } diff --git a/test/counters/period_test.rb b/test/counters/period_test.rb index 2ef7e77..4466c65 100644 --- a/test/counters/period_test.rb +++ b/test/counters/period_test.rb @@ -64,14 +64,14 @@ Timecop.freeze(Time.local(2013, 02, 01, 9)) counter.increment - counter.count(:month).must_equal [{:timestamp => "2013-02", :count => 2}] + counter.count(:month).must_equal [{ timestamp: "2013-02", count: 2 }] counter.count(:hour).must_equal [ - { :timestamp => "2013-02-01 04:00", :count => 0 }, - { :timestamp => "2013-02-01 05:00", :count => 0 }, - { :timestamp => "2013-02-01 06:00", :count => 0 }, - { :timestamp => "2013-02-01 07:00", :count => 1 }, - { :timestamp => "2013-02-01 08:00", :count => 0 }, - { :timestamp => "2013-02-01 09:00", :count => 1 } + { timestamp: "2013-02-01 04:00", count: 0 }, + { timestamp: "2013-02-01 05:00", count: 0 }, + { timestamp: "2013-02-01 06:00", count: 0 }, + { timestamp: "2013-02-01 07:00", count: 1 }, + { timestamp: "2013-02-01 08:00", count: 0 }, + { timestamp: "2013-02-01 09:00", count: 1 } ] end diff --git a/test/von_test.rb b/test/von_test.rb index f2cd28a..3a24100 100644 --- a/test/von_test.rb +++ b/test/von_test.rb @@ -22,7 +22,7 @@ it "increments period/best counters and counts them" do Von.configure do |config| - config.counter 'foo', :monthly => 2, :best => :day + config.counter 'foo', monthly: 2, best: :day end Von.increment('foo') @@ -32,8 +32,8 @@ Timecop.freeze(Time.local(2013, 03, 04)) Von.increment('foo') - Von.count('foo').best(:day).must_equal({:timestamp => "2013-02-03", :count => 2}) - Von.count('foo').per(:month).must_equal [{:timestamp => "2013-02", :count => 2}, {:timestamp => "2013-03", :count => 1}] + Von.count('foo').best(:day).must_equal({ timestamp: "2013-02-03", count: 2 }) + Von.count('foo').per(:month).must_equal [{ timestamp: "2013-02", count: 2 }, { timestamp: "2013-03", count: 1 }] end it "raises a Redis connection errors if raise_connection_errors is true" do