DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Seamlessly Return A String From Http Or Https Feeds
// This class uses URI module to detect regular or secure links, and returns the response as a string, in my case to pass onto a feed parser like simple-rss. I'm working on a simple http authentication addon.
#beef.
require 'net/http'
require 'net/https'
require 'simple-rss'
class SeamlessFeed
def initialize(url,user=nil,password=nil)
@url = URI.parse(url)
end
def output
if self.is_secure?
http = Net::HTTP.new(@url.host, 443)
http.use_ssl = true
http.start do |http|
request = Net::HTTP::Get.new(@url.path)
@response = http.request(request)
@response.value
end
else
@response = Net::HTTP.get_response(@url) #Why can't they all be like this, eh?
end
return @response.body
end
def is_readable?
feed = SimpleRSS.parse(self.output)
return true unless feed.channel.items.size < 1
rescue SimpleRSSError
return false
end
protected
def is_secure?
@url.scheme == 'https'
end
end




