Skip to content

Instantly share code, notes, and snippets.

@stahnma
Created March 12, 2014 00:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stahnma/a8e24ecea840ebcd5b73 to your computer and use it in GitHub Desktop.
Save stahnma/a8e24ecea840ebcd5b73 to your computer and use it in GitHub Desktop.
From ed25bc82b63973d6dc25eb49923166a35d915d77 Mon Sep 17 00:00:00 2001
From: Chris Hoge <chris.hoge@puppetlabs.com>
Date: Thu, 6 Mar 2014 13:32:28 -0800
Subject: [PATCH] Added in place sorting for fitbit leader board
Sort fitbit leaders by adding them to an array in place.
The sort is implicit with the insertion, assuming that there
are no key collisions.
---
files/scripts/fitbit.coffee | 30 ++++++++++++++++++------------
1 file changed, 18 insertions(+), 12 deletions(-)
diff --git a/files/scripts/fitbit.coffee b/files/scripts/fitbit.coffee
index c14cd89..44867f7 100644
--- a/files/scripts/fitbit.coffee
+++ b/files/scripts/fitbit.coffee
@@ -36,23 +36,29 @@ module.exports = (robot) ->
# Show the top five users
robot.respond /fitbit (steps|leaderboard|leaders)/i, (msg) ->
- params =
- token:
- oauth_token: config.secrets.oauthToken
- oauth_token_secret: config.secrets.oauthTokenSecret
+ try
+ params =
+ token:
+ oauth_token: config.secrets.oauthToken
+ oauth_token_secret: config.secrets.oauthTokenSecret
- fitbit.apiCall 'GET', '/user/-/friends/leaderboard.json', params, (err, response, json) ->
+ fitbit.apiCall 'GET', '/user/-/friends/leaderboard.json', params, (err, response, json) ->
- if err?
- displayErrors err, msg
- return
+ if err?
+ displayErrors err, msg
+ return
- leaders = json.friends
+ leaders = json.friends
+ sortedleaders = []
- for own key, leader of leaders
+ for own key, leader of leaders
+ if leader.summary.steps > 0
+ rank = leader.rank.steps * 1 # force conversion to a number
+ sortedleaders[rank] = "##{leader.rank.steps} #{leader.user.displayName} - #{leader.summary.steps}"
- if leader.summary.steps > 0
- msg.send "##{leader.rank.steps} #{leader.user.displayName} - #{leader.summary.steps}"
+ msg.send sortedleaders.join("\n")
+ catch err
+ msg.send "whoops, the fitbit leaders function failed"
# Who are my friends?
robot.respond /fitbit friends/i, (msg) ->
--
1.8.5.5
@azizshamim
Copy link

if only there were a website where we could share this kind of code without having to resort to diffs in fucking text... right?

/cc @rick @jameswhite @jfryman (does this shit even work?)

@rick
Copy link

rick commented Mar 12, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment