Posts
-
Cristalp Here We Come
Heads up all you Euros: Tom and I will be taking on the Grand Raid Cristalp in August.
You have been warned.
-
Ruby Google Charts API Data Encoding
On Friday, Google released their Charts API. You pass your data in a URL, google give you a nice graph back.
The hard part is encoding you’re raw data, into one of Google’s three formats. So I thought I’d help all out!
All of the methods take an array of integers and the maximum value of those integers. It then returns the data in the relevant format.
my_data = [1,2,3,4,5] my_max_value = my_data.sort.last simple_encode(my_data, my_max_value) text_encode(my_data, my_max_value) extended_encode(my_data, my_max_value)
Simple Encoding
def simple_encode(data_arr, max_value) simple_encoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' # Douglas F Shearer 2007 # http://douglasfshearer.com/blog/ruby-google-charts-api-data-encoding data = '' data_arr.each do |value| if value.is_a?(Integer) && value >= 0 data << simple_encoding[((simple_encoding.length-1).to_f * value.to_f / max_value.to_f).to_i] else data << '_' end end data end
Text Encoding
def text_encode(data_arr, max_value) # Douglas F Shearer 2007 # http://douglasfshearer.com/blog/ruby-google-charts-api-data-encoding data = '' data_arr.each do |value| if value.is_a?(Integer) && value >= 0 data << ((999.0 * value.to_f / max_value.to_f).round/10.to_f).to_s else data << '-1' end data << ',' end data.chop end
Extended Encoding
def extended_encode(data_arr, max_value) # Douglas F Shearer 2007 # http://douglasfshearer.com/blog/ruby-google-charts-api-data-encoding characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.'.split(//) data = '' data_arr.each do |value| if value.is_a?(Integer) && value >= 0 new_value = ((4095 * value.to_f / max_value.to_f).to_i) sixtyfours = new_value/64 units = new_value%64 p '64:' + sixtyfours.to_s p 'units: ' + units.to_s data << characters[sixtyfours] + characters[units] else data << '__' end end data end
A Plugin
I may at some point release a ruby wrapper for this as a plugin. Currently I have a fairly good, tested prototype that covers most of the features for line and bar charts. More will be added as I need it, but release may not be imminent.
Any errata or comments for the above should be emailed to my usual address, or just leave a comment below.
-
Free Online Training Diary
After some months of development, I have decided to release the online training diary I created for myself for public consumption.
http://training.douglasfshearer.com
It’s in an Alpha testing phase, since it has pretty much only been me using it.
If you have any queries or suggestions, just use the feedback page on the site.
-
Cycle Speedway
On sunday, the EUCC headed down to the Edinburgh Falcons cycle speedway track for a tryout.
There a few things to get used to in cycle speedway. First the bikes have one gear, no brakes, no rake on the forks, and swept back handlebars much like an old style shopper. For a lap or so you are bewildered by the oddly sharp steering, but then you realise it is exactly what is needed for holding full speed through the turns.
Next up is the track, a 30*15m ash surfaced oval, with almost no banking on the turns. This is actually surprisingly grippy, especially with the cyclo-cross style tyres on the bikes.
Lastly there is the racing format. Four riders, two team of two, race a 4 lap race. Points are given out for 1st, 2nd, 3rd, 4th being 4,3,2,1 respectively. So it’s not just a out-and-out sprint, there are blocking and tactics involved.
Despite all the initial culture shock, it’s marvelously good fun, and the stacks are pretty special too.
We will definitely be going back, thanks to the EF lads for the great afternoon.
-
I named 49 HTML elements in 5 minutes
Is amazing how annoyed you can be with yourself when you look at the list of the ones you missed.
-
My Gmail Doesn't Support IMAP Yet
IMAP still not supported on your GMail account?
To get it you must set your language to English US in GMAIL settings. The POP tab will change to include IMAP when you save and go back to settings.
Gleaned from the Gmail supports IMAP announce post comments.
Enjoy!
-
A New Frustration
I hope it’s worth the trouble!
-
Leopard
I installed Leopard this morning before work. From a day’s work I can say it’s faster (better multi-core support) is general use. A bonus too is having most of the development tools I use day to day build in. No more struggling to get the Ruby version changed and Gems installed.
One issue though, ScreenCapture (Command + Shift + 3 | 4) won’t capture screnshots, and instead crashes.
Process: screencapture [98636] Path: /usr/sbin/screencapture Identifier: screencapture Version: ??? (???) Code Type: X86 (Native) Parent Process: launchd [1] Date/Time: 2007-10-29 20:26:40.211 +0000 OS Version: Mac OS X 10.5 (9A581) Report Version: 6 Exception Type: EXC_BREAKPOINT (SIGTRAP) Exception Codes: 0x0000000000000002, 0x0000000000000000 Crashed Thread: 0 Application Specific Information: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString _getValue:forType:]: unrecognized selector sent to instance 0xa0752a70 *snip*
Why? I have no idea at the moment, but it does leave me without a feature I use several times a day.
A few sites mention this if you do a Google search, but none suggest a fix. I’ll post a fix here once I discover one.
Update. There is now a thread on the Apple Support Forums about this. It seems to be caused by importing a user and their files from a previous install. If you create a new user on the same machine, the problem does not exist.
The Fix. The thread I linked to above has the solution. Basically if you delete the file
~/Library/Preferences/.GlobalPreferences.plist
, then log out and in, the problem should be resolved. Enjoy. -
Gravatar For Ruby And Ruby On Rails
Gravatar is a web based avatar service. You sign up, upload your avatar, and associate it with your email address(es). Now when you visit a site leveraging Gravatar and enter your email address (like comments on a blog), your avatar will automatically appear!
So today I decided to implement Gravatar on this blog, and here’s what I came up with.
If you are using this with Ruby on Rails, then add the code to you Application Helper (
application_helper.rb
). You may ommit therequire
line at the top, as this is already included in your Rails environment.Generate A Gravatar URL
# This require is not needed for Rails. require 'digest/md5' # Returns a Gravatar URL associated with the email parameter. # # Gravatar Options: # - rating: Can be one of G, PG, R or X. Default is X. # - size: Size of the image. Default is 80px. # - default: URL for fallback image if none is found or image exceeds rating. def gravatar_url(email,gravatar_options={}) grav_url = 'http://www.gravatar.com/avatar.php?' grav_url << "gravatar_id=#{Digest::MD5.new.update(email)}" grav_url << "&rating=#{gravatar_options[:rating]}" if gravatar_options[:rating] grav_url << "&size=#{gravatar_options[:size]}" if gravatar_options[:size] grav_url << "&default=#{gravatar_options[:default]}" if gravatar_options[:default] end
All we have to do to get a Gravatar URL is:
gravatar_url('test@gravatar.com') => "http://www.gravatar.com/avatar.php?gravatar_id=df3d4780faaf2446a65ce39eafdfe1c0"
Use the @gravatar_options hash when you want to adjust the size, rating, or add a default URL for when there is no Gravatar.
gravatar_url('test@gravatar.com',{ :size => 20 }) => "http://www.gravatar.com/avatar.php?gravatar_id=df3d4780faaf2446a65ce39eafdfe1c0&size=20"
For more detail on the particulars of the options, see the online documentation.
Generate Gravatar HTML
If you want to generate an
img
tag from this, something like the following is what you want.# Returns a Gravatar image tag associated with the email parameter. def gravatar(email,gravatar_options={}) # Set the img alt text. alt_text = 'Gravatar' # Sets the image sixe based on the gravatar_options. img_size = gravatar_options.include?(:size) ? gravatar_options[:size] : '80' "<img src=\"#{gravatar_url(email, gravatar_options)}\" alt=\"#{alt_text}\" height=\"#{img_size}\" width=\"#{img_size}\" />" end
Now if we run our examples from above…
gravatar('test@gravatar.com') => "<img src="http://www.gravatar.com/avatar.php?gravatar_id=df3d4780faaf2446a65ce39eafdfe1c0&rating=PG" alt="Gravatar" height="80" width="80" />"
And with options…
gravatar('test@gravatar.com',{ :size => 20 }) => "<img src="http://www.gravatar.com/avatar.php?gravatar_id=df3d4780faaf2446a65ce39eafdfe1c0&rating=PG&size=20" alt="Gravatar" height="20" width="20" />"
Enjoy!
Taking it further.
A good enhancement to this would be to cache the Gravatars locally, to speed up their load times. Look out for a future blog post on this.
-
Thanks Adobe
The postman (Who for a change was working rather than striking) rang the doorbell this morning, with a package from Amazon. I was fairly sure I wasn’t waiting on anything, yet it was addressed to me.
I opened it up, inside was a copy of The Complete Guide to Flex 2 and ActionScript 3.0 by Charles E. Brown.
All that was written on the invoice was “Happy Flex coding! With best wishes from Adobe.”.
So, thanks Adobe! Just in time too, as my other Flex book doesn’t cover charting, which is something I’m looking to employ in my latest secret project. Now, if only I could figure out why they bought me it…