Skip to content

Instantly share code, notes, and snippets.

View lappi-lynx's full-sized avatar
🚴
Cycling from home

Stanislav Kniazev 🇺🇦 lappi-lynx

🚴
Cycling from home
View GitHub Profile
@lappi-lynx
lappi-lynx / async_puzzle.js
Created March 20, 2024 21:54
js/node async nuances
const { promisify } = require('util');
const sleep = promisify(setTimeout);
async function bar(n, s, t) {
setImmediate(() => process.stdout.write(s));
await sleep(n);
return t;
}
async function foo() {
@lappi-lynx
lappi-lynx / 4_2_quartz.rb
Last active March 14, 2024 19:56
4.2 Quartz aalto course
# config.ru
class SimpleApp
def self.call(env)
request = Rack::Request.new(env)
case request.path_info
when '/'
latency = Random.rand(0.05..5)
p "I'm lazy, my laziness is #{latency} seconds..."
sleep(latency)
[200, {"content-type" => "text/plain"}, ["Hello, World! Server latency is #{latency} s"]]
@lappi-lynx
lappi-lynx / redis_cache_module.rb
Created February 6, 2024 13:15
Simple redis caching module
require 'redis'
module RedisStore
EXPIRATION_TIME_SECONDS = 1 * 24 * 60 * 60 # 1 day
def redis
@redis ||= Redis.new(host: 'localhost', port: 6379)
end
def set_cache(id, val)
@lappi-lynx
lappi-lynx / Just_read_light_theme.css
Last active January 9, 2019 17:54
Just_read_light_theme
@import url("https://fonts.googleapis.com/css?family=Open+Sans");
body { font-family: 'Iowan Old Style', serif; background-color: rgb(94, 133, 172); line-height: 1.6; font-size: 17px; color: rgb(79, 50, 28); text-rendering: optimizelegibility; }
h1, h2 { font-family: 'Iowan Old Style', sans-serif; font-weight: 700; }
* { box-sizing: border-box; }
body { font-family: 'Iowan Old Style', sans-serif; font-style: normal; font-variant: normal; font-weight: 400; line-height: 1.85em; font-size: 18px; background-color: rgb(230, 218, 201); color: rgb(79, 50, 28); }
code { color: #ccc; background-color: rgb(34, 34, 34); }
pre code { background-color: transparent; }
pre { background-color: rgb(85, 85, 85); padding: 15px; }
.simple-container { max-width: 950px; margin: 0px auto; padding-top: 70px; padding-bottom: 20px; padding-left: 25px; padding-right: 25px; box-shadow: 0px 6px 12px 3px #555; background-color: rgb(248, 241, 227); }
p { color:rgb(79, 50, 28); font-size: 16px; }
@lappi-lynx
lappi-lynx / Just_read_dark_theme.css
Last active November 13, 2018 17:32
Just Read dark_theme (chrome extension for reading)
@import url("https://fonts.googleapis.com/css?family=Open+Sans");
body { font-family: 'Open Sans', serif; background-color: rgb(94, 133, 172); line-height: 1.6; font-size: 17px; color: rgb(255, 240, 240); text-rendering: optimizelegibility; }
h1, h2 { font-family: "Source Sans Pro", sans-serif; font-weight: 700; }
* {
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
body {
font-family: 'Open Sans', serif;
@lappi-lynx
lappi-lynx / git_log_tree.bash
Created September 20, 2017 08:45
git log tree
One options:
$ git config --global alias.grog 'log --graph --abbrev-commit --decorate --all
--format=format:"%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(dim white) - %an%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n %C(white)%s%C(reset)"'
Second One:
$ git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%C(bold blue)<%an>%Creset' --abbrev-commit"
@lappi-lynx
lappi-lynx / prawn_io_image.rb
Created March 3, 2016 16:20
Prawn PDF images with Base64 and StringIO
def render
pdf = Prawn::Document
pdf.image StringIO.new(Base64.decode64(splitBase64(BASE64_IMAGE_GOES_HERE)[:data])), at: [10, cursor - 50], width: 200, height: 125
end
def splitBase64(uri)
if uri.match(%r{^data:(.*?);(.*?),(.*)$})
return {
type: $1, # "image/png"
encoder: $2, # "base64"
@lappi-lynx
lappi-lynx / array_to_relation.rb
Created January 29, 2016 12:20
Rails: Convert array into ActiveRecord Relation object
scope :with_scope, -> { where(id: ARRAY_COLLECTION.map(&:id)) }
@lappi-lynx
lappi-lynx / prawn_multi_column.rb
Last active January 29, 2016 12:15
Multi-column PDF generating with Prawn in ruby
# Not breaking the page but continue on the next column
# Example of 2 columns table with some styling
def render_left_products_list
column_box([-5, cursor], columns: 2, height: cursor, position: :left, width: bounds.width, spacer: 40) do
@categories.each do |category|
move_down 6
table line_item_rows(category), position: :left, cell_style: { size: 7, text_color: "000000", borders: [:bottom], border_lines: [:dotted], padding: [2, 0, 4, 0] } do
column(1).style align: :right
style(row(0), text_color: 'FF0000')
@lappi-lynx
lappi-lynx / sql_to_csv.rb
Last active January 29, 2016 12:16
Allows you to convert data from SQL into CSV format
# First connect to DB with needed sql data
class T < ActiveRecord::Base
establish_connection({adapter:'mysql2', database:'db_for_test', encoding:'utf8',user:'', password: ''})
set_table_name 'utility_company'
end
CSV.open("data.csv", "wb") do |csv|
csv << T.first.attribute_names # adds the attributes name on the first line
T.all.each do |hash|