Skip to content

Instantly share code, notes, and snippets.

Blair Anderson blairanderson

View GitHub Profile
@blairanderson
blairanderson / downloader.rb
Created Jan 28, 2019
ActiveStorage Downloader takes attachment input and yields a tempfile
View downloader.rb
# frozen_string_literal: true
module ActiveStorage
class Downloader #:nodoc:
def initialize(blob, tempdir: nil)
@blob = blob
@tempdir = tempdir
end
def download_blob_to_tempfile
@blairanderson
blairanderson / ruby_module_with_class_instance.rb
Created Nov 13, 2018
Ruby Module with Class and Instance methods extended
View ruby_module_with_class_instance.rb
module Persistence
def self.included(klass)
klass.extend(ClassMethods)
end
module ClassMethods
def all
puts 'all'
end
@blairanderson
blairanderson / fromPdfToJpg
Created Jul 16, 2018 — forked from dvcama/fromPdfToJpg
bash: extract high-res images from pdf using imagemagick (recursively)
View fromPdfToJpg
#!/bin/bash
# extract jpg from PDF
# based on a script edited by Purch
#####################################
if [ -z $1 ];then echo Give target directory; exit 0;fi
find "$1" -depth -name '*.pdf' | while read file ; do
directory=$(dirname "$file")
oldfilename=$(basename "$file")
@blairanderson
blairanderson / wysiwyg.md
Last active Feb 27, 2018
Easiest WYSIWYG implementation I have found. 2018 Edition
View wysiwyg.md

I found it very annoying how quickly these WYSIWYG editors go out of date.

The open sources ones have mostly rotted away, and the others are paid.

<% content_for :head do %>
  <%= stylesheet_link_tag('https://cdnjs.cloudflare.com/ajax/libs/Trumbowyg/2.9.4/ui/trumbowyg.css') %>
<% end %>
@blairanderson
blairanderson / convert-pdf-to-png.sh
Last active Jan 6, 2018
converting PDF to a png
View convert-pdf-to-png.sh
// for single item
convert -density 150 -antialias "input_file_name.pdf" -append -resize 1024x -quality 100 "output_file_name.png"
// for bulk
mogrify -format png -alpha off -density 150 -quality 100 -resize 1024 *.pdf
@blairanderson
blairanderson / raise-arg-error.md
Created Dec 21, 2017
Ruby Argument Error with Custom Message
View raise-arg-error.md

If you want to raise an argument error with a clearer message

def process_thing(account: nil, thing: nil)
  raise(ArgumentError.new("processing requires an account")) if account.blank?
  raise(ArgumentError.new("Must have a Thing to process")) if thing.blank?
  # ... Do the actual work
end
@blairanderson
blairanderson / jekyll.html
Last active Sep 27, 2019
Fullscreen Background Video Slideshow on iOS devices - note currently uses jquery :)
View jekyll.html
{% for video in site.static_files %}
{% if video.path contains 'img/videos' %}
<video muted playsinline>
<source src="{{ site.baseurl }}{{ video.path }}" type="video/mp4">
</video>
{% endif %}
{% endfor %}
@blairanderson
blairanderson / conv.sh
Last active Sep 11, 2019
Optimize Videos for Web - Compress MP4 and remove Audio with FFMPEG. encodes as 264 with CRF 30, scales down to 1920x1080, strips audio
View conv.sh
#! /bin/bash
# The Purpose of this Script is to batch convert and compress any video file to mp4 format
#
# WARNING: LOSSY COMPRESSION !!!
# Variable used:
# sourcedir is the directory where to be converted videos are. Converted video will be saved in the same folder
# usage:
@blairanderson
blairanderson / image-to-multiple-sizes.md
Last active Oct 17, 2017
imagemagick single image to multiple sizes
View image-to-multiple-sizes.md
convert input.png \
\( -clone 0 -resize 128x128 -write icon-128.png \) \
\( -clone 0 -resize 96x96 -write icon-96.png \) \
\( -clone 0 -resize 48x48 -write icon-48.png \) \
\( -clone 0 -resize 16x16 -write icon-16.png \)
@blairanderson
blairanderson / bookmarklet.js
Created Sep 26, 2017
basic javascript bookmarklet
View bookmarklet.js
javascript: var d = document,
w = window,
e = w.getSelection,
k = d.getSelection,
x = d.selection,
s = e ? e() : k ? k() : x ? x.createRange().text : 0,
f = 'http://awesomewebsite.com/bookmarklet/iframe',
l = d.location,
e = encodeURIComponent,
u = f + '?u=' + e(l.href) + '&t=' + e(d.title) + '&s=' + e(s) + '&v=4';
You can’t perform that action at this time.