Webratをrequireし、Webrat::Methodsをincludeすることで、いつものように快適にウェッブサーフィンを行うことができます。
require 'pry'
require 'webrat'
require 'mechanize'
module StageSurfer
class Core
Webrat.configure do |config|
config.mode = :mechanize
end
include Webrat::Methods
def initialize login_page_url, user_id, password
visit login_page_url
#下記はログインページの作りによって変える必要がある!
fill_in "id", :with => user_id
fill_in "password", :with => password
click_button "submit"
end
def surf target_urls
target_urls.each {|url| yield(url, visit(url))}
end
end
end
urls = STDIN.each_line.map {|line| line.chomp }
proc = eval("lambda {|url, page| #{ARGV[3]}}")
StageSurfer::Core.new(ARGV[0], ARGV[1], ARGV[2]).surf(urls, &proc)
上記のクラスを使って、http://your_site/contents_pageにContent-typeという文言が存在する場合は"o"を、なければ"x"を出すという場合は、下記のように呼び出します。
echo http://your_site/contents_page | ruby stage_surf.rb http://your_site/login_page id password 'puts "#{url}\t#{/Content-type/ =~ page.content ? :o : :x}"'