保持机械化页面超过请求边界

时间:2012-01-30 11:20:40

标签: ruby-on-rails ruby mechanize

我正在编写一个ruby应用程序,可以代表用户将评论发布到远程博客。我的问题是我必须在控制器的post方法中使用相同的页面,以使会话保持活动状态。填写验证码:

应用/控制器/ comment_controller.rb

require 'mechanize'
class CommentController < ApplicationController
   def new
       agent = Mechanize.new
       @page = agent.get('http://blog.example.com')
       @captcha_src = @page.search("//div[@id='recaptcha_image']").search("//img")[1].attribute("src")
       #etc.
   end

   def post_comment
      # insert captcha, username, password + text into the form
      agent.submit(@page.form[0], @page.form[0].buttons.submitbutton) # Problem: page instance variable doesn't exist anymore
   end
end

我已经尝试在Rails.cache中保存page-instance-variable,但是机械化的页面无法编组为字符串。

1 个答案:

答案 0 :(得分:0)

我写了一个有效的解决方案。它将隐藏变量和cookie保存在base64编码的字符串中,该字符串在隐藏字段中的请求之间进行传输。下面是构建的代码:

require 'mechanize'
require 'stringio'
require 'base64'

class MechanizeWrapper
  attr_reader :page, :agent

  def initialize(url, useproxy = true)
    @agent = Mechanize.new
    @page = @agent.get(url)
  end

  def get_state()
    hidden_fields = {}
    cookie_jar = StringIO.new

    @page.search("//input[@type='hidden']").each do |hidden| 
      hidden_fields[hidden.path]=hidden.attribute('value').to_s
    end

    @agent.cookie_jar.dump_cookiestxt(cookie_jar);

    state = {:hidden_fields => hidden_fields.inspect, :cookie_jar => cookie_jar.string}
    Base64.encode64(state.inspect)
  end

  def put_state(state_enc)
    state = eval(Base64.decode64(state_enc))
    eval(state[:hidden_fields]).each do |path,value|  
      @page.search(path).first['value'] = value
    end    

    cookie_jar = StringIO.new(state[:cookie_jar])
    @agent.cookie_jar.load_cookiestxt(cookie_jar)
  end
end