This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This ( https://gist.github.com/hatarist/675dc3debf6cf5f825b5c15aed4cbac0 ) with TOAST size formatting | |
SELECT | |
table_name, | |
pg_size_pretty(table_bytes) AS table, | |
pg_size_pretty(index_bytes) AS index, | |
pg_size_pretty(toast_bytes) AS toast, | |
pg_size_pretty(total_bytes) AS total | |
FROM ( | |
SELECT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'cgi' | |
require 'net/http' | |
require 'json' | |
abort("songlink <url>") if ARGV[0].nil? | |
def copy_to_clipboard(text) = IO.popen('pbcopy', 'w') { |clipboard| clipboard.puts text } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
URI(url).tap {|u| u.query = URI.encode_www_form(CGI.parse(u.query || "").merge({ref: 123})) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Make add tags to the normal rails logger, accessed like `hc_logger.info("my log message")` | |
def hc_logger = @hc_logger ||= logger.tagged("HttpClients").tagged(self.class.name) | |
# Create a new logger as an instance variable for a class: | |
class MyServiceClass | |
# .... | |
def my_logger = @my_logger ||= create_logger.tagged(self.class.name) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bot_shedding(wait: 600, threshold: 1) | |
require "sys/cpu" | |
require "etc" | |
# normalize the 1-minute load average based on processor count | |
return unless Sys::CPU.load_avg[0] / Etc.nprocessors > threshold | |
return unless DeviceDetector.new(request.user_agent).bot? | |
logger.info("Shedding bots: Returning 503 / Retry-After: #{wait} for #{request.remote_ip} / #{request.user_agent}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/\b(?:97[89]-?)?(?:\d{1,5}-?)?(?:\d{1,7}-?)?(?:\d{1,6}-?)?\d{1,3}\b/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This version works with files and urls. For URLs, it only downloads the chunks | |
# of the file neccessary to calculate the hash. | |
# | |
# Author: Dan Milne | |
require "net/http" | |
require "uri" | |
module Moviehash | |
class Error < StandardError; end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'json' | |
gem 'sys-filesystem' | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def valid_json?(string) | |
result = JSON.parse(string) | |
result.is_a?(Hash) || result.is_a?(Array) | |
rescue JSON::ParserError, TypeError | |
false | |
end | |
def valid_json(string, default: []) | |
result = JSON.parse(string) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'open-uri' | |
require 'nokogiri' | |
VERSION = 0.1 | |
def getter(url) | |
URI.open(url, "User-Agent" => "Curlr/#{VERSION}") | |
rescue OpenURI::HTTPError => e | |
e.io |