Skip to content

Instantly share code, notes, and snippets.

View pbojinov's full-sized avatar

Petar Bojinov pbojinov

View GitHub Profile
Understanding PATCH
As its name implies, a REST-based API involves passing around the representation of the state of a resource. This is most commonly applied to HTTP, which is very straightforward: to read the current state of a resource, perform a GET operation on the resource’s URI. To update a resource, pass the new representation of the resource to its URI in a PUT operation. The PUT method is defined as a complete replacement of the resource at a given URI, meaning you must always provide the full representation that you want to set. But what if you only want to update a few fields in the resource? That’s done with the PATCH method.
The exact semantics of how the body of a PATCH request is applied to the requested resource are determined by the media type of the request. The way GitHub (and many other JSON APIs) handles PATCH requests is that you provide the JSON representation of the resource to update, omitting any fields that should be left unchanged. So for example, to update only the description
@pbojinov
pbojinov / populate_sqlite.py
Created June 10, 2014 16:57
Create and populate a sqlite database using python sqlite3 adapter
# first create a databse, from the command line run
# > touch hello_world.db
from datetime import datetime, timedelta
import sqlite3
conn = sqlite3.connect('hello_world.db')
c = conn.cursor()
# Create table
@pbojinov
pbojinov / new_tab_bookmark.txt
Created June 11, 2014 21:19
open up bookmark in new tab
// add void(0) so it works in firefox as well
javascript:window.open('https://web.any.do');void(0);
@pbojinov
pbojinov / prototype.js
Last active August 29, 2015 14:02
function prototype (extending objects has its drawbacks) from Crockford's JavaScript: The Good Parts
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
Number.method('integer', function () {
return Math[this < 0 ? 'ceil' : 'floor'](this);
});
console.log((-20 / 3).integer()); // -6
@pbojinov
pbojinov / python.assertions.md
Created July 13, 2014 06:44
python assertions

The TestCase class provides a number of methods to check for and report failures, such as:

Method Checks that
assertEqual(a, b) a == b
assertNotEqual(a, b) a != b
assertTrue(x) bool(x) is True
assertFalse(x) bool(x) is False
assertIs(a, b) a is b
assertIsNot(a, b) a is not b
# ---------------------------------------------------------------------------
#
# Description: This file holds all my BASH configurations and aliases
#
# Sections:
# 1. Environment Configuration
# 2. Make Terminal Better (remapping defaults and adding functionality)
# 3. File and Folder Management
# 4. Searching
# 5. Process Management
// http://stackoverflow.com/a/979995/907388
// not as fast as doing a regex though - http://jsperf.com/query-parameter-parsing
// https://gist.github.com/pbojinov/ee5a9bb7df7986a6e985
var QueryString = function () {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
var query_string = {};
var query = window.location.search.substring(1);
var vars = query.split("&");
// Much faster than string splitting when you need to get a parameter
// http://jsperf.com/query-parameter-parsing
function gup(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null) {
return "";
/**
* Runs the risk of becoming out of date; not very maintainable.
*/
.blue {
color: blue;
}
/**
* Depends on location in order to be rendered properly.
*/