Skip to content

Commit

Permalink
Merge pull request #167 from djdefi/add-backup-feeds
Browse files Browse the repository at this point in the history
Add backup feeds for RSS
  • Loading branch information
djdefi authored Dec 23, 2024
2 parents c6c8d9e + 81b8130 commit a417ae3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ Optional settings can be configured an Docker run time, or be set in your local
## Docker:
docker run --rm -v rss-firehose:/usr/src/app/public -e "RSS_TITLE=My News" -e "RSS_URLS=https://url1/feed,http://url2/rss" -e "ANALYTICS_UA=UA-XXXXX-Y" -it djdefi/rss-firehose
docker run --rm -v rss-firehose:/usr/src/app/public -e "RSS_TITLE=My News" -e "RSS_URLS=https://url1/feed,http://url2/rss" -e "RSS_BACKUP_URLS=https://backup1/feed,http://backup2/rss" -e "ANALYTICS_UA=UA-XXXXX-Y" -it djdefi/rss-firehose
## Ruby:
export RSS_URLS="https://url1/feed,http://url2/rss"
export RSS_BACKUP_URLS="https://backup1/feed,http://backup2/rss"
ruby render.rb
```
Expand All @@ -60,6 +61,7 @@ Available environment variable options:
```
"ANALYTICS_UA=UA-XXXXX-Y"
"RSS_URLS=https://url1/feed,http://url2/rss"
"RSS_BACKUP_URLS=https://backup1/feed,http://backup2/rss"
"RSS_TITLE=My News"
"RSS_DESCRIPTION=My really awesome news aggregation page"
```
21 changes: 18 additions & 3 deletions render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@ def title
end

def rss_urls
if ENV['RSS_URLS']
ENV['RSS_URLS'].split(',')
urls = if ENV['RSS_URLS']
ENV['RSS_URLS'].split(',')
else
File.readlines('urls.txt').map(&:chomp)
end
urls + rss_backup_urls
end

def rss_backup_urls
if ENV['RSS_BACKUP_URLS']
ENV['RSS_BACKUP_URLS'].split(',')
else
File.readlines('urls.txt').map(&:chomp)
['https://calmatters.org/feed/']
end
end

Expand Down Expand Up @@ -75,6 +84,12 @@ def feed(url)
rss_content.channel.title = "Feed currently offline: #{url}"
rss_content.channel.link = url
rss_content.channel.description = "The feed from '#{url}' is currently offline or returned no items."
# Switch to backup feed if available
backup_url = rss_backup_urls.find { |backup| backup != url }
if backup_url
puts "Switching to backup feed: #{backup_url}"
return feed(backup_url)
end
end

rss_content
Expand Down
11 changes: 11 additions & 0 deletions test/render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,16 @@ def test_placeholder_message_for_parsing_error
assert_includes output, placeholder_message, "The placeholder message for a parsing error is not correctly inserted."
end

def test_backup_feed_functionality
# Simulate a primary feed failure and verify backup feed is used
primary_feed_url = "http://example.com/primary_feed"
backup_feed_url = "http://example.com/backup_feed"
placeholder_message = "<a href='http://example.com/backup_feed'>http://example.com/backup_feed - 0 items:</a>"
# Run render.rb with the primary feed URL and backup feed URL
`RSS_URLS=#{primary_feed_url} RSS_BACKUP_URLS=#{backup_feed_url} ruby render.rb`
output = File.read('public/index.html')
assert_includes output, placeholder_message, "The backup feed is not correctly used when the primary feed fails."
end

# Additional tests to verify specific content or structure can be added here
end

0 comments on commit a417ae3

Please sign in to comment.