Count unique records with DataMapper
Saturday 01/3/2009 – Category: Uncategorized – No Comments
dm-aggregates provides an aggregate method that allows for "multiple aggregate functions to be used in a single query, with automatic grouping when :fields is specified"
Hit.aggregate(:ip_address).length
Will yield the number of unique IP addresses in the hits table.
Simple graphing with googlecharts (plus gridlines and markers!)
Saturday 01/3/2009 – Category: Uncategorized – 1 Comment

It didn't occur to me until a day or two after I released Flickr Original that I should probably record usage stats, so I added a few lines of code to track views/downloads. Earlier this week I finally decided to do something with all the data. I decided on a simple line graph to trend views/users/downloads and a counts summary table (inspired by the one from the Flickr stats page).
For the line graph I initially looked at gruff but ended up using the Google Chart API in conjuction with mattetti's Googlecharts gem because it seemed easier to use (and no need to install RMagick/Imagemagick).
The Googlecharts gem basically takes in parameters and generates the url that creates the image.
Gchart.line(:data => [0, 40, 10, 70, 20]) generates:
http://chart.apis.google.com/chart?chs=300x200&chd=s:AiI9R&cht=lc which is this generated image:
The parameters you can use are pretty self-explanatory from the usage examples. I also wanted to add point markers and gridlines (a la Google Analytics), which have to be added as custom params. You can do this by setting the :custom hash to a query string with the extra variables (see the Chart API docs for all the options...there are a lot!) .
Code snippet:
Generates:
Custom Parameters
Line style
chls=
[data set 1 line thickness],
[length of line segment],
[length of blank segment]
chls=3,1,0 creates a solid line 3px thick
Gridlines
chg=
[x axis step size],
[y axis step size],
[length of line segment],
[length of blank segment]
chg=14.29,50,1,4 creates gridlines of 1px line segments spaced 4px apart with a step size of 100/7 on the x-axis and 100/2 on the y-axis. This gives us gridlines for each day and a halfway marker for the counts.
Markers
chm=
[marker type],[color],[data set index],[data point],[size],[priority]
o = circle, 0 uses the current data set, -1 to draw a marker on each data point
chm=o,0066FF,0,-1,6 creates blue 6px circle markers on each point of the current set
Ranges in Ruby (and .. vs …)
Saturday 01/3/2009 – Category: Uncategorized – 1 Comment
Ranges in Ruby are nifty: you can define a set by its start and end values and a range of values will be generated. This works "as long as the objects can be compared using their <=> operator and they support the succ method to return the next object in sequence."
Ranges created with .. will include the last value while those created with ... will exclude the last value. The syntax for this isn't the most intuitive, I had to go back to the docs to make sure I was using the right form.
A simple example:
(1..10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
(1...10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
Date ranges using Merb and DataMapper:
(make sure to add the dm-helpers dependency for Rails-like date/time helpers)
yesterday = (Time.now-1.day).to_date
today = Date.today
@views_yesterday = Hit.count(:type => 'view', :created_at => yesterday..today)
Will yield all hits of type view that were created yesterday.
Using getSelection() with content in frames
Sunday 12/28/2008 – Category: Uncategorized – No Comments
An iPhoneSender user recently reported a bug with the Safari bookmarklet not correctly grabbing the selection and suspected it was because the selected content was in frames.
After some digging around, it turns out he was right--if your selection is in a frame, the normal window.getSelection() method will return nothing; you'll need to target the specific frame instead. We don't know the frame's name so we can't target it with parent.frames["framename"], but what we can do is just loop over all the frames in the document until we find it.
(simplified and expanded):
Easily download full-size Flickr photos with Flickr Original (Firefox extension) (update)
Tuesday 12/16/2008 – Category: Uncategorized – 137 Comments

Follow Flickr Original on twitter for status updates
(UPDATE: Version 1.0.8 available!)
(UPDATE: Version 1.0.7 available!)
(UPDATE: Version 1.0.6 available!)
(UPDATE: Version 1.0.5 available!)
(UPDATE: Version 1.0.4 available!)
(UPDATE: New and improved version 1.0.3 available!)
(UPDATE: Firefox 3.5 version available!)
I love Flickr, but sometimes their interface gets in the way and stuff takes more clicks that I'd like. One thing I like to do is view/download original size images with one click.
After doing some preliminary research, I found that out that you used to be able to figure out the url of the original image based on Flickr's standard url structure...but it seems Flickr has closed the loophole. Bummer.
My extension extracts the url of the image you select, parses out the image id, and sends it to a simple web service that uses the Flickr API to return the url of the original image.
Let me know how it works for you!
Download
Flickr Original 1.0.2
- Updated to be compatible with Firefox 3.5
- Added "View Original Flickr Image" option
- View/download options now only show up on valid Flickr photos
- Web service now returns the largest sized image available (for example, it will return the large version if the original isn't available)
Usage
After downloading and installing you'll be asked to restart Firefox. Do it.
While on Flickr, just right-click on the thumbnail you want to download full-size and select "Download Original Flickr Image". That's all!
Caveats
This won't work on images that the photographer has marked "All Rights Reserved" or has disabled downloading.
Recent Posts
- More Flickr Original Updates
(Sunday 01/23/2011 – Uncategorized – 13 Comments) - Flickr Original updates
(Saturday 08/7/2010 – Uncategorized – 19 Comments) - LED Light for iPhone 4
(Monday 06/28/2010 – Uncategorized – 65 Comments) - WWDC 2010: Worth Every Minute
(Monday 06/14/2010 – Uncategorized – No Comments)
