This file contains 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 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 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 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 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 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
# https://trac.opensubtitles.org/projects/opensubtitles/wiki/HashSourceCodes#Ruby | |
# https://github.com/trytriangles/oshash/tree/master/test | |
def oshash(filename) | |
mask = 0xffffffffffffffff | |
readsize = 64 * 1024 | |
filesize = File.size(filename) | |
hash = filesize | |
File.open(filename, 'rb') do |f| | |
hash = f.read(readsize).unpack("Q<*").inject(hash) {|h, v| h + v & mask} |
This file contains 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 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 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 |
This file contains 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
# https://stackoverflow.com/questions/1078347/rails-is-there-a-rails-trick-to-adding-commas-to-large-numbers | |
an_integer.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse | |
an_int_or_float.to_s.split(".").then {|a| a[0] = a[0]&.reverse&.gsub(/(\d{3})(?=\d)/, '\\1,')&.reverse; a[1].nil? ? a[0] : a[0] + '.' + a[1] } |