Skip to content

Instantly share code, notes, and snippets.

@RobertAudi
RobertAudi / 01-safe-download.rb
Created May 4, 2021 08:06 — forked from janko/01-safe-download.rb
A safe way in Ruby to download a file to disk using open-uri (with/without comments)
require "open-uri"
require "net/http"
Error = Class.new(StandardError)
DOWNLOAD_ERRORS = [
SocketError,
OpenURI::HTTPError,
RuntimeError,
URI::InvalidURIError,
@RobertAudi
RobertAudi / Int+Extenstion.swift
Last active May 2, 2020 12:07 — forked from gbitaudeau/Int+Extenstion.swift
Convert large numbers to smaller format
import Foundation
extension Int {
func abbreviate() -> String {
typealias Abbrevation = (threshold: Double, divisor: Double, suffix: String)
let abbreviations: [Abbrevation] = [
(0, 1, ""),
(1000.0, 1000.0, "K"),
(100_000.0, 1_000_000.0, "M"),
@RobertAudi
RobertAudi / example.rb
Last active March 23, 2020 14:53 — forked from redconfetti/example.rb
Custom Rspec matcher for checking class for constant, with optional class type.
# frozen_string_literal: true
class ExampleClass
PI = 3.14159265359
end
# frozen_string_literal: true
RSpec::Matchers.alias_matcher :increment, :change do |desc|
desc.gsub("changed", "incremented").gsub("change", "increment")
end
RSpec::Matchers.alias_matcher :decrement, :change do |desc|
desc.gsub("changed", "decremented").gsub("change", "decrement").gsub("-", "")
end
@RobertAudi
RobertAudi / attribute_spec_helper.rb
Last active May 6, 2019 08:38 — forked from PareshGupta/attribute_spec_helper.rb
Rspec matcher for attr_accessor, attr_reader and attr_writer
# frozen_string_literal: true
# `have_attr_reader` matcher for attr_reader
RSpec::Matchers.define :have_attr_reader do |field|
match do |object_instance|
object_instance.respond_to?(field)
end
failure_message do |object_instance|
"expected attr_reader for #{field} on #{object_instance}"
@RobertAudi
RobertAudi / chart_colors.txt
Last active October 25, 2021 14:20 — forked from there4/chart_colors.txt
[CSS] Chart Color Collection
#3366CC
#DC3912
#FF9900
#109618
#990099
#3B3EAC
#0099C6
#DD4477
#66AA00
#B82E2E
@RobertAudi
RobertAudi / inspector.rb
Created September 21, 2016 12:20 — forked from ubermajestix/inspector.rb
Override inspect on ruby objects
# When an RSpec test like this fails,
#
# @my_array.should == [@some_model, @some_model2]
#
# RSpec will call inspect on each of the objects to "help" you figure out
# what went wrong. Well, inspect will usually dump a TON OF SHIT and make trying
# to figure out why `@my_array` is not made up of `@some_model` and `@some_model2`.
#
# This little module and technique helps get around that. It will redefine `inspect`
# if you include it in your model object.
#
# Generate a unique token before the creating a
# new record. Option to specify an array of
# attributes as predicates for the uniqueness check.
# The tokenable attribute is always appended to
# the list of predicates.
#
# Example usage in a model:
#
# class Contribution < ActiveRecord::Base
export ORIG_GEM_PATH=${GEM_PATH:-}
export ORIG_GEM_HOME=${GEM_HOME:-}
export ORIG_PATH=${PATH}
omg() {
if [[ "$1" =~ "^h(elp)?\$" ]]; then
echo "Usage: omg [h|help|r|reset]"
elif [[ "$1" =~ "^r(eset)?\$" ]]; then
if [[ -z "$ORIG_GEM_HOME" ]]; then
unset GEM_HOME
class String
def humanize
self.split(/[-_]/).map {|w| w.capitalize }.join(' ')
end
end