# Case...when statement bug when using openid\_authentication?

**URL:** <https://www.ruby-forum.com/t/case-when-statement-bug-when-using-openid-authentication/138730>\
**Category:** Rails\
**Created:** [May 27, 2008, 9:26am UTC](https://www.ruby-forum.com/t/case-when-statement-bug-when-using-openid-authentication/138730 "2008-05-27T09:26:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![marzapower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@marzapower](https://www.ruby-forum.com/u/marzapower)\
**Post date:** [May 27, 2008, 9:26am UTC](https://www.ruby-forum.com/t/case-when-statement-bug-when-using-openid-authentication/138730/1 "2008-05-27T09:26:07Z")

</div>

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-

---

<div class="post-metadata">

**Author:** ![marzapower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@marzapower](https://www.ruby-forum.com/u/marzapower)\
**Post date:** [May 27, 2008, 4:03pm UTC](https://www.ruby-forum.com/t/case-when-statement-bug-when-using-openid-authentication/138730/2 "2008-05-27T16:03:49Z")

</div>

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 😃

---

<div class="post-metadata">

**Author:** ![marzapower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@marzapower](https://www.ruby-forum.com/u/marzapower)\
**Post date:** [May 27, 2008, 4:42pm UTC](https://www.ruby-forum.com/t/case-when-statement-bug-when-using-openid-authentication/138730/3 "2008-05-27T16:42:17Z")

</div>

On 27 May 2008, at 08:26, Daniele Di Bernardo 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)

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

---

<div class="post-metadata">

**Author:** ![marzapower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@marzapower](https://www.ruby-forum.com/u/marzapower)\
**Post date:** [May 27, 2008, 4:57pm UTC](https://www.ruby-forum.com/t/case-when-statement-bug-when-using-openid-authentication/138730/4 "2008-05-27T16:57:21Z")

</div>

Frederick C. wrote:

> On 27 May 2008, at 08:26, Daniele Di Bernardo 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 …

---

<div class="post-metadata">

**Author:** ![marzapower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@marzapower](https://www.ruby-forum.com/u/marzapower)\
**Post date:** [May 27, 2008, 5:46pm UTC](https://www.ruby-forum.com/t/case-when-statement-bug-when-using-openid-authentication/138730/5 "2008-05-27T17:46:48Z")

</div>

On May 27, 3:57 pm, Daniele Di Bernardo \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> > 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

---

<div class="post-metadata">

**Author:** ![marzapower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@marzapower](https://www.ruby-forum.com/u/marzapower)\
**Post date:** [May 27, 2008, 6:13pm UTC](https://www.ruby-forum.com/t/case-when-statement-bug-when-using-openid-authentication/138730/6 "2008-05-27T18:13:37Z")

</div>

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 …

---

<div class="post-metadata">

**Author:** ![marzapower](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e79b87/32.png) [@marzapower](https://www.ruby-forum.com/u/marzapower)\
**Post date:** [May 27, 2008, 6:13pm UTC](https://www.ruby-forum.com/t/case-when-statement-bug-when-using-openid-authentication/138730/7 "2008-05-27T18:13:03Z")

</div>

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

Ok, thank you! I didn’t of a desync between code and documentation. I  
will write a different implementation of the controller, now …
