te·los  - An ultimate object or aim.

Installing Steam for Linux on Debian 7.0 Wheezy [amd64]

I used this script to get me most of the way. I did the following:

$ dpkg --add-architecture i386
$ apt-get update
$ apt-get install ia32-libs libc6-i686 libgl1-mesa-glx
$ reboot

Example of Soap XML with Slim

Envelope

doctype xml
soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  soapenv:Header
    soapenv:Action mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none"
  soapenv:Body
    == yield

Body

upr:ProfileRetrieveReq TargetBranch=target_branch xmlns:upr="http://www.travelport.com/schema/uprofile_v13_0" xmlns:com="http://www.travelport.com/schema/common_v19_0"
  com:BillingPointOfSaleInfo OriginApplication="UAPI"
  upr:ProfileID= id
  upr:ProfileDataFilter
    upr:ProfileDataCategory= filter

Render

def default_render_locals
  {target_branch: self.class.target_branch}
end

def default_render_options
  {format: :xhtml, attr_quote: '"'}
end

def template_path(name)
  File.expand_path(File.join("templates", name), File.dirname(__FILE__))
end

def render(template_name, locals, options = default_render_options)
  Tilt.new(template_path("layout.xml.slim")).render do
    template = Tilt.new(template_path(template_name), 0, options)
    template.render(Object.new, default_render_locals.merge(locals))
  end
end

Break Time


AngularJS Mock External Services

You should (just like I should have) read the following documentation.

The $httpBackend used in production, always responds to requests with responses asynchronously. If we preserved this behavior in unit testing, we'd have to create async unit tests, which are hard to write, follow and maintain. At the same time the testing mock, can't respond synchronously because that would change the execution of the code under test. For this reason the mock $httpBackend has a flush() method, which allows the test to explicitly flush pending requests and thus preserving the async api of the backend, while allowing the test to execute synchronously.

Mock

var $httpBackend;
beforeEach(inject(function ($injector)) {
  $httpBackend = $injector.get('$httpBackend');
  $httpBackend
     .whenJSONP(/ajax.googleapis.com/)
     .respond({});  
}));

Test

it("does something...", function () {
  // do something.
  $httpBackend.flush();
  // expect something.
});

Export iTunes .m3u8 Playlist Files Script

Ruby

#!/usr/bin/env ruby
# ./export_m3u8.rb path/to/playlist.m3u8

require 'rubygems'
require 'pathname'
require 'fileutils'

if not Pathname.new(ARGV[0]).exist?
  puts "m3u8 file not found!"
  puts "usage: #{__FILE__} path/to/playlist.m3u8"
  exit(1)
end

playlist_path   = ARGV[0]
playlist_name   = File.basename(playlist_path, ".*")

folder_path     = File.join(Dir.pwd, playlist_name)
FileUtils.mkdir_p(folder_path)

IO.read(playlist_path).split("\r").each_with_index do |file_path, index|
  next unless Pathname.new(file_path).exist?

  file_name = "#{index} " + File.basename(file_path)
  puts "Copying... #{file_name}"
  FileUtils.cp(file_path, File.join(folder_path, file_name))
end

puts "Finished."

Python

#!/usr/bin/env python
# ./export_m3u8.py path/to/playlist.m3u8

import sys
import os
import shutil

if not os.path.exists(sys.argv[1]):
  print "m3u8 file not found!"
  print "usage: %s path/to/playlist.m3u8" % sys.argv[0]
  sys.exit(1)

playlist_path = sys.argv[1]
playlist_name = os.path.splitext(os.path.basename(playlist_path))[0]

folder_path   = os.path.join(os.getcwd(), playlist_name)
if not os.path.exists(folder_path):
  os.makedirs(folder_path)

playlist_file = open(playlist_path).read().split("\r")
for index, file_path in enumerate(playlist_file):
  if not os.path.exists(file_path):
    continue

  file_name = "%d %s " % (index, os.path.basename(file_path))
  print "Copying... %s" % file_name
  shutil.copy(file_path, os.path.join(folder_path, file_name))

print "Finished."

RaspberryPi 3-Step SD-Card Format for OSX

Please make sure you identify the correct disk. You can download officially supported distros here.

First

$ diskutil list                                                                                                                         [master][ruby-1.9.3-p392] 
/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *251.0 GB   disk0
   1:                        EFI                         209.7 MB   disk0s1
   2:                  Apple_HFS Macintosh HD            250.1 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
/dev/disk1
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *4.0 GB     disk1
   1:                 DOS_FAT_32 UNTITLED                4.0 GB     disk1s1

Second

$ sudo diskutil unmountDisk disk1                                                                                                        
Unmount of all volumes on disk1 was successful

Third

$ sudo dd bs=1m if=/Users/james/Downloads/2013-02-09-wheezy-raspbian.img of=/dev/disk1 

Finding A Ruby Memory Leak

The following code is helpful finding obvious changes in Ruby's ObjectSpace and tracking leaks.

object_counts = Hash.new(0)
ObjectSpace.each_object {|object| object_counts[object.class] += 1}
puts object_counts.inspect
# => [[Image, 67], [Array, 2474], [String, 59407], ...

Nelson Elhage has a great post on finding more sinister memory leaks here.


Inconsistent indentation with haml textarea

Sometimes you don’t want Haml to indent all your text. For example, tags like pre and textarea are whitespace-sensitive; indenting the text makes them render wrong. - Haml Documentation

Sinatra

set :haml, {:ugly => true}

Haml Template Filter

:preserve

Scorched Earth in Ruby

I wanted to build the simplest game with destructible terrain represented by an image bitmap. I had a lot of fun building this. You can find the code on github here.

img

Random Terrain

Texplay made it easy to draw to the terrain image.

# http://banisterfiend.wordpress.com/2008/08/23/texplay-an-image-manipulation-tool-for-ruby-and-gosu/
width, height = $window.width, $window.height
self.image ||= ::TexPlay.create_image($window, width, height)

# bounds
x1, y1  = 0, height/1.5
x2, y2  = width, height

# paint terrain
image.paint do
  # clear
  rect(0, 0, x2, y2, :fill => true, :color => Color::EMPTY)

  # rand seed
  cycles = rand(10)

  # draw
  x1 = 0.0
  while(x1 < $window.width) do
    y1 = (Math.sin(x1 / width * cycles) * 100) + height / 1.5
    line(x1, y1, x1, y2, :fill => true, :texture => Gosu::Image["terrain.png"])
    pixel(x1, y1, :color => Color::SURFACE)
    x1 += 1.0
  end
end

Angle against Terrain

It samples points underneath the player and takes the average angle.

self.angle = 270 + (angles.inject(:+) / angles.size) unless angles.size == 0

Player Colors

The Singleton ColorBank class worked well providing predefined colors to filter over the player's image.

self.color = ColorBank.instance.next

Projectile Angle

I love how simply the following code sets the projectile angle.

self.angle = Gosu.angle(x, y, x + velocity_x, y + velocity_y)

img


It's that time again.

You can find the code for my new blog using Sinatra and Sequel on github here.