groundhog javascript

Just a few lines of JavaScript for the prototype framework that will call your method everytime either when you either resize the window or complete an ajax request:

function yourmethod() { alert('HI');} document.observe('dom:loaded', yourmethod); Ajax.Responders.register({onComplete: yourmethod}); Event.observe(window, "resize", yourmethod);

a few bits on flickr_fu

For integrating flickr into photolog.at I decided to give flickr_fu a shot as rflickr is not maintained as it used to be. A warning: fickr_fu isn’t that complete, e.g. blogs are missing but it should be too hard to extend and push the patches to the hub.

Like with OpenID I want to integrate flickr the hard way: Assigning multiple accounts to one of my users. I’m not sure if there are many users who have more than one account, but I’m sure for groups of artists it will be just the perfect tool.

your credentials

There are a few things you have to understand and know when working with the flickr API. First, you credentials:

  • API key: you can request three different kinds of API keys: For Desktop, Web or Mobile apps. Of course I take the web one and of course it’s the worst possible…
  • Secret: An additional security measure, dunno why at all
  • frob: Looking up that word is a time-waster and if you don’t handle with the right frob you are wasting time. For all but the web app you won’t need the frob but for the web app you need one and you get when the user grants access to your app and is being redirected to the callback url that you setup with the API key. Sadly this callback url is not at all configurable on runtime, but I’ll come back on that later.
  • token: Once you obtained the frob you can store this token, hopefully forever. Tokens are per-user and you should run checkToken if a request fails

problems with has_many

As I said, more than one flickr account per user. Which is bad as I cannot customize the callback url on the fly, adding a parameter to find the account that is being processed. This problem bothered me half an afternoon and the solution is simply to try the frob returned from flickr on all accounts the user has setup with my app. Normally that’s one account anyways and you can sort descending by updated_at to speed up the process.

You can test the accounts by simply trying to get the token and once you have a hit, store the token and forget about the stupid frob.

...to be continued…

Google chart

I stumbled accross (well, let’s say it was on delicious popular) about gchartrb which is a Ruby API for Google Charts and the only place to work it into was my good old ananasblau.de

The examples from the API are quite simple and when I tried to do a chart for the last twenty days I was in big trouble. Here’s my source which uses some rarely used Date methods and by far to many collects. Basically I get my models data, create a hash with date-keys, fill it up, sort it (afterwards it’s an array for some reason) and for the two axis I collect and max on the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

stats = MyModel.find(:all, :group => 'date', :order => 'created_at ASC',
    :select => ['count(id) as count, DATE_FORMAT(created_at, "%Y-%m-%d") as date'], 
    :conditions => ' created_at > from_unixtime(unix_timestamp() - 3600*24*21)')
  unless stats.empty?
    @dates = {}
    Date.parse(stats[0].date).upto(Date.today()){|d| @dates[d.strftime('%Y-%m-%d')] = 0 }
    stats.collect { |c| @dates[c.date.to_s] = c.count.to_i }
    @dates = @dates.sort{|a,b| a[0] <=> b[0]} # note, it's an array from here on
    #  d[0][8,2] for day only. d[0][5,5] for month and day
    @chart =  GoogleChart::BarChart.new('700x100', "", :vertical, true)
    @chart.axis :x, :labels => @dates.collect{|d| d[0][8,2] }, :alignment => :left
    @chart.axis :y, :range => [0, @dates.max{|a,b| a[1] <=> b[1]}[1]]
    @chart.grid
    @chart.data "", @dates.collect{|d| d[1]}, '55CC88' 
  end