检查源是否存在

发布于 2024-10-30 20:18:22 字数 551 浏览 0 评论 0原文

我正在用 Rails 编写一个网站,并且需要查看 RSS 提要。该脚本按预期工作,但我刚刚收到一条错误,表明无法读取源代码。

这是脚本:

def setup_feed
  source = "http://path_to_feed/"
  content = "" # raw content of rss feed will be loaded here

  open(source) do |s|
    content = s.read
  end
  @rss = RSS::Parser.parse(content, false)
end

我担心的是,如果源出于某种原因不可用,该站点将产生错误或只是“崩溃”。我该如何保护自己免受这种情况的侵害?

这是确切的错误:

Errno::ENOENT in WelcomeController#index

No such file or directory - http://path_to_feed/

I'm writing a website in Rails and I need to reed a RSS feed. The script works as intended, but I just got an error indicating that the source could not be read.

This is the script:

def setup_feed
  source = "http://path_to_feed/"
  content = "" # raw content of rss feed will be loaded here

  open(source) do |s|
    content = s.read
  end
  @rss = RSS::Parser.parse(content, false)
end

My concern is that the site will produce an error or just "crash" if the source isn't available for whatever reasons. How can I protect myself against this?

This is the exact error:

Errno::ENOENT in WelcomeController#index

No such file or directory - http://path_to_feed/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

早茶月光 2024-11-06 20:18:22

编辑
找到了更新的链接:http://binarysoul.com/blog/rails-3-url -Eric Himmelreich 的验证

class Example
  include ActiveModel::Validations

  ##
  # Validates a URL
  #
  # If the URI library can parse the value, and the scheme is valid
  # then we assume the url is valid
  #
  class UrlValidator < ActiveModel::EachValidator
    def validate_each(record, attribute, value)
      begin
        uri = Addressable::URI.parse(value)

        if !["http","https","ftp"].include?(uri.scheme)
          raise Addressable::URI::InvalidURIError
        end
      rescue Addressable::URI::InvalidURIError
        record.errors[attribute] << "Invalid URL"
      end
    end
  end

  validates :field, :url => true
end

EDIT
Found a more recent link: http://binarysoul.com/blog/rails-3-url-validation by Eric Himmelreich

class Example
  include ActiveModel::Validations

  ##
  # Validates a URL
  #
  # If the URI library can parse the value, and the scheme is valid
  # then we assume the url is valid
  #
  class UrlValidator < ActiveModel::EachValidator
    def validate_each(record, attribute, value)
      begin
        uri = Addressable::URI.parse(value)

        if !["http","https","ftp"].include?(uri.scheme)
          raise Addressable::URI::InvalidURIError
        end
      rescue Addressable::URI::InvalidURIError
        record.errors[attribute] << "Invalid URL"
      end
    end
  end

  validates :field, :url => true
end
遮云壑 2024-11-06 20:18:22

通常,您需要在后台处理这样的外部请求。

这里提供一个简单的示例有点复杂,但主要步骤是:

  1. 创建一个模型来存储来自 feed 的数据
  2. 编写一个 rake 任务来执行请求,并将结果保存到您的模型中
  3. 设置一个要运行的 cron 作业rake 任务定期
  4. 在页面上显示模型的内容,

这样,页面加载仅依赖于您自己的数据库,而不依赖于您可能无法控制的任何外部资源。

Generally, you'll want to handle external requests like this in the background.

It's a bit too involved to provide a simple example here, but the main steps are:

  1. Create a model to store the data from your feed
  2. Write a rake task to perform the request, and save the result into your model
  3. Setup a cron job to run the rake task periodically
  4. Display the contents of your model on the page

This way, page loads are dependent only on your own database, not on any external resources that you may not control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文