Rails - rake任务中的访问URL - 套接字错误

时间:2016-12-19 15:04:55

标签: ruby-on-rails ruby rake

我试图创建一个访问一系列URL的rake任务,记录状态代码,然后将它们打印到控制台。我的代码如下:

import Foundation
let myArray = ["a", "b", "c", "a", "b", "a", "b", "c", "a", "b"]
let countedSet = NSCountedSet(array: myArray)
let count = countedSet.count(for: "a")
print(count) // 4

// or, simply
let count = NSCountedSet(array: myArray).count(for: "a")

我从终端运行时收到以下错误代码:

require 'csv'
require 'net/http'
require "open-uri"

namespace :routes do
  desc "check status codes"
  task check_301s: :environment do
    # open_csv
    # edit_urls
    generate_routes
    # visit_urls
    # log_status_codes
    visit_and_log_routes
    # give_results
  end

  def generate_routes
    csv = CSV.open('lib/better.csv', headers: true)
    @urls = []
    csv.each do |row|
      @urls << row['url'].gsub('foo', 'localhost:3000')
    end
  end

  def visit_and_log_routes
    responses = []
    @urls.each do |url|
      http = Net::HTTP.new(url, 3000)
      response = http.request_get('/')
      responses << response
    end
    puts responses
  end
end

我不确定是否有更简单的方法来访问网址并记录其状态代码,如果是,请告诉我如何操作,如果没有,那么我如何更正此错误消息。我假设我只是遗漏了相关选项,但我不确定如何/在哪里添加它们。

1 个答案:

答案 0 :(得分:0)

对于任何造成同样令人印象深刻的错误的人来说......路线需要http://而不仅仅是localhost:3000