2012-11-30 53 views
2

在我的数据库Rails应用程序中我有一些查询。如何在没有测试的情况下使用水豚

# PostController 
@post.query # => 'blabla' 

,我希望把这个字符串在谷歌或雅虎

简单的ruby文件我用

require ... all gems 
Capybara.app_host = 'http://www.google.com/' 

class Google 
    include Capybara::DSL 
    def search 
    visit('/') 
    fill_in "q", :with => "blah blah" 
    click_button("Google") 
    ... 

,但我怎么可以把这个代码控制器(或模型方法)?

回答

0
class PostsController < ApplicationController 

require 'capybara' 
require 'capybara/dsl' 

include Capybara::DSL 

Capybara.current_driver = :webkit 

    def index 
    @posts = Post.all 
    end 

    def show 
    @post = Post.find(params[:id]) 
    visit(@post.search_type) 
    fill_in "q", :with => @post.query 
    click_button("Google") 

    loop_variable = 0 
    num_of_page = 1 
    max_count_of_page = 4 
    flag_on_break = false 

    while(loop_variable == 0) do 
     has_on_page = page.has_content?(@post.search_question) 

     if has_on_page 
     loop_variable = 1 
     else 
     find('.b:last a').click 
     num_of_page += 1 
     end 
     if num_of_page > max_count_of_page 
      flag_on_break = true 
      break 
      end 
     end 

     if flag_on_break 
     @number = 'Nothing not found' 
     else 
     @number = num_of_page 
     end 
    end 

    def new 
    @post = Post.new 
    end 

    def create 
    @post = Post.new(params[:post]) 

    if @post.save 
     flash[:notice] = "Post created!" 
     redirect_to root_path 
    end 
    end 
end 

这是一块我的代码 - 但主要的想法,我认为是可以理解的

0

水豚不打算用于此类用途。您可能需要尝试mechanize

+0

我正在寻找如何使用谷歌与水豚搜索(当我有时间 - 我把答案) –

相关问题