Skip to content

Instantly share code, notes, and snippets.

View kalharbi's full-sized avatar

Khalid Alharbi kalharbi

  • Saudi Arabia
View GitHub Profile
@kalharbi
kalharbi / log.rb
Created July 11, 2014 21:30
Ruby Logging - different channels for different levels. Log errors to FILE and all levels to STDOUT.
require 'logging' # gem install logging
require 'singleton'
class Log
include Singleton
attr_accessor :log_file_name
@@file_name = nil
# set log file name
@kalharbi
kalharbi / load_ajax.js
Created July 2, 2014 05:42
PhantomJS - Load dynamic web page content that uses AJAX
// Original code:
// https://github.com/ariya/phantomjs/blob/master/examples/waitfor.js
// https://github.com/ariya/phantomjs/blob/master/examples/rasterize.js
/**
* Wait until the test condition is true or a timeout occurs. Useful for waiting
* on a server response or for a ui change (fadeIn, etc.) to occur.
*
* @param testFx javascript condition that evaluates to a boolean,
* it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
@kalharbi
kalharbi / gist:ac181291ccf7e601927f
Last active August 29, 2015 14:02
Retrieve zip file from MongoDB GridFS and save it in a directory
MongoClient mongoClient = new MongoClient("localhost", 27017);
DB db = mongoClient.getDB("archives");
GridFS gridGfs = new GridFS(db, "fs");
// search by metadata
BasicDBObject query = new BasicDBObject("metadata.n", "myZipFile")
.append("metadata.ver", "1.2.6");
GridFSDBFile zipFile = gridGfs.findOne(query);
File outFile = new File("/Users/Khalid/git/gists/file.zip");
outFile.createNewFile();
zipFile.writeTo(outFile);
@kalharbi
kalharbi / gist:56dd376fe49c47d9b9be
Created May 6, 2014 05:00
A custom JSON encoder that excludes null or empty pair values.
from json import JSONEncoder
class CustomJsonEncoder(JSONEncoder):
def remove_none(self, data):
if isinstance(data, dict):
return {k:self.remove_none(v) for k, v in data.items() if k and v }
elif isinstance(data, list):
return [self.remove_none(item) for item in data if item]
elif isinstance(data, set):