Skip to content

Commit 28a4b6a

Browse files
1s22s11s22s1
andauthored
Simplify sample code (#59)
Because the feed can be read without using open-uri, I rewrite the sample code for the purpose of making it simpler. Co-authored-by: 1s22s1 <1s22s1@users.noreply.github.com>
1 parent 8a5b056 commit 28a4b6a

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

README.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,16 @@ Or install it yourself as:
3030

3131
### Consuming RSS
3232

33-
If you'd like to read someone's RSS feed with your Ruby code, you've come to the right place. It's really easy to do this, but we'll need the help of open-uri:
33+
If you'd like to read someone's RSS feed with your Ruby code, you've come to the right place. It's really easy to do this:
3434

3535
```ruby
36-
require 'rss'
37-
require 'open-uri'
38-
39-
url = 'https://www.ruby-lang.org/en/feeds/news.rss'
40-
URI.open(url) do |rss|
41-
feed = RSS::Parser.parse(rss)
42-
puts "Title: #{feed.channel.title}"
43-
feed.items.each do |item|
44-
puts "Item: #{item.title}"
45-
end
46-
end
36+
require 'rss'
37+
38+
feed = RSS::Parser.parse('https://www.ruby-lang.org/en/feeds/news.rss')
39+
puts "Title: #{feed.channel.title}"
40+
feed.items.each do |item|
41+
puts "Item: #{item.title}"
42+
end
4743
```
4844

4945
As you can see, the workhorse is RSS::Parser#parse, which takes the source of the feed and a parameter that performs validation on the feed. We get back an object that has all of the data from our feed, accessible through methods. This example shows getting the title out of the channel element, and looping through the list of items.

lib/rss.rb

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,14 @@
1919
# == Consuming RSS
2020
#
2121
# If you'd like to read someone's RSS feed with your Ruby code, you've come to
22-
# the right place. It's really easy to do this, but we'll need the help of
23-
# open-uri:
22+
# the right place. It's really easy to do this.
2423
#
2524
# require 'rss'
26-
# require 'open-uri'
27-
#
28-
# url = 'http://www.ruby-lang.org/en/feeds/news.rss'
29-
# URI.open(url) do |rss|
30-
# feed = RSS::Parser.parse(rss)
31-
# puts "Title: #{feed.channel.title}"
32-
# feed.items.each do |item|
33-
# puts "Item: #{item.title}"
34-
# end
25+
#
26+
# feed = RSS::Parser.parse('https://www.ruby-lang.org/en/feeds/news.rss')
27+
# puts "Title: #{feed.channel.title}"
28+
# feed.items.each do |item|
29+
# puts "Item: #{item.title}"
3530
# end
3631
#
3732
# As you can see, the workhorse is RSS::Parser#parse, which takes the source of

0 commit comments

Comments
 (0)