case...when statement bug when using openid_authentication?

Hello people! I'm asking for help to solve a little problem concerning the usage of the openid_authentication plugin. I'm following the README file included by DHH, so I wrote this method in my SessionsController:

    def open_id_authentication(identity_url)       # Pass optional :required and :optional keys to specify what sreg fields you want.       # Be sure to yield registration, a third argument in the #authenticate_with_open_id block.       authenticate_with_open_id(identity_url,           :required => [ :nickname, :email ],           :optional => :fullname) do |status, identity_url, registration>             logger.debug "Received status #{status.inspect}"         if (status === :successful)           logger.debug "We got a successful answer"         end         case status         when :missing           logger.debug "Missing!"           failed_login "Sorry, the OpenID server couldn't be found"         when :canceled           logger.debug "Canceled!"           failed_login "OpenID verification was canceled"         when :failed           logger.debug "Failed!"           failed_login "Sorry, the OpenID verification failed"         when :successful           logger.debug "Correct!"           if @current_user = @account.users.find_by_identity_url(identity_url)             assign_registration_attributes!(registration)

            if current_user.save               successful_login             else               failed_login "Your OpenID profile registration failed: " +                 @current_user.errors.full_messages.to_sentence             end           else             failed_login "Sorry, no user by that identity URL exists"           end         else           logger.debug "Something other ..."         end       end     end

If you look at the logger.debug calls, you'll see that when we get a successful response, the logger "should" print "We got a successful answer" and then "Correct!". But, in fact, it prints "We got a successful answer" and then "Something other ...". So, apparently, the status === :successful comparison is true, but the case ... when :successful test is false, even if this statement uses the === operator.

What's wrong in this code? I really can't figure out ... and I really need some "expert" help.

Thank you!

-daniele-

Damn it! I'm going mad ... this "bug" is really impossible to understand, and no one here seems to help me ... Maybe I'll ask DHH directly :smiley:

If you look at the logger.debug calls, you'll see that when we get a successful response, the logger "should" print "We got a successful answer" and then "Correct!". But, in fact, it prints "We got a successful answer" and then "Something other ...". So, apparently, the status === :successful comparison is true, but the case ... when :successful test is false, even if this statement uses the ===
operator.

What's wrong in this code? I really can't figure out ... and I really need some "expert" help.

Well it's worth remembering that === is not reflexive: 1 === Numeric => false Numeric === 1 => true

And that a case statement will do label === value (but in your if
you've got value === label)

But the real question is what exactly is the value of status ? is it
actually a symbol or something else (hint: look at the code. it's not
a symbol)

Fred

Frederick Cheung wrote:

What's wrong in this code? I really can't figure out ... and I really need some "expert" help.

Well it's worth remembering that === is not reflexive: 1 === Numeric => false Numeric === 1 => true

And that a case statement will do label === value (but in your if you've got value === label)

Thanks, didn't know that.

But the real question is what exactly is the value of status ? is it actually a symbol or something else (hint: look at the code. it's not a symbol)

Already looked at it. In fact, status it's not a simbol, it's an instance of the class Result, defined within the openid_authentication plugin:

  class Result     ERROR_MESSAGES = {       :missing => "Sorry, the OpenID server couldn't be found",       :canceled => "OpenID verification was canceled",       :failed => "OpenID verification failed",       :setup_needed => "OpenID verification needs setup"     }

    def self.(code)       new(code)     end

    def initialize(code)       @code = code     end

    def ===(code)       if code == :unsuccessful && unsuccessful?         true       else         @code == code       end     end

    ERROR_MESSAGES.keys.each { |state| define_method("#{state}?") { @code == state } }

    def successful?       @code == :successful     end

    def unsuccessful?       ERROR_MESSAGES.keys.include?(@code)     end

    def message       ERROR_MESSAGES[@code]     end   end

in which we find the definition of ===. From the logs, I get something like this, from status.inspect:

#<OpenIdAuthentication::Result:0x481ce38 @code=:successful>

The code in my controller is taken from the README file, written by DHH. Strange, really strange ...

Frederick Cheung wrote:

Thanks, didn't know that.

> But the real question is what exactly is the value of status ? is it > actually a symbol or something else (hint: look at the code. it's not > a symbol)

Already looked at it. In fact, status it's not a simbol, it's an instance of the class Result, defined within the openid_authentication plugin:

in which we find the definition of ===.

which makes it obvious why status === :success works, but :success === status doesn't (since at that point Symbol's implementatio will be called.

From the logs, I get something like this, from status.inspect:

#<OpenIdAuthentication::Result:0x481ce38 @code=:successful>

The code in my controller is taken from the README file, written by DHH. Strange, really strange ...

The code was changed (rev 6318) but the documentation wasn't. the readme's just wrong.

Fred

Daniele Di Bernardo wrote:

The code was changed (rev 6318) but the documentation wasn't. the readme's just wrong.

I didn't of a desync

didn't think of ...