Time Wasters

Because you can’t be productive all the time


Compress Rails project images

We were having some issues with slow loading times on our site, so I set out to compress all of our uploaded images.  Since we have thousands of images doing it by hand is not really an option, and plenty of the images are transparent PNGs.  So I enlisted the help of TinyPNG.

TinyPNG is an online application that compresses both PNG and JPG images, and preserves the alpha layer for PNG images, and it offers an API along with a ruby gem.  So you can use it to automate compressing images.  First start by installing the gem, and I also use the fastimage gem for this script so if you don’t have it install that too.

1
2
3
gem install tinify
gem install fastimage
rbenv rehash (if you're using rbenv)

Now you just need the script below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
require "tinify"
require 'fastimage'
Tinify.key = "YOUR API KEY"
 
i = 0
Dir.glob("public/system/rich/rich_files/rich_files/**/*.{jpg,png}") do |item|
  #only do 50 images at a time
  break if i == 10
 
  #ignorre directories and original files
  next if item == '.' or item == '..' or item.include? "original"
 
  #ignore iamges less than 50KB
  filesize = (File.size(item) * 0.001).floor
  next if filesize < 50
 
  #ignore images that are smaller thatn 100KB and over 600px large
  size = (FastImage.size(item).inject(:+))
  next if size >= 600 and filesize < 100
  i = i + 1
 
  source = Tinify.from_file(item)
  source.to_file(item)
 
  new_filesize = (File.size(item) * 0.001).floor
 
  puts "Converted: #{item}"
  puts "Original: #{filesize} Optimized #{new_filesize}"
end
 
puts "All done!"

Some things you’ll need to address to get this working are first, swap out “YOUR API KEY” with your actual API key, you can get one here. And if you’re not using the rich gem to handle image uploads you’ll need to change “public/system/rich/rich_files/rich_files” to whatever the location of your images is. The “**/*” part of the path will search all sub-directories and files in those sub-directories, and “.{jpg,png}” will only search jpg and png images.

The code will go through all the JPG and PNG files in your images directory, it will ignore all original versions, and then ignore files that are too small to bother with and then print out the files that were compressed along with their original and optimized sizes, then print out “All done!” once it’s finished.

Save it as tinify.rb and you can run it in the console with:

1
ruby tinify.rb

This will process 50 images at a time, and ignore images that I’ve set as too low to both with, so all images under 50KB are ignored, and images over 600x600px and lower than 100KB are ignored.  But this does pose a problem – images that aren’t reduced enough will still get re-reduced if you run the script multiple times.  For now I’m just monitoring it, and I’ll adjust it if too many images get caught.

Permalink » No comments

AJAX errors explained

I’ve recently finished adding Google Analytics to a mobile App I’ve been working on for some time, and it’s been returning quite a few different error codes from the API.  So, I’m keeping track of them here along with their causes – to make debugging faster and easier in the future.

Status: 406 / Error: Not Acceptable / Error Thrown: Not Acceptable

  1. A server side issue with the response
  2. The session on the server timed out

Status: 0 / Error: error

  1. user was unable to reach the server (most likely connection issue)

Status: 200 / Error: OK / Error Thrown: SyntaxError: Unexpected end of input

  1. Returned JSON was cut off (possibly connection issue)
  2. No JSON whatsoever was returned

Status: 0 / Error: timeout / Error Thrown: timeout

  1. duh, timeout!

Status: 200 / Error: OK / Error Thrown: SyntaxError: Unexpected token

  1. Have HTML returning instead of JSON
  2. Malformed JSON

Status: 401 / Error: Unauthorized / Error Thrown: Unauthorized

  1. User tried to login with wrong username and password

Permalink » No comments

Fats vs Carbs

This is something of a continuation of my “What’s in a calorie?”  article.  Where I dumied down good/bad carbs and fats and looked into why protein is the best thing since sliced bread (which is actually horrible for you).  So I want to continue my oversimplification to carbs vs fats.  There are many many more factors, but I’m trying to pieces things together so with nothing more then a nutrition facts label I can get an educated guess on exactly how good or bad that food is.

After a touch of research, here is my “ranking” of specific macro nutrients:

  1. Protein
  2. Good Fats
  3. Complex Carbs
  4. Saturated Fats
  5. Simple Carbs
  6. Trans Fats

Pretty much every article I looked at described Trans Fat as “the devil” so I think that’s pretty good on the bottom.  Current diet trends are all about Fats > Carbs so I have complex carbs under good fats.  Though, complex carbs/saturated fats seem like a grey area and could easily go the other way.  I’d really like to find something that compares the two in more depth.

This is still a work in progress, but I’m getting the ball rolling.

This time around I got introduced to the glycemic index which is another way to rank carbs. And I already know that not all sugars are created equal, but it seems like the only way to tell the difference is to memorize ingredients.  I don’t have time in my life to give everything such a thorough inspection, but I do have thoughts about putting together a program to assign points to foods based on their nutrition facts to make the process much more approachable.  Pretty much just like fooducate – but with an automatic instantaneous result.

Sources:

http://www.acaloriecounter.com/diet/saturated-fat-trans-fat/
Trans fat is the worst, sat fat not as bad

http://www.olsonnd.com/which-is-worse-fat-or-sugar/
Sugars/simple carbs are worse then sat fat, but better then trans fat

http://www.nytimes.com/2014/09/02/health/low-carb-vs-low-fat-diet.html
Good carbs are worse then good fats

Permalink » No comments

Server side scripting statistics 2015

I’m continuing my study on popularity of different server side languages, and made a few adjustments this year I’ll go over.  This is the exact searches I’m placing on the different job sites:

Keywords: “Ruby on Rails”, “PHP”, “ASP.NET”, “Python”, “Perl”
Location: “San Francisco, CA”
Search Radius: within 30 miles
Posting date: within 30 days

Paying attention to the search radius and posting date are new here, these are the defaults for the different sites:

Monster: defaults to 20 miles with no time limit
Career Builder: defaults to 30 miles within 30 days
Craigslist: doesn’t offer an advanced search so we can’t normalize that
Dice: defaults to 30 miles within 30 days

Both Career Builder and Dice default to 30 miles in 30 days, so I went with that. And there’s nothing I can do to specify the craigslist results, so really only Monster’s numbers will change here.  I did search Monster using the default settings, and the are those numbers:

Default criteria Monster numbers: Ruby 73, PHP 76, ASP 8, Python 135, Perl 9

The numbers are a bit different, but not significantly so.  So, I’m not going to include them in the table, and I don’t believe that’s a significant enough change to skew the results from the numbers from previous years.

One additional thing to note it that I no longer need to omit the . from ASP.NET on craigslist, though doing so didn’t change the number of results.

And, as a note to myself – I found a site that will calculate the totals and percentages for me, which saved a lot of time.

SiteRoRPHPASP.NETPythonPerl
2008 total71 (5%)860 (69%)309 (24%)
2010 Total193 (18%)720 (67%)158 (14%)
2011 Total (2273)374 (16%)1508 (66%)391 (17%)
2012 Total (2085)402 (19%)1396 (67%)287 (14%)
2013 Total (1889)396 (21%)1109 (59%)384 (18%)
2014 Total (2003/5291)412 (21%/8%)1262 (63%/24%)329 (16%/6%)2167 (41%)1121 (21%)
2015
Monster.com (364)64 (17.6%)111 (30.5%)9 (2.5%)175 (48%)5 (1.4%)
Career Builder (346)25 (7.2%)59 (17%)30 (8.7%)154 (44.5%)78 (22.5%)
Craigslist (1281)61 (4.8%)624 (48.7%)44 (3.4%)399 (31%)153 (11.9%)
Dice (1833)425 (23.2%)234 (12.8%)99 (5.4%)756 (41.2%)319 (17.4%)
Total (3824)575 (15%)1028 (26.9%)182 (4.7%)1484 (38.8%)555 (14.5%)

Ruby is higher then it’s ever been, while PHP has gone down a bit.  ASP.NET and Perl took a huge dive – they’re roughly half what they were.  This is the second lowest I’ve seen ASP.  I only have last years number for Perl, so I can’t really deduce too much from this change.

In general over the years it looks like Ruby is gaining popularity, while other languages are just bouncing around.  This suggests to me that the newer Ruby is still finding it’s place, while the other languages are more settled in their positions in the marketplace.

I included totals on the job sites this year too, Dice ended up with the most – which isn’t surprising since it specializes in tech jobs.  With Craigslist right behind – which also isn’t very surprising since it’s the cheapest to list out of all the job sites here.

Permalink » No comments