Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

2008-12-21

Trying Tumblr

My friendly Scottish Landscape photographer mentioned to me the other day that he'd been having a play with Tumblr. As normally happens when one of us tries something out, the other has to go have a play too. So, in no time, I created my own Tumblr account.

I've decided to use it as somewhere to "showcase" some of my photographs. Think of it as a mini-blog of personal favourites.

The nice thing about messing with this is that it gave me another reason to make use of the ruby library I wrote a while back for querying my published photographs. Tumblr has a simple API for writing entries so it made sense to write a little command line tool that would post a photograph to the blog. All the command does is it takes the filename of a photograph on the command line, finds it amongst the collection of published photographs, and then publishes an entry for it using nothing more than this code:

Net::HTTP.post_form( URI.parse( BASE_URL ),
{
"email" => EMAIL,
"password" => PASSWORD,
"type" => "photo",
"source" => image.jpeg_url,
"caption" => caption( image ),
"click-through-url" => image.url,
"tags" => image.tags.join( ',' ),
"generator" => "org.davep.tumblphoto"
} )
Nice! :-)

2008-08-29

CoolIris with RedBubble's Featured Works

Having started playing with CoolIris yesterday and having had some success, and with the thread that started this all off in the first place in mind, I thought it might be fun to mock up how things might look if RedBubble provided a feed that CoolIris could use.

So, I fired up GNU emacs and knocked out a quick and dirty variation on getrbart that created a "Media RSS" feed for the first 10 pages of the featured pages. The result looks like this:


If you have Cooliris installed in your browser you can see what the result looks like by visiting this page and kicking off CoolIris.

Thanks go to Bobbie for helping to test it.

2008-07-02

Self-Documenting ruby t-shirt

For reasons I don't really understand myself, recently I've been messing about with the idea of code on t-shirts. Sure, it's not exactly a new idea, but I couldn't resist it. I started out with this Lisp t-shirt (mainly because I wanted one myself, the order is in and I'm looking forward to getting it) and then went on and did this one (which I'll probably end up buying at some point in the near future).

And then I got to thinking that it might be interesting to have a go at creating a t-shirt that was, in effect, self-documenting. The idea being that the design would contain code that would, as much as possible, be usable in creating the design. After thinking on this for a while I decided that a Mandelbrot set would be a good place to start.

Having already done some Lisp I thought it was time to do something with ruby. So, I sat down and knocked up a quick script that would plot a Mandelbrot set, emitting it as a PBM file, just to make life easier. I then converted that to a PNG. After that I loaded up Photoshop Elements, created an empty image sized correctly for a RedBubble t-shirt design, added in the Mandelbrot and then layered the ruby source code over the top. A tweak or two later and I had a finished design:


I'm rather pleased with the result.

That said, the idea still needs some more work. While the shirt contains the code to produce the background image it's not a fully self-documenting t-shirt in that running the code on the shirt won't produce the whole design. That's to say: you can't run the code and get a PNG that's ready to upload to RedBubble.

I think a bit of reading of the documentation for RMagick is in order.

2008-05-29

Rails, Simile Timeline and Pocket IE

Following on from earlier, I ran into another problem with my little test application but, thankfully, it ended up being easy to fix.

In the application I make use of Timeline. Including it in the application is simple enough. I just include this:

javascript_include_tag(
"http://simile.mit.edu/timeline/api/timeline-api.js" )
in the html.erb file and the Timeline is available for use (I'll probably go with a local version at some point soon, I've just not got around to sorting that out yet).

The problem I ran into is that Pocket IE on my Pocket PC (a Dell Axim X50) doesn't seem to like this and throws an "Object Error". This was easy enough to solve in the end. I simply added this:
# Test if the request came from Pocket IE.
def pocket_ie?( request )
request.env[ "HTTP_USER_AGENT" ][ /MSIE.*Windows CE/ ]
end
to my ApplicationHelper class and then, in the html.erb file, simply said:
unless pocket_ie?( request )
javascript_include_tag(
"http://simile.mit.edu/timeline/api/timeline-api.js" )
end
Combined with a handheld specific stylesheet to hide the div that contains the timeline I end up with a version of the page that works in Pocket IE with no problems.

Okay, I guess it's a little kludgy, but it works and solves the problem at hand.

Ruby on Rails (and some WAP)

I'm quite a fan of ruby and have been for many years. In the last year or so I've been meaning to get to grips with rails but, every time I've tried, we've ended up having a falling out.

Most of the reasons have tended to be based around the (it seems to me) amazing lack of useful documentation. Sure, there's lots of stuff out there that documents the framework, but the sort of documentation that helps ease you into the learning process seems very thin on the ground.

Not too long back, at the suggestion of Rich Daley, I invested in a copy of Agile Web Development With Rails. This was just the sort of thing I was looking for and it's been a huge help (and also a huge source of frustration in itself seeing as how it's aimed at an earlier version of rails than the one I've got installed, but I've managed to get over that hump — it also seems that there's a third edition on the way).

Even with that book I still had a couple of aborted attempts to get to know rails better. As often happens with me the problem really came down to not having a goal to work to, a problem to solve, an application to build. Finally, last week, I devised a problem to solve and set to work.

And it worked, and it's been enjoyable. So far I'm impressed.

For me a good development system is one where my needs have been anticipated and, up until now, rails seems to deliver. A good example happened very recently. After having "finished" my little test application I thought it might be fun to add some simple WAP support. I imagined it would be easy enough using respond_to. Something like this:

