Skip to content

Instantly share code, notes, and snippets.

View dannguyen's full-sized avatar
💭
havin a normal one

Dan Nguyen dannguyen

💭
havin a normal one
View GitHub Profile
@dannguyen
dannguyen / loofah-and-nokogiri-whitespace.rb
Created February 14, 2014 00:24
How to use the Loofah library with a Nokogiri node to preserve line breaks between block elements
npage = Nokogiri::HTML(html)
h[:text] = Loofah.fragment(npage.search('.displaytext').to_html).to_text
@dannguyen
dannguyen / ruby-wrap-graphicsmagick-blue.rb
Created February 18, 2014 22:15
Ruby invoking graphicsmagick to make images blue because I'm too lazy to learn shell loops
Sector.all.each do |sector|
files = Dir.glob("./**/skift/#{sector.slug}.png").reject{|f| f =~ /-blue\.\w{3,4}$/}
files.each do |fname|
cmd = "convert #{fname} -fill '#2b74a5' +opaque darkblue #{fname}"
puts cmd
`#{cmd}`
end
end
@dannguyen
dannguyen / sublime.text.user-settings.json
Last active August 29, 2015 13:57
Setting the defaults for my Sublime Text 3
{
"always_show_minimap_viewport": true,
"auto_complete_commit_on_tab": true,
"auto_find_in_selection": true,
"binary_file_patterns":
[
"*.log",
"*.jpg",
"*.jpeg",
"*.png",
@dannguyen
dannguyen / sublime-text-3.keys.move-character.json
Created March 16, 2014 04:00
Sublime Text 3 keyboard shortcut: Mapping Shift-Tab to move outside a right closing bracket/braces/quote-mark
[
{
"keys": ["shift+tab"], "command": "move", "args": {"by": "characters", "forward": true}
}
]
@dannguyen
dannguyen / value-lengths-dist-for-field.sql
Last active August 29, 2015 13:57
find distribution of value lengths for a given field
SET @field_name = "place_of_service";
SET @tablename = "payments";
SET @query = CONCAT('SELECT ', @field_name, ', count(1), length(', @field_name, ') AS lt FROM ', @tablename,' GROUP BY ', @field_name, ' ORDER BY lt desc');
PREPARE stmt FROM @query;
EXECUTE stmt;
@dannguyen
dannguyen / minimal-rspec-setup.rb
Created April 3, 2014 16:51
Minimal number of files to run `rspec` from command-line in your folder
# Needed files:
# - Rakefile
# - Gemfile
# - spec/spec_helper
#####
## Gemfile
source 'https://rubygems.org'
gem 'rspec'
stores.each do |store|
if country == store[:name]
store_id = store[:id]
break
end
end
# http://ruby-doc.org/core-1.9.3/Enumerable.html
### With #select
@dannguyen
dannguyen / google-analytics-for-wordpress-sql-join.sql
Last active August 29, 2015 13:59
connecting wordpress to google analytics in MySQL
SELECT
`wp_terms`.`slug` AS wordpress_category,
COUNT(1) AS post_count,
SUM(`google_stats`.`Visits`) AS total_visits,
SUM(`google_stats`.`New Visits`) AS total_new_visits,
ROUND((SUM(`google_stats`.`Visits`) / COUNT(1)), 1) AS visits_per_post
FROM `wp_posts`
INNER JOIN `wp_term_relationships`
@dannguyen
dannguyen / make-andpdftk.sh
Last active August 29, 2015 14:01
This is just a quickie write-up of how to de-secure PDF files using ghostscript from the command line. There's some setup code to simulate a bunch of locked PDFs.
# First
# http://unix.stackexchange.com/questions/46948/how-to-make-duplicates-with-different-names-from-a-single-file
mkdir copies
newnames=("a" "b" "c" "d" "e")
orgfile=my_original_doc.pdf
for x in "${newnames[@]}"; do
cp $orgfile "copies/$x.pdf"
done
@dannguyen
dannguyen / rails4-ruby-2-cap3-setup.md
Last active May 18, 2016 12:29
Rails 4, Ruby 2, Capistrano 3