Skip to content

Instantly share code, notes, and snippets.

@eladmeidar
eladmeidar / cached_method.rb
Created November 22, 2020 12:36
Caching a method in Rails
module CachedMethod
def self.included(base)
base.class_eval do
@method_cache_validators = Hash.new(0)
extend ClassMethods
end
end
module ClassMethods
@eladmeidar
eladmeidar / fetch_logos.rb
Created November 8, 2020 06:34
Simple ruby script to fetch possible logos from a domain.
# Usage: ruby fetch_logos.rb <FULL_DOMAIN>
require 'httparty'
require 'nokogiri'
require 'css_parser'
require 'byebug'
class Array
def self.wrap(object)
if !object.is_a?(Array)
let isRunning = false
const foo = function() {
isRunning = true
console.log('called')
isRunning = false
}
const events = ['click', 'mousedown']
let func = function(eventsArr, cb) {
@eladmeidar
eladmeidar / widget_inject.rb
Last active January 30, 2019 15:09
Coding exercise
class String
module WidgetExceptions
class InvalidWidget < ArgumentError; end
end
def widget_inject(widgets = [])
self_with_widgets = self.clone
widgets.sort_by {|w| w[:position] }.reverse.each do |widget|
raise WidgetExceptions::InvalidWidget, "widget must be a hash, was #{widget.class.name}" unless widget.is_a?(Hash)
check process thin-3000 with pidfile /var/www/html/redmine2/current/tmp/pids/thin.3000.pid
start program = "cd /var/www/html/redmine2/current && /usr/local/rvm/bin/rvm default do bundle exec thin start -C /var/www/html/redmine2/current/config/thin/performance.yml"
stop program = "cd /var/www/html/redmine2/current && /usr/local/rvm/bin/rvm default do bundle exec thin stop -C /var/www/html/redmine2/current/config/thin/performance.yml"
if 3 restarts within 5 cycles then timeout
if totalmem is greater than 400.0 MB for 2 cycles then restart
group thin
check process thin-3000 with pidfile /var/www/html/redmine2/current/tmp/pids/thin.3000.pid
start program = "cd /var/www/html/redmine2/current && /usr/local/rvm/bin/rvm default do bundle exec thin start -C /var/www/html/redmine2/current/config/thin/performance.yml"
stop program = "cd /var/www/html/redmine2/current && /usr/local/rvm/bin/rvm default do bundle exec thin stop -C /var/www/html/redmine2/current/config/thin/performance.yml"
if 3 restarts within 5 cycles then timeout
if totalmem is greater than 400.0 MB for 2 cycles then restart
group thin
class FavoritesManager
attr_reader :user_id
def initialize(user_id)
@user_id = user_id
@client = REDIS
end
def favorite_post(post)
@eladmeidar
eladmeidar / PostsController.rb
Created September 1, 2015 12:23
perfect controller
class PostsController < ApplicationController
before_action :find_post, only: [:show, :edit, :destroy]
skip_before_action :verify_authenticity_token, only: [:create, :update, :destroy]
respond_to :html, :json, :xml
def index
@posts = Post.all
respond_with(@posts)
@eladmeidar
eladmeidar / client.rb
Last active August 29, 2015 14:16
Chat Client
require "socket"
class Client
def initialize(host, port)
@server = TCPSocket.open( host, port )
@request = nil
@response = nil
listen
send
@request.join
@eladmeidar
eladmeidar / array.rb
Created March 2, 2015 16:13
Check if all array keys are in hash keys
def do_we_have?(hash = {}, keys = [])
hash_keys = hash.keys.map {|key| key.to_s }
found_all = true
keys.each do |key|
unless hash_keys.include?(key)
found_all = false
end
end
return found_all
end