Category Archives: ruby

Using Ruby for integer format conversions

Here are some recipes for interpreting stream of bytes as different C types. I will keep adding more to this as I go…
Convert a byte stream (embedded in a string) into 1 byte signed integers:
“\xfc\xfd\xfe\xff”.unpack(”c*”)
=> [-4, -3, -2, -1]
Convert a byte stream (embedded in a string) into 1 byte unsigned integers:
“\xfc\xfd\xfe\xff”.unpack(”C*”)
=> [252, 253, 254, 255]

Fixing the geocoder gem.

I wanted to use the ruby geocoder library on the windows machine, but the installation of the gem failed due to some weird error. I checked the rubyforge project page to see if someone else had a similar problems and someone actually had, but the bug was open for a long time. I decided to [...]

More about Phishtank API

Here is what will be good-to-have from phishtank.com API:

Good documentation about each interface e.g. how is callback_url used by auth.frob.request API ?
Description of all possible fields in return response (all possible XML elements and their possible values)
Some test URL’s and emails which will return known responses (i.e. phishy URL, good URL, not in the database [...]

Phish Tank

http://www.phishtank.com is a new service which aims to help weed out phishing URLs and email addresses using wisdom of the crowds. Users can submit emails/URLs which they suspect of fraud and others can vote if they really are fraudulent or not. I think it is a great concept. There is a REST API using which [...]

Ruby script to get a list of all mp3 files in a directory sorted durationwise.

Here is some ruby practice…

def mp3len(dirname)
require ‘mp3info’
summary=[]
Dir[dirname].each do |f|
Mp3Info.open(f) do |info|
summary.push([f,info.length])
end
end
summary
end
puts “Enter directory containing mp3 files”
dirname=gets.chomp
dirname = File.expand_path(dirname)
puts “Searching #{dirname}”
dirname += “/**/*.mp3″
s=mp3len(dirname).sort {|a,b| a[1] < => b[1]}
s.each{|q| puts “#{q[0]} => #{q[1]} [...]

Escaping URLs vs escaping HTML

I often get confused between two types of escaping you need to do when developing web applications: URL escaping and HTML escaping. This is a short note about when should you use what.
URL Escaping (or HTTP encoding or URL encoding) is used to escape the characters not allowed to be permitted for a URL (e.g. [...]

Yahoo Geocoding in ruby…

Find lattitude and longitude of any address
Yahoo just released a new beta of their maps webservice. Here is a small ruby script (inspired by Rasmus’s PHP code ) that I wrote that returns Lattitude, Longitude of the address provided…

require ‘open-uri’
require “rexml/document”
include REXML
url=’http://api.local.yahoo.com/MapsService/V1/geocode?appid=yahoomap.rb&location=’
puts ‘Enter Location: ‘
address=gets
address=URI.escape(address)
result=URI(url+address).read
doc = Document.new result
r=doc.elements["/ResultSet/Result"]
print “Precision: “, r.attributes["precision"],”\n”
r.children.each { |c| print c.name, [...]