From 11a46882106c5f5c7048c24cc9c497629f62c81f Mon Sep 17 00:00:00 2001 From: Neil Bartley Date: Sat, 6 Jun 2015 00:04:32 +0100 Subject: [PATCH 1/4] Added support for leaderboard --- lib/geckoboard/push.rb | 14 ++++++++++++++ test/geckoboard/push_test.rb | 5 +++++ 2 files changed, 19 insertions(+) diff --git a/lib/geckoboard/push.rb b/lib/geckoboard/push.rb index d2b5600..81c880a 100644 --- a/lib/geckoboard/push.rb +++ b/lib/geckoboard/push.rb @@ -93,5 +93,19 @@ def funnel(items, reverse = false, hide_percentage = false) opts[:percentage] = "hide" if hide_percentage self.push(opts) end + + # Items should be an array of hashes, each hash containing: + # - value (numeric value) + # - label (optional) + # - previous_rank (optional) + def leaderboard(items) + data = items.collect do |item| + d = {:value => item[:value], :label => item[:label]} + d[:previous_rank] = item[:previous_rank] if item.include?(:previous_rank) + d + end + opts = {:items => data} + self.push(opts) + end end end diff --git a/test/geckoboard/push_test.rb b/test/geckoboard/push_test.rb index e68ce56..cb23fb6 100644 --- a/test/geckoboard/push_test.rb +++ b/test/geckoboard/push_test.rb @@ -65,6 +65,11 @@ def test_funnel assert_equal true, @push.funnel([{:label => "Test1", :value => 5}, {:label => "Test2", :value => 10}], true, true) end + def test_leaderboard + expect_http_request({"api_key" => "12345", "data" => {"item" => [{"value" => 5, "label" => "Test1"}, {"value" => 10, "label" => "Test2", "previous_rank" => "2"}]}}.to_json) + assert_equal true, @push.leaderboard([{:label => "Test1", :value => 5}, {:label => "Test2", :value => 10, :previous_rank => 2}]) + end + def expect_http_request(json) response = Net::HTTPOK.new("1.1", 200, "OK") response.instance_variable_set(:@body, '{"success":true}') From f2f150aa83701905fe171302060087ab10d7b39d Mon Sep 17 00:00:00 2001 From: Neil Bartley Date: Sat, 6 Jun 2015 00:05:40 +0100 Subject: [PATCH 2/4] Added HTTParty::Response fix from https://github.com/kickcode/geckoboard-push/pull/6 --- lib/geckoboard/push.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/geckoboard/push.rb b/lib/geckoboard/push.rb index 81c880a..b5de97f 100644 --- a/lib/geckoboard/push.rb +++ b/lib/geckoboard/push.rb @@ -24,7 +24,7 @@ def initialize(widget_key) # Makes a call to Geckoboard to push data to the current widget def push(data) raise Geckoboard::Push::Error.new("Api key not configured.") if Geckoboard::Push.api_key.nil? || Geckoboard::Push.api_key.empty? - result = JSON.parse(self.class.post("/#{Geckoboard::Push.api_version || 'v1'}/send/#{@widget_key}", {:body => {:api_key => Geckoboard::Push.api_key, :data => data}.to_json})) + result = JSON.parse(self.class.post("/#{Geckoboard::Push.api_version || 'v1'}/send/#{@widget_key}", {:body => {:api_key => Geckoboard::Push.api_key, :data => data}.to_json}).body) raise Geckoboard::Push::Error.new(result["error"]) unless result["success"] result["success"] end From cf8421f408ab82ac45227fd1a16f210373d66a28 Mon Sep 17 00:00:00 2001 From: Neil Bartley Date: Sun, 12 Jul 2015 22:30:08 +0100 Subject: [PATCH 3/4] Updated Gemfile.lock --- Gemfile.lock | 1 - 1 file changed, 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 1d5d0ad..76cd75c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -27,7 +27,6 @@ PLATFORMS DEPENDENCIES fakeweb (~> 1.3.0) geckoboard-push! - httparty (~> 0.8.1) mocha (~> 0.10.0) rake rdoc From 7e6c0640f76fc5724760c0dac35a28568bce7d00 Mon Sep 17 00:00:00 2001 From: Neil Bartley Date: Sun, 12 Jul 2015 22:35:52 +0100 Subject: [PATCH 4/4] items not item --- test/geckoboard/push_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/geckoboard/push_test.rb b/test/geckoboard/push_test.rb index cb23fb6..10bfdd9 100644 --- a/test/geckoboard/push_test.rb +++ b/test/geckoboard/push_test.rb @@ -66,7 +66,7 @@ def test_funnel end def test_leaderboard - expect_http_request({"api_key" => "12345", "data" => {"item" => [{"value" => 5, "label" => "Test1"}, {"value" => 10, "label" => "Test2", "previous_rank" => "2"}]}}.to_json) + expect_http_request({"api_key" => "12345", "data" => {"items" => [{"value" => 5, "label" => "Test1"}, {"value" => 10, "label" => "Test2", "previous_rank" => "2"}]}}.to_json) assert_equal true, @push.leaderboard([{:label => "Test1", :value => 5}, {:label => "Test2", :value => 10, :previous_rank => 2}]) end