Diego Scataglini

Looking Ahead

Manipulating Sessions in Warden/Devise

February9

As it turns out the best way to set extra sessions stuff or cookies when you use devise is through a method hook provided by warden.

Warden provides 2 hooks that are very useful: after_set_user and before_logout. In a rails app you would put these hooks in an initializer in config/initializers/<yourfile>.rb.

The hook looks like this:

Warden::Manager.after_set_user :except => :fetch do |record, warden, options|
  if record && warden.authenticated?(options[:scope])
    #... your magic
  end
end

Within the block the 3 parameters yielded are:

  • record => current resource, most likely a user object,
  • warden => an instance of warden::proxy
  • options => a series of options like the scope of this login (user,admin,author,moderator,etc)

The object warden has access through pretty much anything, session data, env data, cookies, strategies etc.

  • warden.cookies #=> a cookie jar like object that acts just like rails cookies
  • warden.session #=> warden specific session data stored in “warden.user.#{scope}.session”
  • warden.manager #=> to access a bunch of more stuff that is warden specific
  • warden.env #=> pretty much anything else you want to reach into

The thing that surprised me was that warden.session is stored inside a key (“warden.user.#{scope}.session”) inside the main session object. So how do you manipulate the main session object?

The answer happens to be simple: warden.env['rack.session'] will bring back everything in the session.

{"_csrf_token"=>"#{token}",
  "warden.user.#{scope}.key"=>
    ["#{resource.class}", ["#{resource.to_key}"], "#{resource.salt}"],
  "warden.user.#{scope}.session"=>
    ... anything that you set through warden.session[key] => value ...
}

And you manipulate it as you would expect warden.env['rack.session']['foo'] = ‘bar’

For more info on callbacks https://github.com/hassox/warden/wiki/Callbacks

Luke Wroblewski on Designing for Mobile First

September2

This summer I had the pleasure to review Luke Wroblewski’s O’Reilly master class on Designing for mobile first This title is also available on safari books online (best deal ever, get on safari). For those who follow Luke Wroblewski’s writing at http://lukew.com/ regularly & have read Tapworthy this might be a bit of redundant data. Even so, it’s nice to have it all nicely compiled in a 3 hours workshop.

If you haven’t started looking at designing for mobile device or even if you have this is the workshop you need to watch. It’ll definitely jump start you in the first case & round you on the second. In case you haven’t figured it out, mobile is not the next big thing, it’s the current big thing.

Aside from all the statistics & reasons on why you should care about mobile, what I liked about the workshop were certain design considerations. But that’s not all, Luke goes into the heart of how you need to think in terms of mobile design. To properly understand how to design for your user you need to envision him/her not at their desk but on the run, with a coffee on one hand and the mobile on the other. What are they trying to do? What is their goal? Because designing for mobile means being much more goal oriented. I love how he propose it: think about your user as 1 eye, 1 thumb. Partial attention = Focused design. (priceless advise)

Luke give a thorough overview of both mobile capabilities & mobile constraints.  Some of the mobile constraints also are some of its strengths as well. He talks about how to align the design with the mobile behavior. He shows some very interesting approaches to the navigation (learned quite a bit). From high level stuff, user motivation/activities to the nitty gritty implementation details, touch target size, mobile form structure, css sprites etc. Luke covered it all. Added bonus, Luke shares his knowledge of how to structure forms (sequential, non-linear, contextual), mobile or not, for more on that read his book.

If you’re involved in designing a mobile experience you owe it to your customers & your company to watch this workshop. If you have a subscription to safari books online, search for this title as it is available there.

I cannot recommend this workshop high enough.

Update: Luke has come out with a book properly titled Mobile First published by a book apart. Also some free videos are available as well as many articles.

HTML 5, a gentle introduction

September14

These are my slides for September’s Boca Raton’s ruby meetup. I’ll be giving an introduction to HTML 5. Just the markup. I cover semantic changes, deprecation and the new tags. Including gotchas.

Unfortunately there is just too much in HTML 5 for a single sitting.

_whyday – ruby explorations

August19
Why the lucky stiff
Image via Wikipedia

Yesterday at the boca raton ruby meetup I showed off some hacking that I did about 3-4 years ago when I was exploring ruby meta-programming as many people were doing at the time.

It was the period of _why, markaby and the poignant guide to ruby.

In that period I used to take the train to work, ride it for about 90 minutes, work a full day, go to happy hour with the CTO of the company and then ride the train back. During the ride back home I’d write a “column” that I entitled “Drunken Ruby Master Technique” where I’d try weird ruby syntax and coding styles.

Last night I revived such articles that gave me gems like this:

Here are the links to the original articles:

Not everything has real applications of course, but I thought at the time they were interesting findings.

I think it was really _why that inspired me to explore for language idiosyncrasies like the following and just have fun with it.

[*{"a" => "string"}] #=> [["a", "string"]]
[*{"a" => "string"}].first.class #=> Array

[*["a" => "string"]] #=> [{"a" => "string"}]
[*["a" => "string"]].first.class #=> Hash

Thank you _why, we miss you.

Google Analytics setDomain beware

August17

In May I added a google analytic line of code that should be used when tracking users among different subdomain under the same profile. The call in question is setDomain. I went through the documented steps (http://code.google.com/apis/analytics/docs/tracking/gaTrackingSite.html#domainSubDomains) and pushed it to production. I have used this functionality successfully previously.

About a month later I realized that line of code was completely screwing up with my analytic reporting. Was was happening is that google would view a portion of my traffic, if not all, as if, whenever they’d click on a link on the site, the user would leave the site and then come back on the page that they actually navigated to. This pretty much made every single page on the site a landing and an exit page.

This inflated a bunch of data, bounce ratio went up to 98% for most pages, unique visits & % new visitors were inflated and the page per visit went down accordingly. The only traffic that was tracked correctly was the ajax call that I was tracking. Since there was no change in url they didn’t count as exit/re-entry.

I looked through the GA googlegroups posting and apparently I wasn’t the only one having this problem.

The shocking part to me is that nobody from google posted a reply. I tried a bunch of possible remedies in vain.

So just as a buyer beware post, if you’re using this directive make sure it works, check your bounce ratio for the next 2 days.

As you can see from my graphic below it’s pretty obvious the effects of this seemingly innocuous function call.

Inflated reporting as a result of setDomain

Inflated reporting as a result of setDomain

Use at your own risk, really.

posted under Web | No Comments »