cookie_file=File.join(File.dirname(__FILE__),"cookies.yml")w=Mechanize.newifFile.exists?(cookie_file)w.cookie_jar.loadcookie_fileendw.get("http://some_url")# or whatever logic to verify whether you need to authenticate or notifw.page.forms.count!=0form=w.page.forms.firstemail_field=form.field("admin_user[email]")pwd=form.field("admin_user[password]")email_field.value="your_username@your_domain.com"pwd.value="password"form.submitendw.cookie_jar.save(cookie_file,session: true)