def index
respond_to do |format|
format.html
format.wml
end
end
However, that didn't work. rails didn't know what to do with a "wml". A quick Google search later (see, the online docs are fine once you know what you're doing) and I found that all I needed to do was to edit my config/environment.rb to add the following:
Mime::Type.register "text/vnd.wap.wml", :wml
A restart of the server later and everything was fine. All I then needed to do was to create a index.wml.builder along the lines of this:
xml.instruct! :xml, :version => "1.0"

xml.wml "xml:lang" => "en-gb" do

xml.card :title => "My title here" do

xml.p "Something interesting here"
xml.p "Something else interesting here."

end

end
and I was done (and testing was easy enough thanks to the wmlbrowser add-on for Firefox).

I think I'm going to like rails. Now to find a real problem that needs solving...

2008-04-30

extlogkeywords

On the off chance that it might be useful to someone I've put a copy of extlogkeywords on my website. This is the tool I wrote to generate the data to feed GraphViz and which is central to the production of my clickable search term interconnectedness graph thingy.

It's not terribly good ruby code, it could do with some tidying up. Also, because I hacked up a quick-and-dirty apache log parser, it probably won't even work for your logs (I should probably hunt down a good and flexible ruby library for that, there's got to be one out there). Still, if anyone wants a play/hack, it's there for the taking.

2008-04-29

More ruby, graphviz and weblogs

Things have progressed a little further with my initial play with GraphViz. I noticed that it can produce SVG output and that the input format permits links to be associated with nodes. This meant it would be easy enough to produce a graph, with links, that could relate back to searches made on my site.

I decided to keep things simple and just deal with my photography so, after a little bit of extra scripting later, my site has a clickable search term interconnectedness graph thingy.

No sofas, slimy things with legs, horses, monks (electric or otherwise) or stoned poets were harmed during the writing of this code.

ruby, graphviz and weblogs

For the past few days I've been following Tim's messing about with some Haskell weblog analysis code and I've been meaning to have a play myself.

Some time ago I knocked up a little ruby script to extract the search terms from my weblogs. It never did anything clever and probably didn't do anything as near as clever as some of the things I could download but, well, you know what it's like when you want to hack up something yourself.

So, last night, I got around to extending it so that it would emit different types of reports. One that I added was a type of graph output (similar to Tim's) and, once I had that, I kept thinking that doing something graphical with the data would be the next fun option.

A quick search on the net and I stumbled on GraphViz. I then had a quick read up on the DOT language and I then extended my utility so that it would emit the graph data in DOT format. It took a little bit of messing about but, finally, I had something that worked.

The result looks like this. That's a subset of my web logs (if I throw too much at GraphViz's neato command I run out of memory!) and it only shows individual words that have appeared more than once. The numbers in brackets are the count of the number of times the word appears in the data. The lines, obviously, show how the words are related to each other as found in search query strings.

There's no surprise that the graph seems to cluster around Clipper and mutt.

I'm not sure that any of this tells me anything useful, but it is a different way of looking at what searches result in hits on my site.

2008-04-03

RedBubble works on a Google Map

Yet another simple application of the photography querying code I recently wrote:


The querying tool that I wrote knows which items on my main site are also on RedBubble. This means that it's one simple query to emit a KML file that would show all of those works on a Google Map. The code looks something like this:
#!/usr/bin/env ruby

require 'photography'
include Photography

...KML header/document start emitted here...

# For each photograph that is on RedBubble...
Albums.new.load.images.select {|i| i.for_sale? }.each {|i|
...KML Placemark emitted here...
}

...end of KML document emitted here...
Nice and simple.

The resulting file can then be fed to Google Maps, Google Earth or Virtual Earth. It can also be used to create an embedded map.

2008-04-02

ruby, geonames and photography

Recently I've been working on some ruby code that serves up the photography content of my website as a ruby object (no, it's not available for download anywhere, it wouldn't be any use to anyone else). The point behind it is that it lets me query my photographs in all sorts of different ways (especially handy when working in irb as it means I've got a kind of REPL for working with my photographs) and also makes it easy to write programs that generate useful data.

Last night I got to thinking that it would be interesting to use this code to try and "reverse geocode" the ICBM address that every album has so that I could display the nearest centre of population to the album.

After a bit of searching on the net I found GeoNames. A handy enough site itself but even more handy because it has an API and a list of client libraries that are available. Handily there's a library for ruby.

A little bit of hacking later and I had this:

#!/usr/bin/env ruby

require 'geonames'
require 'photography'

include Photography
include Geonames

# Get place names for all the icbm locations
Albums.new.load.collect {|album|
[ album.latitude, album.longitude ]
}.uniq.each {|icbm|
near = WebService.find_nearby_place_name( icbm[ 0 ], icbm[ 1 ] )
puts( "#{icbm[ 0 ]}\t" +
"#{icbm[ 1 ]}\t" +
"#{near[ 0 ].name}\t" +
"#{near[ 0 ].country_name}\t" +
"#{near[ 0 ].distance}" )
}
Put simply: it gets a list of the unique locations that I've recorded and runs through them getting the nearest known location. I then use the resulting data on my site to display the name of the nearest location to where the album is from.

It isn't perfect. For example, most of the photographs I've shot in and around Billingborough end up being shown as being near Horbling (the next village north of here, and somewhat smaller than Billingborough). Still, it's a start.

Currently I've got the text linking to a search on nearby.org.uk. Via that there's some fascinating (for varying values of fascinating) links to further searches that can be done based on location.