Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ PLATFORMS
DEPENDENCIES
fakeweb (~> 1.3.0)
geckoboard-push!
httparty (~> 0.8.1)
mocha (~> 0.10.0)
rake
rdoc
16 changes: 15 additions & 1 deletion lib/geckoboard/push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
5 changes: 5 additions & 0 deletions test/geckoboard/push_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" => {"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

def expect_http_request(json)
response = Net::HTTPOK.new("1.1", 200, "OK")
response.instance_variable_set(:@body, '{"success":true}')
Expand Down