저는 레일스에 웹 사이트를 작성 중이며 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
내 관심사는 사이트가 오류 또는 그냥 "충돌"를 생성 할 것입니다 소스는 어떤 이유로 사용할 수없는 경우. 어떻게 자신을 보호 할 수 있습니까?
이 정확한 오류입니다 : 당신은이 같은 외부 요청을 처리 할 수 있습니다,
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
나는 URI 파서가 작동하도록 할 수 없었지만, 구조 비트를 추가 했으므로 오류를 처리하는 것처럼 보입니다. 감사합니다. – Frank