Skip to content

Instantly share code, notes, and snippets.

@alvinsj
alvinsj / memoize.rb
Last active December 14, 2015 08:28
redis memoization, sidekiq background job (with unique job)
module Memoize
def remember(klass, method_name)
klass.class_eval do
memory = $redis
original = "original_#{method_name}"
original_method = method(method_name)
define_singleton_method(method_name) do |*args|
@alvinsj
alvinsj / data.rb
Last active December 14, 2015 21:39
custom liquid filter + drop
class Data
attr_accessor :date_range
def initialize(type)
@type = type
end
def graph_points
SomeClass.graph_points(
@type,
@alvinsj
alvinsj / gist:5245030
Created March 26, 2013 12:32
chmod 644 to files, 755 to directories
$ find /path/to/dir -type f -print0 | xargs -I {} -0 chmod 0644 {}
$ find /path/to/dir -type d -print0 | xargs -I {} -0 chmod 0755 {}
@alvinsj
alvinsj / daemon.rb
Last active December 15, 2015 15:38
Setup Proxy Server in Ruby
require 'daemons'
Daemons.run('./proxy.rb')
# To fix:
# LoadError: dlopen(/Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib
# Referenced from: /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
# Reason: image not found – /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
# from /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle
# from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require’
# from /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/lib/mysql2.rb:7
# from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `gem_original_require’
# from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `require’
# from (irb):1
class CreateTableS3Uploads < ActiveRecord::Migration
def up
create_table :s3_uploads do |t|
t. :url
t.timestamps
end
end
def down
drop_table :s3_uploads
end
new APIRequest( new APIClient(HomeActivity.this), APP_SERVER+"/search", APIRequest.GET)
.withDefaultParams(defaultParamsHashMap())
.addParam("format", "json")
.addParam("q", searchTerm)
.addHeaderParam("Referer", APP_REFERER);
.start(new JSONRequestHandler(){
@Override
public void before(){
showProgressBar();
new APIRequest( new APIClient(HomeActivity.this), APP_SERVER+"/latest_news", APIRequest.GET)
.withDefaultParams(defaultParamsHashMap())
.addParam("format", "json")
.addParam("limit", 10)
.addHeaderParam("Referer", APP_REFERER);
.startWithCache(CacheStategy.RESPONSE_VERSION, CACHE_GROUP_KEY, CACHE_KEY,new JSONRequestHandler(){
@Override
public String getVersion(HttpEntity response){
return versionFromResponse(response);
}
new APIRequest( new APIClient(HomeActivity.this), APP_SERVER+"/download_pdf", APIRequest.GET)
.withDefaultParams(defaultParamsHashMap())
.addParam("format", "json")
.addParam("id", pdfId)
.addHeaderParam("Referer", APP_REFERER);
.startDownload(CACHE_ID, DOWNLOAD_FOLDER, new APIDataRequestHandler(){
@Override
public void before(){
showProgressBar();
// Initialization
cacheStore = new CacheStore(getContext()) {
@Override
public String storeName() {
return "ProductsStore";
}
};
// store value
cacheStore.put(TYPE, KEY, VALUE);