Skip to content

Instantly share code, notes, and snippets.

View ratelimit.js
/* 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 Aug 24, 2012
Google Web server application OAuth flow, step 1
View gist:3450666
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 Aug 24, 2012
Google Web server application OAuth flow, step 2
View gist:3450785
credentials = flow.step2_exchange(code) # code from the localhost redirect
@milancermak
milancermak / gist:3451039
Created Aug 24, 2012
Example of creating a OAuth2Credentials with dummy access token and a working refresh token
View gist:3451039
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:4196125
Created Dec 3, 2012
killpaste bookmarklet
View gist:4196125
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 Dec 15, 2012
Installing Ruby 1.9.2 on OS X 10.8
View gist:4295532
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 Dec 11, 2015
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)
View gist:4518064
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 Jun 13, 2013
Question about combining signals in ReactiveCocoa
View gist:5775152
@interface LoginViewController ()
@property (strong, nonatomic) RACSubject *textFieldReturnPressed;
@property (strong, nonatomic) UITextField *usernameTextField;
@property (strong, nonatomic) UITextField *passwordTextField;
@end
@implementation LoginViewController
@milancermak
milancermak / classMethodIntrospection.swift
Created Dec 21, 2015
A way how to introspect methods in Swift at runtime
View classMethodIntrospection.swift
var methodCount: UInt32 = 0
let methods = class_copyMethodList(anInstance.dynamicType, &methodCount) // replace anInstace
for i in 0..<Int(methodCount) {
NSLog("method: \(NSStringFromSelector(method_getName(methods[i])))")
}
@milancermak
milancermak / gist:3451509
Created Aug 24, 2012
Example of validating a recurring payment from Google Play using the Web server application OAuth 2.0 flow
View gist:3451509
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__":