Skip to content

Instantly share code, notes, and snippets.

View mamantoha's full-sized avatar
🇺🇦
#StandWithUkraine

Anton Maminov mamantoha

🇺🇦
#StandWithUkraine
  • Ternopil, Ukraine
  • 08:31 (UTC +03:00)
View GitHub Profile
@mamantoha
mamantoha / is_port_open.cr
Created August 21, 2019 11:02
A quick way to find out if a given port is open with Crystal.
require "socket"
def is_port_open?(ip : String, port : Int32, timeout = 5)
s = TCPSocket.new(ip, port, dns_timeout: timeout, connect_timeout: timeout)
s.close
true
rescue IO::Timeout
false
end
@mamantoha
mamantoha / experience.rb
Last active March 14, 2024 08:21
Rails API Filtering and Sorting
# app/models/experience.rb
#
# == Schema Information
#
# Table name: experiences
#
# id :integer not null, primary key
# title :string
# description :text
# created_at :datetime not null
@mamantoha
mamantoha / set_wallpaper
Last active February 26, 2023 21:02
Set desktop wallpaper in Plasma5 via a dbus command
qdbus org.kde.plasmashell /PlasmaShell org.kde.PlasmaShell.evaluateScript 'var allDesktops = desktops(); for (i = 0; i < allDesktops.length; i++) {d = allDesktops[i]; d.wallpaperPlugin = "org.kde.image";d.currentConfigGroup = Array("Wallpaper", "org.kde.image", "General"); d.writeConfig("Image", "file:///path/to/image.png")}'
@mamantoha
mamantoha / query.rb
Created November 17, 2016 16:41
PostgreSQL: query to select records from last week on weekdays between 9:00 and 18:00
Model
.where(
"EXTRACT(dow FROM log_in) IN (1,2,3,4,5)"
)
.where(
"log_in::time BETWEEN '9:00' AND '18:00'"
)
.where(
"log_in BETWEEN now()::timestamp - (interval '1 week' AND now()::timestamp)"
)
@mamantoha
mamantoha / _.md
Last active November 23, 2021 13:10
Ruby goto
@mamantoha
mamantoha / polymorphic_many_to_many_in_rails.md
Created April 5, 2018 08:12
polymorphic_many_to_many_in_rails.md

Polymorphic many-to-many association in Rails

Migration

class CreateBannerCategories < ActiveRecord::Migration
  def change
    create_table :banner_categories do |t|
      t.integer :banner_id
      t.integer :category_id
@mamantoha
mamantoha / 01_tree.rb
Last active March 4, 2019 18:04
Implementing a clone of *nix tree in Ruby
# encoding: utf-8
# Build a Hash tree from Array Of file names
#
def build_hash_tree(filenames)
files_tree = filenames.inject({}) { |h, i| t = h; i.split("/").each { |n| t[n] ||= {}; t = t[n] }; h }
end
# Box-drawing character - https://en.wikipedia.org/wiki/Box-drawing_character
# ├ └ ─ │
@mamantoha
mamantoha / pry_everywhere.rb
Created November 13, 2018 07:56
Pry everywhere
# config/initializers/pry_everywhere.rb
require 'sidekiq'
# Perform Sidekiq jobs immediately in development,
# so you don't have to run a separate process.
# You'll also benefit from code reloading.
if Rails.env.development? && ENV['SIDEKIQ_INLINE'].present?
require 'sidekiq/testing'
Sidekiq::Testing.inline!
end
@mamantoha
mamantoha / common_prefix.rb
Created October 16, 2012 11:04
Finding the longest common prefix of an array of paths in Ruby
# Return the longest path prefix (taken character-by-character) that is a prefix of all paths in array.
# If array is empty, return the empty string ('').
# Note that this may return invalid paths because it works a character at a time.
#
def common_prefix(m)
# Given a array of pathnames, returns the longest common leading component
return '' if m.empty?
s1, s2 = m.min, m.max
s1.each_char.with_index do |c, i|
return s1[0...i] if c != s2[i]
@mamantoha
mamantoha / set_kde_wallpaper.rb
Last active April 16, 2018 12:26
Ruby script to change desktop wallpaper in Plasma > 5.7 from GoPro photo of the day
require 'net/http'
require 'open-uri'
require 'open3'
require 'json'
require 'optparse'
require 'tempfile'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: #{__FILE__} [options]"