Skip to content

Instantly share code, notes, and snippets.

nhm tanveer hossain khan (hasan) we4tech

View GitHub Profile
@we4tech
we4tech / exp_factory_method.rb
Created Sep 21, 2013
Instead of relying on 3rd party service, we could wrap them in bridge or expose through factory or facade
View exp_factory_method.rb
def handle_action_created(action)
if action.from.kind_of?(Source)
$redis.lpush(
"#{self.class.name}:#{self.id}",
[action.from.node.id, action.from.node.neo_id].join(":")
)
end
end
# Perhaps we could do
@we4tech
we4tech / american_style.rb
Created Sep 21, 2013
Pasta in ruby mixin
View american_style.rb
module AmericanStyle
extend ActiveSupport::Concern
included do
has_many :american_ingredients
end
module ClassMethods
def macaroni_cheese; ... end
end
@we4tech
we4tech / after_refactor_order_service.rb
Last active Dec 21, 2015
Just an example how to refactor and separate code and their concerns.
View after_refactor_order_service.rb
class OrderService
class << self
def find_or_create_purchase_for_order(order, credit_card_id, shipping_fee = 0.0)
user, vendor = order.buyer, order.seller
purchase = Purchase.find_by_user_id_and_order_id(user.id, order.id)
return purchase if purchase.present?
create_purchase order, credit_card_id, shipping_fee
end
@we4tech
we4tech / twitter_client_patch.rb
Created May 31, 2013
Twitter Client with control loop, If any too many requests error raised. It'll sleep for a certain time period and will wake up and continue from last batch. This is an addition to Twitter::Client 'gem twitter'
View twitter_client_patch.rb
# Twitter Client with control loop, If any too many requests error raised.
# It'll sleep for a certain time period and will wake up and continue from last batch.
#
# This is an addition to Twitter::Client 'gem twitter'
module BeatDeckMachine::Scrapers::OAuth::TwitterClientPatch
MAX_ATTEMPTS = 20
extend ActiveSupport::Concern
@we4tech
we4tech / thread_pool.rb
Last active Dec 16, 2015
Thread pool, and separate mongo client with each thread.
View thread_pool.rb
class ThreadPool
attr_accessor :size, :pool
def logger
BeatDeckMachine::logger
end
def initialize(size)
@size = size
@jobs = Queue.new
@we4tech
we4tech / db.bash
Created Jan 24, 2013
Simple bash script to quickly import an existing schema to a fixed database or get quick access to mysql console.
View db.bash
#!/bin/bash
echo "---------------------------------------"
echo " Welcome to Quick Schema Importer "
echo "---------------------------------------"
ROOT=$PWD
CONF=""
@we4tech
we4tech / export-mongo-collections.rb
Last active Dec 11, 2015
Export mongodb collections and their properties to HTML format
View export-mongo-collections.rb
# Export collection name and their properties list in HTML format.
require 'mongo'
include Mongo
# Connect with mongodb server
client = MongoClient.new('localhost', 27017)
# Load database
@we4tech
we4tech / pre-push
Created Nov 9, 2015
GIT Pre push script to prevent from accidental force push to master branch
View pre-push
#!/usr/bin/env ruby
# Make sure you have 'colorize' gem installed already.
require 'colorize'
current_branch = `git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,'`
push_cmd = `ps -ocommand | grep "git push"`
if current_branch =~ /\/master/ && push_cmd =~ /(\-f|force|delete)/
puts '*********************************************'.colorize(:red)
@we4tech
we4tech / lambda-example.rb
Created Jul 24, 2012
Ruby closure lambda example
View lambda-example.rb
l = lambda { |a, b| ... }
l.call('A')
#=> Error need to pass 2 arguments
@we4tech
we4tech / lambda-proc-diff.rb
Created Jul 24, 2012
ruby closure lambda proc difference
View lambda-proc-diff.rb
def whats_your_name?
l = Proc.new { return "Karim" }
l.call
return "Rahim"
end
#=> "Karim"
You can’t perform that action at this time.