Encoding problem

i’m new to ruby, and having got the basics by now i decided to try and
write a program that fetches RSS feeds and mashes them up into a
burroughs-style cut-up.

i first tested the feedtools gem.

code:

require ‘rubygems’
require ‘feed_tools’

feed = FeedTools::Feed.open(‘Slashdot’)

puts feed.title
puts feed.link
puts feed.description

for item in feed.items
puts item.title
puts item.link
puts item.content
end

then:

tryl ~/ruby tests $ ruby feeds.rb
internal:lib/rubygems/custom_require:29:in `require’:
/usr/lib/ruby/gems/1.9.1/gems/feedtools-0.2.29/lib/feed_tools/helpers/uri_helper.rb:43:
invalid multibyte char (US-ASCII) (SyntaxError)
/usr/lib/ruby/gems/1.9.1/gems/feedtools-0.2.29/lib/feed_tools/helpers/uri_helper.rb:43:
invalid multibyte char (US-ASCII)
/usr/lib/ruby/gems/1.9.1/gems/feedtools-0.2.29/lib/feed_tools/helpers/uri_helper.rb:43:
syntax error, unexpected $end, expecting ‘)’
if IDN::Idna.toASCII(‘http://www.詹姆斯.com/’) ==

etc.

i tried adding “# encoding: utf-8” in the beginning, i made sure the
file is saved utf-8 encoded…

same.

any ideas?

See here:

http://rubyforge.org/tracker/index.php?func=detail&aid=27804&group_id=775&atid=3061


Date: Sat, 30 Jul 2011 02:49:58 +0900
From: [email protected]
Subject: encoding problem
To: [email protected]

i’m new to ruby, and having got the basics by now i decided to try and
write a program that fetches RSS feeds and mashes them up into a
burroughs-style cut-up.

Here’s an alternative that uses a simple xml parsing gem (xml-object),
and the standard net/http library:

require ‘net/http’
require ‘xml-object’

def fetch(uri_str, limit = 10)

You should choose better exception.

raise ArgumentError, ‘HTTP redirect too deep’ if limit == 0

response = Net::HTTP.get_response(URI.parse(uri_str))
case response
when Net::HTTPSuccess then response
when Net::HTTPRedirection then fetch(response[‘location’], limit - 1)
else
response.error!
end
end

xml_data = fetch(“Slashdot”).body

rss = XMLObject.new(xml_data)

puts rss.channel.title
puts rss.channel.link
puts rss.channel.description

rss.items.each do |item|
puts item.title
puts item.link
puts item.description
end

The fetch() method is required because this URL redirects multiple
times, which by the way the correct URL is:

http://rss.slashdot.org/Slashdot/slashdot

xml-object lets you access xml as an object. More details on the
homepage[1]. You might be able to get faster parsing by using
libxml-ruby. This blog has a quick breakdown on performance[2]. I
decided to go with xml-object simply because it matched more closely to
how you were accessing it. Hope that gives you something to work with.

Regards,
Chris W.
Twitter: @cwgem

[1] http://xml-object.rubyforge.org/doc/
[2] Parsing XML with Ruby // RailsTips by John Nunemaker