Geolocate visitors to your Rails App

March 24th, 2008

In modern web apps, knowing the location of your customer can be very important. A friend and I are developing a location-based service right now. I whipped up the following Ruby code that uses the hostip.info API to fetch a user’s location based on his IP address.

require ‘net/http’

def geoip(ip)
  begin
    res=Net::HTTP.get(URI.parse(”http://api.hostip.info/get_html.php?ip=#{ip}&position=true”))
    resarr = res.split(”\n”)
    pos = { :country => resarr[0].split(”: “)[1],
            :city => resarr[1].split(”: “)[1].split(”,”)[0],
            :state => resarr[1].split(”, “)[1],
            :lat => resarr[2].split(”: “)[1],
            :lng => resarr[3].split(”: “)[1]}
  rescue SocketError, Errno::ECONNREFUSED => e
    pos = nil
  end
end

This method takes an ip address. When calling this method, use Rails’ built-in method for determining the originating IP address, request.remote_ip.

location = geoip(request.remote_ip)

Crayon Physics Deluxe

March 19th, 2008

I just had the pleasure of reading this Slate article on a game that was unveiled at the Game Developers Conference called Crayon Physics Deluxe. A description by me wouldn’t do it justice. Check out the video:

I’ve realized over the years that while I do have artistic sensibilities in the traditional sense, what I really appreciate is genre-busting, disruptive work such as this.

Lat, lng points to GMmaps polyline in Ruby

February 11th, 2008

def encode(num)
  str = “”
  num = (num * 1e5).floor
  num <<= 1
  num = ~num if num < 0
  while num >= 0×20
    str += (((num & 0×1f) | 0×20) + 63).chr
    num >>= 5
  end
  str += (num+63).chr
  return str
end

def to_encoded_polyline(points)
  polyline = “”
  points.each do |point|
    polyline += encode(point[:lat])
    polyline += encode(point[:lng])
  end
  return polyline
end

“Why would I want to do that?”

February 6th, 2008

I’m working with a friend on a chat application that we think is a unique approach to an idea that’s been around for a few years, but has never been implemented in a sensible way. When I tell people at work or at school about it, most of them get pretty excited, but I’ve noticed that I get quite a different reaction when I explain it to non-”tech-savvy” people. Their response is either, “I don’t get it,” or more frequently, “Why would I want to do that?”

At first, I found this response rather disheartening, since as an entrepreneur, my top priority is to make something that people are going to want. After some reflection, though, I think that this response is OK. I think about applications like Facebook and Twitter, both of which are hugely popular presently, but would have sounded tedious and pointless to Joe Sixpack a year or two before they were launched.

I’m crossing my fingers, and hoping that when I have a workable demo to show non-tech people, they will “get it” and be as excited as my technologically inclined peers. Is this leap of faith necessary, or am I being foolish in trying to sell people something that they may not readily understand?

Either way, I think my elevator pitch needs some work.

This is a big deal

December 20th, 2007

http://news-service.stanford.edu/news/2008/january9/nanowire-010908.html