Skip to content

Commit fe7adc8

Browse files
committed
Merge pull request #76 from id774/issue66
Refactoring plugin Subscription::Text
2 parents 2ab07a9 + 5b1cd3c commit fe7adc8

4 files changed

Lines changed: 30 additions & 34 deletions

File tree

lib/automatic/feed_parser.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,27 @@ module FeedParser
1212
require 'uri'
1313
require 'nokogiri'
1414

15+
class FeedObject
16+
attr_accessor :title, :link, :description, :author, :comments
17+
def initialize
18+
@link = 'http://dummy'
19+
@title = 'dummy'
20+
@description = ''
21+
@author = ''
22+
@comments = ''
23+
end
24+
end
25+
26+
def self.generate_feed(feed)
27+
feed_object = FeedObject.new
28+
feed_object.title = feed['title'] unless feed['title'].nil?
29+
feed_object.link = feed['url'] unless feed['url'].nil?
30+
feed_object.description = feed['description'] unless feed['description'].nil?
31+
feed_object.author = feed['author'] unless feed['author'].nil?
32+
feed_object.comments = feed['comments'] unless feed['comments'].nil?
33+
feed_object
34+
end
35+
1536
def self.get(url)
1637
begin
1738
unless url.nil?

plugins/subscription/text.rb

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,68 +8,43 @@
88
# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0.
99

1010
module Automatic::Plugin
11-
class TextFeed
12-
attr_accessor :title, :link, :description, :author, :comments
13-
14-
def initialize
15-
@link = 'http://dummy'
16-
@title = 'dummy'
17-
@description = ''
18-
@author = ''
19-
@comments = ''
20-
end
21-
end
22-
2311
class SubscriptionText
24-
2512
def initialize(config, pipeline=[])
2613
@config = config
2714
@pipeline = pipeline
15+
@return_feeds = []
2816
end
2917

3018
def run
3119
create_feed
32-
33-
if @dummyfeeds != []
34-
@pipeline << Automatic::FeedParser.create(@dummyfeeds)
35-
end
20+
@pipeline << Automatic::FeedParser.create(@return_feeds) unless @return_feeds.length == 0
3621
@pipeline
3722
end
3823

3924
private
4025

41-
def generate_textfeed(feed)
42-
textFeed = TextFeed.new
43-
textFeed.title = feed['title'] unless feed['title'].nil?
44-
textFeed.link = feed['url'] unless feed['url'].nil?
45-
textFeed.description = feed['description'] unless feed['description'].nil?
46-
textFeed.author = feed['author'] unless feed['author'].nil?
47-
textFeed.comments = feed['comments'] unless feed['comments'].nil?
48-
@dummyfeeds << textFeed
49-
end
50-
5126
def create_feed
5227
unless @config.nil?
5328
@dummyfeeds = []
5429
unless @config['titles'].nil?
5530
@config['titles'].each {|title|
5631
feed = {}
5732
feed['title'] = title
58-
generate_textfeed(feed)
33+
@return_feeds << Automatic::FeedParser.generate_feed(feed)
5934
}
6035
end
6136

6237
unless @config['urls'].nil?
6338
@config['urls'].each {|url|
6439
feed = {}
6540
feed['url'] = url
66-
generate_textfeed(feed)
41+
@return_feeds << Automatic::FeedParser.generate_feed(feed)
6742
}
6843
end
6944

7045
unless @config['feeds'].nil?
7146
@config['feeds'].each {|feed|
72-
generate_textfeed(feed)
47+
@return_feeds << Automatic::FeedParser.generate_feed(feed)
7348
}
7449
end
7550

@@ -80,7 +55,7 @@ def create_feed
8055
feed = {}
8156
feed['title'], feed['url'], feed['description'], feed['author'],
8257
feed['comments'] = line.force_encoding("utf-8").strip.split("\t")
83-
generate_textfeed(feed)
58+
@return_feeds << Automatic::FeedParser.generate_feed(feed)
8459
end
8560
end
8661
}

spec/fixtures/sampleFeeds.tsv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
testTitle http://dummy.from.file testDescription testComments
1+
testTitle http://dummy.from.file testDescription testAuthor testComments

spec/fixtures/sampleFeeds2.tsv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
testTitle1 http://dummy.from.file1 testDescription1 testComments1
2-
testTitle2 http://dummy.from.file2 testDescription2 testComments2
1+
testTitle1 http://dummy.from.file1 testDescription1 testComments1
2+
testTitle2 http://dummy.from.file2 testDescription2 testAuthor2

0 commit comments

Comments
 (0)