From 462cabd19eaab6ae2d48a083de101f0e1e2e2880 Mon Sep 17 00:00:00 2001 From: John Bachir Date: Fri, 2 Mar 2018 17:43:38 -0500 Subject: [PATCH 1/9] accommodate new API behavior As of May 2017, successful submissions to stathat do not return a body: https://blog.stathat.com/2017/05/05/bandwidth.html This changes the code and tests to accommodate this new behavior. - the success code is now 200, not 204, so all 200s are considered successful - because there is no body, we no longer check for resp.msg == "ok" - oddly, failures return a "status: 500" in their body, but still have a 200 success code. I don't know if this is desired behavior or a bug, and I don't know how long this has been the case. i have written to StatHat support about it. so, we still parse for a code in the body and let it take precedent --- lib/stathat.rb | 17 +++++++++++------ test/test_stathat.rb | 6 ++---- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/stathat.rb b/lib/stathat.rb index 711fb32..68041d6 100644 --- a/lib/stathat.rb +++ b/lib/stathat.rb @@ -22,8 +22,8 @@ def send_to_stathat(url, args) uri.query = args.map { |arg, val| arg.to_s + "=" + CGI::escape(val.to_s) }.join('&') end - resp = Net::HTTP.get(uri) - return Response.new(resp) + resp = Net::HTTP.get_response(uri) + return Response.new(resp.body, resp.code.to_i) end end end @@ -173,18 +173,23 @@ def enqueue(url, args, cb=nil) end class Response - def initialize(body) + def initialize(body, http_status) @body = body + @http_status = http_status @parsed = nil end def valid? - return status == 200 + return (200..299).cover? status end def status - parse - return @parsed['status'] + if @body + parse + return @parsed['status'] + else + return @http_status + end end def msg diff --git a/test/test_stathat.rb b/test/test_stathat.rb index 76140d9..932e90d 100644 --- a/test/test_stathat.rb +++ b/test/test_stathat.rb @@ -42,15 +42,13 @@ def test_classic_value_bad_keys def test_ez_value_sync resp = StatHat::SyncAPI.ez_post_value("test ez value stat", "test@stathat.com", 0.92) assert(resp.valid?, "response was invalid") - assert_equal(resp.msg, "ok", "message should be 'ok'") - assert_equal(resp.status, 200, "status should be 200") + assert_equal(resp.status, 204, "status should be 200") end def test_ez_count_sync resp = StatHat::SyncAPI.ez_post_value("test ez count stat", "test@stathat.com", 12) assert(resp.valid?, "response was invalid") - assert_equal(resp.msg, "ok", "message should be 'ok'") - assert_equal(resp.status, 200, "status should be 200") + assert_equal(resp.status, 204, "status should be 200") end def test_classic_count_bad_keys_sync From a258cd5b89988a5c9fd4067fabab3a27fb218d38 Mon Sep 17 00:00:00 2001 From: John Bachir Date: Fri, 2 Mar 2018 16:23:15 -0500 Subject: [PATCH 2/9] indent with 2 spaces instead of 8 --- lib/stathat.rb | 383 ++++++++++++++++++++++--------------------- test/test_stathat.rb | 108 ++++++------ 2 files changed, 246 insertions(+), 245 deletions(-) diff --git a/lib/stathat.rb b/lib/stathat.rb index 68041d6..1409d51 100644 --- a/lib/stathat.rb +++ b/lib/stathat.rb @@ -6,201 +6,202 @@ require 'singleton' module StatHat - class Common - CLASSIC_VALUE_URL = "https://api.stathat.com/v" - CLASSIC_COUNT_URL = "https://api.stathat.com/c" - EZ_URL = "https://api.stathat.com/ez" - - class << self - def send_to_stathat(url, args) - uri = URI.parse(url) - - begin - uri.query = URI.encode_www_form(args) - rescue NoMethodError => e - # backwards compatability for pre 1.9.x - uri.query = args.map { |arg, val| arg.to_s + "=" + CGI::escape(val.to_s) }.join('&') - end - - resp = Net::HTTP.get_response(uri) - return Response.new(resp.body, resp.code.to_i) - end - end - end - - class SyncAPI - class << self - def ez_post_value(stat_name, ezkey, value, timestamp=nil) - args = { :stat => stat_name, - :ezkey => ezkey, - :value => value } - args[:t] = timestamp unless timestamp.nil? - Common::send_to_stathat(Common::EZ_URL, args) - end - - def ez_post_count(stat_name, ezkey, count, timestamp=nil) - args = { :stat => stat_name, - :ezkey => ezkey, - :count => count } - args[:t] = timestamp unless timestamp.nil? - Common::send_to_stathat(Common::EZ_URL, args) - end - - def post_count(stat_key, user_key, count, timestamp=nil) - args = { :key => stat_key, - :ukey => user_key, - :count => count } - args[:t] = timestamp unless timestamp.nil? - Common::send_to_stathat(Common::CLASSIC_COUNT_URL, args) - end - - def post_value(stat_key, user_key, value, timestamp=nil) - args = { :key => stat_key, - :ukey => user_key, - :value => value } - args[:t] = timestamp unless timestamp.nil? - Common::send_to_stathat(Common::CLASSIC_VALUE_URL, args) - end - end - end - class API - class << self - def ez_post_value(stat_name, ezkey, value, timestamp=nil, &block) - Reporter.instance.ez_post_value(stat_name, ezkey, value, timestamp, block) - end + class Common + CLASSIC_VALUE_URL = "https://api.stathat.com/v" + CLASSIC_COUNT_URL = "https://api.stathat.com/c" + EZ_URL = "https://api.stathat.com/ez" - def ez_post_count(stat_name, ezkey, count, timestamp=nil, &block) - Reporter.instance.ez_post_count(stat_name, ezkey, count, timestamp, block) - end - - def post_count(stat_key, user_key, count, timestamp=nil, &block) - Reporter.instance.post_count(stat_key, user_key, count, timestamp, block) - end - - def post_value(stat_key, user_key, value, timestamp=nil, &block) - Reporter.instance.post_value(stat_key, user_key, value, timestamp, block) - end - end - end + class << self + def send_to_stathat(url, args) + uri = URI.parse(url) - class Reporter - include Singleton - - def initialize - @que = Queue.new - @runlock = Mutex.new - run_pool() - end - - def finish() - stop_pool - # XXX serialize queue? - end - - def post_value(stat_key, user_key, value, timestamp, cb) - args = { :key => stat_key, - :ukey => user_key, - :value => value } - args[:t] = timestamp unless timestamp.nil? - enqueue(Common::CLASSIC_VALUE_URL, args, cb) - end - - def post_count(stat_key, user_key, count, timestamp, cb) - args = { :key => stat_key, - :ukey => user_key, - :count => count } - args[:t] = timestamp unless timestamp.nil? - enqueue(Common::CLASSIC_COUNT_URL, args, cb) - end - - def ez_post_value(stat_name, ezkey, value, timestamp, cb) - args = { :stat => stat_name, - :ezkey => ezkey, - :value => value } - args[:t] = timestamp unless timestamp.nil? - enqueue(Common::EZ_URL, args, cb) - end - - def ez_post_count(stat_name, ezkey, count, timestamp, cb) - args = { :stat => stat_name, - :ezkey => ezkey, - :count => count } - args[:t] = timestamp unless timestamp.nil? - enqueue(Common::EZ_URL, args, cb) - end - - private - def run_pool - @runlock.synchronize { @running = true } - @pool = [] - 5.times do |i| - @pool[i] = Thread.new do - while true do - point = @que.pop - # XXX check for error? - begin - resp = Common::send_to_stathat(point[:url], point[:args]) - if point[:cb] - point[:cb].call(resp) - end - rescue - pp $! - end - @runlock.synchronize { - break unless @running - } - end - end - end - end - - def stop_pool() - @runlock.synchronize { - @running = false - } - @pool.each do |th| - th.join if th && th.alive? - end - end - - def enqueue(url, args, cb=nil) - return false unless @running - point = {:url => url, :args => args, :cb => cb} - @que << point - true - end + begin + uri.query = URI.encode_www_form(args) + rescue NoMethodError => e + # backwards compatability for pre 1.9.x + uri.query = args.map { |arg, val| arg.to_s + "=" + CGI::escape(val.to_s) }.join('&') end - class Response - def initialize(body, http_status) - @body = body - @http_status = http_status - @parsed = nil - end - - def valid? - return (200..299).cover? status - end - - def status - if @body - parse - return @parsed['status'] - else - return @http_status - end - end - - def msg - parse - return @parsed['msg'] - end - - private - def parse - return unless @parsed.nil? - @parsed = JSON.parse(@body) - end + resp = Net::HTTP.get_response(uri) + return Response.new(resp.body, resp.code.to_i) + end + end + end + + class SyncAPI + class << self + def ez_post_value(stat_name, ezkey, value, timestamp=nil) + args = { :stat => stat_name, + :ezkey => ezkey, + :value => value } + args[:t] = timestamp unless timestamp.nil? + Common::send_to_stathat(Common::EZ_URL, args) + end + + def ez_post_count(stat_name, ezkey, count, timestamp=nil) + args = { :stat => stat_name, + :ezkey => ezkey, + :count => count } + args[:t] = timestamp unless timestamp.nil? + Common::send_to_stathat(Common::EZ_URL, args) + end + + def post_count(stat_key, user_key, count, timestamp=nil) + args = { :key => stat_key, + :ukey => user_key, + :count => count } + args[:t] = timestamp unless timestamp.nil? + Common::send_to_stathat(Common::CLASSIC_COUNT_URL, args) + end + + def post_value(stat_key, user_key, value, timestamp=nil) + args = { :key => stat_key, + :ukey => user_key, + :value => value } + args[:t] = timestamp unless timestamp.nil? + Common::send_to_stathat(Common::CLASSIC_VALUE_URL, args) + end + end + end + + class API + class << self + def ez_post_value(stat_name, ezkey, value, timestamp=nil, &block) + Reporter.instance.ez_post_value(stat_name, ezkey, value, timestamp, block) + end + + def ez_post_count(stat_name, ezkey, count, timestamp=nil, &block) + Reporter.instance.ez_post_count(stat_name, ezkey, count, timestamp, block) + end + + def post_count(stat_key, user_key, count, timestamp=nil, &block) + Reporter.instance.post_count(stat_key, user_key, count, timestamp, block) + end + + def post_value(stat_key, user_key, value, timestamp=nil, &block) + Reporter.instance.post_value(stat_key, user_key, value, timestamp, block) + end + end + end + + class Reporter + include Singleton + + def initialize + @que = Queue.new + @runlock = Mutex.new + run_pool() + end + + def finish() + stop_pool + # XXX serialize queue? + end + + def post_value(stat_key, user_key, value, timestamp, cb) + args = { :key => stat_key, + :ukey => user_key, + :value => value } + args[:t] = timestamp unless timestamp.nil? + enqueue(Common::CLASSIC_VALUE_URL, args, cb) + end + + def post_count(stat_key, user_key, count, timestamp, cb) + args = { :key => stat_key, + :ukey => user_key, + :count => count } + args[:t] = timestamp unless timestamp.nil? + enqueue(Common::CLASSIC_COUNT_URL, args, cb) + end + + def ez_post_value(stat_name, ezkey, value, timestamp, cb) + args = { :stat => stat_name, + :ezkey => ezkey, + :value => value } + args[:t] = timestamp unless timestamp.nil? + enqueue(Common::EZ_URL, args, cb) + end + + def ez_post_count(stat_name, ezkey, count, timestamp, cb) + args = { :stat => stat_name, + :ezkey => ezkey, + :count => count } + args[:t] = timestamp unless timestamp.nil? + enqueue(Common::EZ_URL, args, cb) + end + + private + def run_pool + @runlock.synchronize { @running = true } + @pool = [] + 5.times do |i| + @pool[i] = Thread.new do + while true do + point = @que.pop + # XXX check for error? + begin + resp = Common::send_to_stathat(point[:url], point[:args]) + if point[:cb] + point[:cb].call(resp) + end + rescue + pp $! + end + @runlock.synchronize { + break unless @running + } + end end + end + end + + def stop_pool() + @runlock.synchronize { + @running = false + } + @pool.each do |th| + th.join if th && th.alive? + end + end + + def enqueue(url, args, cb=nil) + return false unless @running + point = {:url => url, :args => args, :cb => cb} + @que << point + true + end + end + + class Response + def initialize(body, http_status) + @body = body + @http_status = http_status + @parsed = nil + end + + def valid? + return (200..299).cover? status + end + + def status + if @body + parse + return @parsed['status'] + else + return @http_status + end + end + + def msg + parse + return @parsed['msg'] + end + + private + def parse + return unless @parsed.nil? + @parsed = JSON.parse(@body) + end + end end diff --git a/test/test_stathat.rb b/test/test_stathat.rb index 932e90d..401d7b6 100644 --- a/test/test_stathat.rb +++ b/test/test_stathat.rb @@ -3,65 +3,65 @@ class TestStathat < MiniTest::Unit::TestCase - def test_ez_value - StatHat::API.ez_post_value("test ez value stat", "test@stathat.com", 0.92) do |resp| - assert(resp.valid?, "response was invalid") - assert_equal(resp.msg, "ok", "message should be 'ok'") - assert_equal(resp.status, 200, "status should be 200") - end - sleep(1) - end + def test_ez_value + StatHat::API.ez_post_value("test ez value stat", "test@stathat.com", 0.92) do |resp| + assert(resp.valid?, "response was invalid") + assert_equal(resp.msg, "ok", "message should be 'ok'") + assert_equal(resp.status, 200, "status should be 200") + end + sleep(1) + end - def test_ez_count - StatHat::API.ez_post_value("test ez count stat", "test@stathat.com", 12) do |r| - assert(r.valid?, "response was invalid") - assert_equal(r.msg, "ok", "message should be 'ok'") - assert_equal(r.status, 200, "status should be 200") - end - sleep(1) - end + def test_ez_count + StatHat::API.ez_post_value("test ez count stat", "test@stathat.com", 12) do |r| + assert(r.valid?, "response was invalid") + assert_equal(r.msg, "ok", "message should be 'ok'") + assert_equal(r.status, 200, "status should be 200") + end + sleep(1) + end - def test_classic_count_bad_keys - StatHat::API.post_count("XXXXXXXX", "YYYYYYYY", 12) do |r| - assert_equal(r.valid?, false, "response was valid") - assert_equal(r.msg, "invalid keys", "incorrect error message") - assert_equal(r.status, 500, "incorrect status code") - end - sleep(1) - end + def test_classic_count_bad_keys + StatHat::API.post_count("XXXXXXXX", "YYYYYYYY", 12) do |r| + assert_equal(r.valid?, false, "response was valid") + assert_equal(r.msg, "invalid keys", "incorrect error message") + assert_equal(r.status, 500, "incorrect status code") + end + sleep(1) + end - def test_classic_value_bad_keys - StatHat::API.post_value("ZZZZZZZZ", "YYYYYYYYY", 0.92) do |r| - assert_equal(r.valid?, false, "response was valid") - assert_equal(r.msg, "invalid keys", "incorrect error message") - assert_equal(r.status, 500, "incorrect status code") - end - sleep(1) - end + def test_classic_value_bad_keys + StatHat::API.post_value("ZZZZZZZZ", "YYYYYYYYY", 0.92) do |r| + assert_equal(r.valid?, false, "response was valid") + assert_equal(r.msg, "invalid keys", "incorrect error message") + assert_equal(r.status, 500, "incorrect status code") + end + sleep(1) + end - def test_ez_value_sync - resp = StatHat::SyncAPI.ez_post_value("test ez value stat", "test@stathat.com", 0.92) - assert(resp.valid?, "response was invalid") - assert_equal(resp.status, 204, "status should be 200") - end + def test_ez_value_sync + resp = StatHat::SyncAPI.ez_post_value("test ez value stat", "test@stathat.com", 0.92) + assert(resp.valid?, "response was invalid") + assert_equal(resp.status, 204, "status should be 200") + end - def test_ez_count_sync - resp = StatHat::SyncAPI.ez_post_value("test ez count stat", "test@stathat.com", 12) - assert(resp.valid?, "response was invalid") - assert_equal(resp.status, 204, "status should be 200") - end + def test_ez_count_sync + resp = StatHat::SyncAPI.ez_post_value("test ez count stat", "test@stathat.com", 12) + assert(resp.valid?, "response was invalid") + assert_equal(resp.status, 204, "status should be 200") + end - def test_classic_count_bad_keys_sync - r = StatHat::SyncAPI.post_count("XXXXXXXX", "YYYYYYYY", 12) - assert_equal(r.valid?, false, "response was valid") - assert_equal(r.msg, "invalid keys", "incorrect error message") - assert_equal(r.status, 500, "incorrect status code") - end + def test_classic_count_bad_keys_sync + r = StatHat::SyncAPI.post_count("XXXXXXXX", "YYYYYYYY", 12) + assert_equal(r.valid?, false, "response was valid") + assert_equal(r.msg, "invalid keys", "incorrect error message") + assert_equal(r.status, 500, "incorrect status code") + end - def test_classic_value_bad_keys_sync - r = StatHat::SyncAPI.post_value("ZZZZZZZZ", "YYYYYYYYY", 0.92) - assert_equal(r.valid?, false, "response was valid") - assert_equal(r.msg, "invalid keys", "incorrect error message") - assert_equal(r.status, 500, "incorrect status code") - end + def test_classic_value_bad_keys_sync + r = StatHat::SyncAPI.post_value("ZZZZZZZZ", "YYYYYYYYY", 0.92) + assert_equal(r.valid?, false, "response was valid") + assert_equal(r.msg, "invalid keys", "incorrect error message") + assert_equal(r.status, 500, "incorrect status code") + end end From 71c637040cd791a3ed4adddb04137984d5696750 Mon Sep 17 00:00:00 2001 From: John Bachir Date: Fri, 2 Mar 2018 16:25:27 -0500 Subject: [PATCH 3/9] remove cruft comments --- Gemfile | 9 --------- test/helper.rb | 8 -------- 2 files changed, 17 deletions(-) diff --git a/Gemfile b/Gemfile index 1604589..5fc5ebb 100644 --- a/Gemfile +++ b/Gemfile @@ -1,14 +1,5 @@ source "http://rubygems.org" -# Add dependencies required to use your gem here. -# Example: -# gem "activesupport", ">= 2.3.5" -# Add dependencies to develop your gem here. -# Include everything needed to run rake, tests, features, etc. group :development do gem "minitest", ">= 0" -# gem "bundler", "~> 1.0.0" -# gem "jeweler", "~> 1.5.2" -# gem "rcov", ">= 0" -# gem "rocco" end diff --git a/test/helper.rb b/test/helper.rb index be4465f..473ab92 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -1,12 +1,4 @@ require 'rubygems' -#require 'bundler' -#begin -# Bundler.setup(:default, :development) -#rescue Bundler::BundlerError => e -# $stderr.puts e.message -# $stderr.puts "Run `bundle install` to install missing gems" -# exit e.status_code -#end require 'minitest/unit' $LOAD_PATH.unshift(File.dirname(__FILE__)) From 3fed302034ccd154960cc6bf4f1d71548371e7ca Mon Sep 17 00:00:00 2001 From: John Bachir Date: Fri, 2 Mar 2018 16:27:09 -0500 Subject: [PATCH 4/9] remove support for <1.9 --- lib/stathat.rb | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/lib/stathat.rb b/lib/stathat.rb index 1409d51..6debf4e 100644 --- a/lib/stathat.rb +++ b/lib/stathat.rb @@ -15,14 +15,7 @@ class Common class << self def send_to_stathat(url, args) uri = URI.parse(url) - - begin - uri.query = URI.encode_www_form(args) - rescue NoMethodError => e - # backwards compatability for pre 1.9.x - uri.query = args.map { |arg, val| arg.to_s + "=" + CGI::escape(val.to_s) }.join('&') - end - + uri.query = URI.encode_www_form(args) resp = Net::HTTP.get_response(uri) return Response.new(resp.body, resp.code.to_i) end From 054c8eacdb1c26c0a02f337e5f8a779674ed6b5f Mon Sep 17 00:00:00 2001 From: John Bachir Date: Fri, 2 Mar 2018 16:27:51 -0500 Subject: [PATCH 5/9] don't use return when not necessary --- lib/stathat.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/stathat.rb b/lib/stathat.rb index 6debf4e..d3b317e 100644 --- a/lib/stathat.rb +++ b/lib/stathat.rb @@ -17,7 +17,7 @@ def send_to_stathat(url, args) uri = URI.parse(url) uri.query = URI.encode_www_form(args) resp = Net::HTTP.get_response(uri) - return Response.new(resp.body, resp.code.to_i) + Response.new(resp.body, resp.code.to_i) end end end @@ -174,21 +174,21 @@ def initialize(body, http_status) end def valid? - return (200..299).cover? status + (200..299).cover? status end def status if @body parse - return @parsed['status'] + @parsed['status'] else - return @http_status + @http_status end end def msg parse - return @parsed['msg'] + @parsed['msg'] end private From 2cd6e7f50068789f6639ad834bfbb71d2c25dbe6 Mon Sep 17 00:00:00 2001 From: John Bachir Date: Fri, 2 Mar 2018 16:28:49 -0500 Subject: [PATCH 6/9] use non-indented style for private section --- lib/stathat.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/stathat.rb b/lib/stathat.rb index d3b317e..48313bd 100644 --- a/lib/stathat.rb +++ b/lib/stathat.rb @@ -124,7 +124,8 @@ def ez_post_count(stat_name, ezkey, count, timestamp, cb) enqueue(Common::EZ_URL, args, cb) end - private + private + def run_pool @runlock.synchronize { @running = true } @pool = [] @@ -191,7 +192,8 @@ def msg @parsed['msg'] end - private + private + def parse return unless @parsed.nil? @parsed = JSON.parse(@body) From 3a1dd39a692440a8f0ae3f3f0145da42a0405e0e Mon Sep 17 00:00:00 2001 From: John Bachir Date: Fri, 2 Mar 2018 16:36:39 -0500 Subject: [PATCH 7/9] ruby 2 hash style --- lib/stathat.rb | 34 +++++++++------------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/lib/stathat.rb b/lib/stathat.rb index 48313bd..b1f6a0d 100644 --- a/lib/stathat.rb +++ b/lib/stathat.rb @@ -25,33 +25,25 @@ def send_to_stathat(url, args) class SyncAPI class << self def ez_post_value(stat_name, ezkey, value, timestamp=nil) - args = { :stat => stat_name, - :ezkey => ezkey, - :value => value } + args = { stat: stat_name, ezkey: ezkey, value: value } args[:t] = timestamp unless timestamp.nil? Common::send_to_stathat(Common::EZ_URL, args) end def ez_post_count(stat_name, ezkey, count, timestamp=nil) - args = { :stat => stat_name, - :ezkey => ezkey, - :count => count } + args = { stat: stat_name, ezkey: ezkey, count: count } args[:t] = timestamp unless timestamp.nil? Common::send_to_stathat(Common::EZ_URL, args) end def post_count(stat_key, user_key, count, timestamp=nil) - args = { :key => stat_key, - :ukey => user_key, - :count => count } + args = { key: stat_key, ukey: user_key, count: count } args[:t] = timestamp unless timestamp.nil? Common::send_to_stathat(Common::CLASSIC_COUNT_URL, args) end def post_value(stat_key, user_key, value, timestamp=nil) - args = { :key => stat_key, - :ukey => user_key, - :value => value } + args = { key: stat_key, ukey: user_key, value: value } args[:t] = timestamp unless timestamp.nil? Common::send_to_stathat(Common::CLASSIC_VALUE_URL, args) end @@ -93,33 +85,25 @@ def finish() end def post_value(stat_key, user_key, value, timestamp, cb) - args = { :key => stat_key, - :ukey => user_key, - :value => value } + args = { key: stat_key, ukey: user_key, value: value } args[:t] = timestamp unless timestamp.nil? enqueue(Common::CLASSIC_VALUE_URL, args, cb) end def post_count(stat_key, user_key, count, timestamp, cb) - args = { :key => stat_key, - :ukey => user_key, - :count => count } + args = { key: stat_key, ukey: user_key, count: count } args[:t] = timestamp unless timestamp.nil? enqueue(Common::CLASSIC_COUNT_URL, args, cb) end def ez_post_value(stat_name, ezkey, value, timestamp, cb) - args = { :stat => stat_name, - :ezkey => ezkey, - :value => value } + args = { stat: stat_name, ezkey: ezkey, value: value } args[:t] = timestamp unless timestamp.nil? enqueue(Common::EZ_URL, args, cb) end def ez_post_count(stat_name, ezkey, count, timestamp, cb) - args = { :stat => stat_name, - :ezkey => ezkey, - :count => count } + args = { stat: stat_name, ezkey: ezkey, count: count } args[:t] = timestamp unless timestamp.nil? enqueue(Common::EZ_URL, args, cb) end @@ -161,7 +145,7 @@ def stop_pool() def enqueue(url, args, cb=nil) return false unless @running - point = {:url => url, :args => args, :cb => cb} + point = { url: url, args: args, cb: cb } @que << point true end From 6f99ed539e16c6d821792f55e9e29f63dc4ec44a Mon Sep 17 00:00:00 2001 From: John Bachir Date: Fri, 2 Mar 2018 18:01:51 -0500 Subject: [PATCH 8/9] remove unnecessary/inconsistent () --- lib/stathat.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/stathat.rb b/lib/stathat.rb index b1f6a0d..2461951 100644 --- a/lib/stathat.rb +++ b/lib/stathat.rb @@ -76,10 +76,10 @@ class Reporter def initialize @que = Queue.new @runlock = Mutex.new - run_pool() + run_pool end - def finish() + def finish stop_pool # XXX serialize queue? end @@ -134,7 +134,7 @@ def run_pool end end - def stop_pool() + def stop_pool @runlock.synchronize { @running = false } From 26238d35d3ebb96bf191bdc1281df3b3faa2071e Mon Sep 17 00:00:00 2001 From: John Bachir Date: Fri, 2 Mar 2018 18:05:29 -0500 Subject: [PATCH 9/9] tidy up block style - consistent use of {} for one-line blocks - use do..end for more complicated block --- lib/stathat.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/stathat.rb b/lib/stathat.rb index 2461951..5a12351 100644 --- a/lib/stathat.rb +++ b/lib/stathat.rb @@ -126,18 +126,16 @@ def run_pool rescue pp $! end - @runlock.synchronize { + @runlock.synchronize do break unless @running - } + end end end end end def stop_pool - @runlock.synchronize { - @running = false - } + @runlock.synchronize { @running = false } @pool.each do |th| th.join if th && th.alive? end