is a small wrapper around Mechanize to enable caching for GET requests.
Add this line to your application's Gemfile:
gem 'cached_mechanize'
And then execute:
$ bundle
Or install it yourself as:
$ gem install cached_mechanize
Right now there is only a file based caching backend. There might follow more in the future (redis, etc.).
To configure the backend use this syntax:
CachedMechanize.configure do |config|
config.cache_class = CachedMechanize::CacheBackends::FileCache
config.cache_options = {
path: "/path/to/my/cached_files"
}
end
Now you can use this class like you would use Mechanize:
agent = CachedMechanize.new
doc = agent.get('http://www.imdb.com')
By default all retrieved pages will be cached for one day (86400 seconds). To change this behaviour you can provide an expires_after
option:
doc = agent.get('http://www.imdb.com', expires_after: 42)
Note that the syntax has changed! If you want to provide parameters, additional headers or a referer, you can do it like this:
doc = agent.get('http://www.imdb.com', {
referer: 'http://thatothersite.com',
expires_after: 42
})
- add more backends
- add tests
- Fork it ( http://github.com/wukerplank/cached_mechanize/fork )
- 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 new Pull Request