Skip to content

Instantly share code, notes, and snippets.

/* Use this to cause a function to fire no more than once every 'ms' milliseconds.
For example, an expensive mousemove handler:
$('body').mouseover(ratelimit(function(ev) {
// ...
}, 250));
*/
function ratelimit(fn, ms) {
var last = (new Date()).getTime();
@milancermak
milancermak / gist:3450666
Created August 24, 2012 13:44
Google Web server application OAuth flow, step 1
from oauth2client.client import OAuth2WebServerFlow
flow = OAuth2WebServerFlow(client_id="some-random-hash.apps.googleusercontent.com",
client_secret="your-client-secret",
scope="https://www.googleapis.com/auth/androidpublisher")
auth_url = flow.step1_get_authorize_url(redirect_uri="http://localhost")
@milancermak
milancermak / gist:3450785
Created August 24, 2012 13:53
Google Web server application OAuth flow, step 2
credentials = flow.step2_exchange(code) # code from the localhost redirect
@milancermak
milancermak / gist:3451039
Created August 24, 2012 14:10
Example of creating a OAuth2Credentials with dummy access token and a working refresh token
import datetime
import httplib2
from oauth2client.client import OAuth2Credentials
credentials = OAuth2Credentials("irrelevant access token",
"some-random-hash.apps.googleusercontent.com", # Client ID
"your-client-secret",
"refresh-token",
datetime.datetime.now(), # token expiry
"https://accounts.google.com/o/oauth2/token")
@milancermak
milancermak / gist:3451509
Created August 24, 2012 14:47
Example of validating a recurring payment from Google Play using the Web server application OAuth 2.0 flow
import datetime
import httplib2
# to see in detail what's going on, uncomment
# httplib2.debuglevel = 4
from apiclient.discovery import build
from oauth2client.client import OAuth2Credentials, OAuth2WebServerFlow
if __name__ == "__main__":
@milancermak
milancermak / pre-commit.sh
Created October 7, 2012 19:24
Python pre-commit hook
#!/bin/sh
# make sure requirements.txt is up to date with every commit
# by comparing the output of pip freeze
pip freeze | diff requirements.txt - > /dev/null
if [ $? != 0 ]
then
echo "Missing python module dependencies in requirements.txt. Run 'pip freeze > requirements.txt' to update."
exit 1
fi
@milancermak
milancermak / gist:4196125
Created December 3, 2012 16:32
killpaste bookmarklet
javascript:(function(){var%20H=["cut","copy","paste","contextmenu","mousedown"],%20Z=[],%20s="",%20j;%20function%20R(N,a){%20while%20(N[a])%20{%20Z[a]=Z[a]?Z[a]+1:1;%20N[a]=null;%20}%20}%20function%20zapEH(N)%20{%20var%20a,i,C;%20for%20(j%20in%20H)%20R(N,"on"+H[j]);%20C=N.childNodes;%20for%20(i=0;i<C.length;++i)%20zapEH(C[i]);%20}%20zapEH(document);%20for%20(j%20in%20Z)%20s%20+=%20j%20+%20"%20("%20+%20Z[j]%20+%20")\n";%20if(s)%20alert("Zapped%20event%20handlers:\n\n"+s);%20else%20alert("No%20event%20handlers%20found.");})();
@milancermak
milancermak / gist:4295532
Created December 15, 2012 14:23
Installing Ruby 1.9.2 on OS X 10.8
curl -L https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
rvm install 1.9.2 --reconfigure --debug -C --enable-pthread --with-gcc=clang
# ruby --version
# ruby 1.9.2p320 (2012-04-20 revision 35421) [x86_64-darwin12.2.0]
@milancermak
milancermak / gist:4518064
Last active December 11, 2015 00:38
An example of a good error message coming from an HTTP API (illustrative for http://milancermak.posterous.com/http-apis-and-errors-doing-it-the-right-way)
HTTP/1.1 402 Payment Required
Content-Type: application/json
{
"title": "End of trial",
"description": "Unfortunately, your trial period has ended. To continue using the application, you need to purchase a subscription.",
"explanation_url": "https://example.com/troubleshooting/payment-required",
"trace_url": "https://apilog.example.com/f26a3e1dc"
}
@milancermak
milancermak / gist:5775152
Created June 13, 2013 16:28
Question about combining signals in ReactiveCocoa
@interface LoginViewController ()
@property (strong, nonatomic) RACSubject *textFieldReturnPressed;
@property (strong, nonatomic) UITextField *usernameTextField;
@property (strong, nonatomic) UITextField *passwordTextField;
@end
@implementation LoginViewController