From 4dea5f74319a743a88f77f4efe0226054a955a25 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 14 May 2015 20:46:15 +0200 Subject: Separate lib and executable. Add a separate executable, and move the rendering of the feeds into the feed class. --- bin/rss2html | 35 +++++++++++++++++++++++++++++++++++ lib/rss2html.rb | 31 ++----------------------------- lib/rss2html/feed.rb | 24 ++++++++++++++++++++++++ rss2html.gemspec | 3 +++ 4 files changed, 64 insertions(+), 29 deletions(-) create mode 100755 bin/rss2html diff --git a/bin/rss2html b/bin/rss2html new file mode 100755 index 0000000..b7be920 --- /dev/null +++ b/bin/rss2html @@ -0,0 +1,35 @@ +#!/usr/bin/env ruby +# +# A Simple RSS reader +# Copyright (C) 2015 Harald Eilertsen +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +require 'rss2html' +require 'yaml' + +config_file = File.expand_path('~/.config/feeds.yml') + +unless File.exists?(config_file) + puts "Config file #{config_file} not found. Please create it!" + exit +end + +feeds = YAML.load(IO.read(config_file)) +items = [] + +feeds.each do |t, f| + feed = Feed.new(t, f) + puts feed.render +end diff --git a/lib/rss2html.rb b/lib/rss2html.rb index 4890fd4..6d95382 100644 --- a/lib/rss2html.rb +++ b/lib/rss2html.rb @@ -14,32 +14,5 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -require_relative 'lib/feed_item' -require_relative 'lib/feed' - -require 'yaml' -require 'erb' - -config_file = File.expand_path('~/.config/feeds.yml') - -unless File.exists?(config_file) - puts "Config file #{config_file} not found. Please create it!" - exit -end - -feeds = YAML.load(IO.read(config_file)) -items = [] - -feed_header = ERB.new(IO.read('lib/views/feed_header.html.erb')) -feed_footer = ERB.new(IO.read('lib/views/feed_footer.html.erb')) -item_template = ERB.new(IO.read('lib/views/item.html.erb')) - -feeds.each do |t, f| - feed = Feed.new(t, f) - puts feed_header.result(binding) - - feed.fetch do |entry| - item = FeedItem.new(entry) - puts item_template.result(binding) - end -end +require 'rss2html/feed' +require 'rss2html/feed_item' diff --git a/lib/rss2html/feed.rb b/lib/rss2html/feed.rb index a2f3db4..0f3f01e 100644 --- a/lib/rss2html/feed.rb +++ b/lib/rss2html/feed.rb @@ -16,12 +16,17 @@ require 'rss' require 'open-uri' +require 'erb' class Feed attr_reader :title, :url def initialize(title, args) @title, @url = title, args['url'] + + @feed_header = load_template('feed_header.html.erb') + @feed_footer = load_template('feed_footer.html.erb') + @item_template = load_template('item.html.erb') end def fetch @@ -33,4 +38,23 @@ class Feed end end end + + def render + feed = self + output = @feed_header.result(binding) + + fetch do |entry| + item = FeedItem.new(entry) + output += @item_template.result(binding) + end + + output += @feed_footer.result(binding) + end + + private + + def load_template(t) + view_path = File.join(File.dirname(__FILE__), 'views') + ERB.new(IO.read(File.join(view_path, t))) + end end diff --git a/rss2html.gemspec b/rss2html.gemspec index f540feb..fd12b11 100644 --- a/rss2html.gemspec +++ b/rss2html.gemspec @@ -13,4 +13,7 @@ Gem::Specification.new do |s| lib/rss2html/views/feed_header.html.erb lib/rss2html/views/item.html.erb } + s.executables = %w{ + bin/rss2html + } end -- cgit v1.2.3