Read cookie

Brian C. wrote:

Thread[:key] = cookies[“key”]

Sorry, that should have been

Thread.current[:key] = cookies["key"]

Pål Bergström wrote:

So I thought I would just ask: Can Ruby access a cookie in a
users browser?

And I say again, that question is completely meaningless. Ruby is a
programming language; it can do anything that a programming language can
do. But by itself it knows nothing about cookies - they are an artefact
of HTTP. So you need to be talking about a Ruby HTTP environment, such
as Rails or Sinatra or Rack or Webrick, which deals with incoming HTTP
requests. That’s the “context” which someone else asked for.

All of these do have mechanisms for accessing cookies - even if it’s
only directly accessing the HTTP Cookie: and Set-Cookie: headers - so
the answer to your question is quite clearly “Yes”.

However, I imagine that “Yes” is not the answer you were looking for.

What you actually have is a problem, and you want help finding a
solution. In that case, you need to describe the specific problem.

“I cannot access cookies in Rails” is not sufficient - clearly you can,
and I showed you code which does it.

“in Rails, I cannot access cookies inside a model object” is getting
there. Or “I know how to access cookies inside the Rails controller but
not in other objects”

Even if you wrote some code in lib/foo.rb, this is still running in the
context some Ruby HTTP framework; how you access cookies depends on what
the framework provides. If it were running standalone it wouldn’t
receive a HTTP request in the first place (unless you were writing a
HTTP server from scratch)

I could do it with cgi but not anymore.

That also means nothing unless you expand on “it” (what you’re trying to
do), or the problem (you have some code which worked before but which
does not work now, in which case you can post the code itself and ask
for ways to rewrite it)

In the very old days, I think Rails had a global CGI object for “the
current request”, since it was only ever intended to handle one request
at a time within any particular process. Now that Rails apps can be
multi-threaded, this will have been removed, as it wouldn’t make any
sense - each controller instance must have its own HTTP request object.
They’ve also removed the use of the CGI library, since Rack does it
better and faster.

I don’t have the before_save and after_find in the controller. It’s in
the model.

Yes, it’s part of the design of Rails that models have before_save and
after_find hooks. Note that ActiveRecord, from which you have built your
models, is also a part of Rails - albeit one which can be used outside
of Rails quite easily.

Regards,

Brian.

2010/9/3 Pål Bergström [email protected]:

Jesús Gabriel y Galán wrote:

require ‘mechanize’

agent = WWW::Mechanize.new
page = agent.get(“http://www.google.com”)
p agent.cookies

Got it working when installed as a gem. Thank you so much :slight_smile: Don’t
understand why I didn’t get an answer before.

Maybe because your question was not completely clear, and could get
confused for someone trying to develop a web application and retreving
the cookie in the server that’s serving the request. A better question
could have been:

I am hitting my own website with a script and the server returns a
cookie in the response: what’s the best way to make the request and
read the cookie set by the server? Or something like this :slight_smile:

I’ve asked about this
severeal times. Hopefully I’m on the right track now.

I do get a cookie. A session cookie. But how do I get a particular
cookie?

The cookies method returns an array of cookies, you will have to
search for the particular one inside that array: try Array#find method
or such.

Jesus.

2010/9/4 PÃ¥l Bergström [email protected]

Thank you for the clarification. I’ll guess I will use the instance
variable and add it everywhere I need it. I was hoping I could avoid
that.

You could put it in a method in your ApplicationController, then in each
of
your specific controllers, just use a before filter to apply it to the
methods you want. Then you only have to have one additional line in each
of
the controllers that will use that before filter.

class ApplicationController < ActionController::Base
def verify_key
redirect_to some_path unless logged_in? &&
current_user.valid_key?(session[‘aes-key’])
end
end

class ResourcesTypeOneController < ApplicationController
before_filter :verify_key , :only => [:destroy, :edit , :update]

end

class ResourcesTypeTwoController < ApplicationController
before_filter :verify_key , :except => [:show]

end

2010/9/3 Jesús Gabriel y Galán [email protected]:

2010/9/3 Pål Bergström [email protected]:

I do get a cookie. A session cookie. But how do I get a particular
cookie?

The cookies method returns an array of cookies, you will have to
search for the particular one inside that array: try Array#find method
or such.

That should read Enumerable#find

Jesus.

Jesús Gabriel y Galán wrote:

That should read Enumerable#find

If you understand what I’m trying to do, do you think the use of
Mechanize is a good or bad idea? Is it better to use the instance
variable to send the variable from controller to the model?

(As you said, probably more a Rails question. But now I’m here :slight_smile: )

On Tue, Sep 07, 2010 at 12:14:35AM +0900, Pål Bergström wrote:

If you understand what I’m trying to do, do you think the use of
Mechanize is a good or bad idea? Is it better to use the instance
variable to send the variable from controller to the model?

Mechanize is a HTTP client - a programmable web browser if you like.

Rails is a HTTP server.

You wouldn’t use Mechanize in a Rails app, unless your Rails app was in
turn
connecting to a different web server and pulling information from that.

If you connected to your own web server using Mechanize, the web server
would see it as a new client, distinct from all your other clients, and
probably generate a new cookie for it.

Brian C. wrote:

you need to describe the specific problem.

I know. I believe I did that. As I said I’ve asked before, not this
time. But there is a reason for that. I think the problem is our
different mindsets. I don’t know, so I ask stupid questions. You know
and don’t really understand my problem and what I’m trying to do as my
question is unlogical.

Thank you for the clarification. I’ll guess I will use the instance
variable and add it everywhere I need it. I was hoping I could avoid
that.

2010/9/6 Pål Bergström [email protected]:

Jesús Gabriel y Galán wrote:

That should read Enumerable#find

If you understand what I’m trying to do, do you think the use of
Mechanize is a good or bad idea? Is it better to use the instance
variable to send the variable from controller to the model?

(As you said, probably more a Rails question. But now I’m here :slight_smile: )

I answered what I did in case you were really looking after calling a
website and retrieving the cookie from the response. It seems that I
misunderstood your question, since it’s obvious from the following
conversation that you were talking about retrieving the cookie from a
client’s request. I don’t know much about Rails, Brian and others have
given advice regarding that.

Now that your question is clear (reading a cookie from the request and
using it around controllers and models) you won’t need Mechanize for
anything.

Jesus.