mechanize-cookie_store is an extension to mechanize gem that allows you to persist the mechanize agent cookie set in a smarter way than serialized or plain files in the server's system. One of its biggest benefits is allowing you to share your cookies across multiple server instances.
Add this line to your application's Gemfile:
gem 'mechanize-cookie_store'
And then execute:
$ bundle
Or install it yourself as:
$ gem install mechanize-cookie_store
For Rails applications you can create config/initializers/mechanize-cookie_store.rb
and provide the necessary
configuration params to access your desired persistance method.
If you're not using Rails, you just need to make sure to run the same configuration code before using Mechanize.
Please see below the available built-in stores and their specific configurations:
Currently, the only supported store is Redis.
Mechanize::CookieStore.configure do |config|
config.connection_params = {
url: "your-redis-url",
namespace: "your-redis-namespace" # optional
}
end
url
must have a valid redis server urlnamespace
parameter is optional, default namespace ismechanize_cookies
After configuring it, usage looks pretty similar to the way you would use mechanize without this gem.
Instantiate a mechanize session and use it:
agent = Mechanize.new
agent.get("https://www.google.com")
Then save
orload
your cookie_jar the same way you would do it without mechanize-cookie_store
.
agent.cookie_jar.save # to persist cookies to storage
agent.cookie_jar.load # to load back back into the agent from storage
Or use if you want to load specific cookie set you can pass to load
the options
domain
, path
and name
like this:
agent.cookie_jar.load(domain: "example.com", path: "/mypath", name: "myCookie")
Please note that these options correpond to the fields of the HTTP::Cookie class, an inherited dependency from Mechanize.
This gem depends on:
- Fork it ( https://github.com/cedarcode/mechanize=cookie_store/ )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
To run the test suite, make sure you have Redis server running in localhost:6379
. Then just run bundle exec rake
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Mechanize::CookieStore project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.