Skip to content

Commit f6da7ef

Browse files
committed
Merge pull request #69 from id774/issue66
Add function to read text from files
2 parents a51145d + 6eea895 commit f6da7ef

5 files changed

Lines changed: 85 additions & 21 deletions

File tree

plugins/subscription/text.rb

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,45 +25,68 @@ class SubscriptionText
2525
def initialize(config, pipeline=[])
2626
@config = config
2727
@pipeline = pipeline
28+
end
29+
30+
def run
31+
create_feed
32+
33+
if @dummyfeeds != []
34+
@pipeline << Automatic::FeedParser.create(@dummyfeeds)
35+
end
36+
@pipeline
37+
end
38+
39+
private
2840

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+
51+
def create_feed
2952
unless @config.nil?
3053
@dummyfeeds = []
3154
unless @config['titles'].nil?
3255
@config['titles'].each {|title|
33-
textFeed = TextFeed.new
34-
textFeed.title = title
35-
@dummyfeeds << textFeed
56+
feed = {}
57+
feed['title'] = title
58+
generate_textfeed(feed)
3659
}
3760
end
3861

3962
unless @config['urls'].nil?
4063
@config['urls'].each {|url|
41-
textFeed = TextFeed.new
42-
textFeed.link = url
43-
@dummyfeeds << textFeed
64+
feed = {}
65+
feed['url'] = url
66+
generate_textfeed(feed)
4467
}
4568
end
4669

4770
unless @config['feeds'].nil?
4871
@config['feeds'].each {|feed|
49-
textFeed = TextFeed.new
50-
textFeed.title = feed['title'] unless feed['title'].nil?
51-
textFeed.link = feed['url'] unless feed['url'].nil?
52-
textFeed.description = feed['description'] unless feed['description'].nil?
53-
textFeed.author = feed['author'] unless feed['author'].nil?
54-
textFeed.comments = feed['comments'] unless feed['comments'].nil?
55-
@dummyfeeds << textFeed
72+
generate_textfeed(feed)
5673
}
5774
end
5875

76+
unless @config['files'].nil?
77+
@config['files'].each {|f|
78+
open(File.expand_path(f)) do |file|
79+
file.each_line do |line|
80+
feed = {}
81+
feed['title'], feed['url'], feed['description'], feed['author'],
82+
feed['comments'] = line.force_encoding("utf-8").strip.split("\t")
83+
generate_textfeed(feed)
84+
end
85+
end
86+
}
87+
end
5988
end
6089
end
6190

62-
def run
63-
if @dummyfeeds != []
64-
@pipeline << Automatic::FeedParser.create(@dummyfeeds)
65-
end
66-
@pipeline
67-
end
6891
end
6992
end

spec/fixtures/sampleFeeds.tsv

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

spec/fixtures/sampleFeeds2.tsv

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

spec/plugins/subscription/text_spec.rb

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# -*- coding: utf-8 -*-
22
# Name:: Automatic::Plugin::Subscription::Text
33
# Author:: soramugi <http://soramugi.net>
4+
# 774 <http://id774.net>
45
# Created:: May 6, 2013
5-
# Updated:: May 6, 2013
6-
# Copyright:: Copyright (c) 2012-2013 Automatic Ruby Developers.
6+
# Updated:: Feb 19, 2014
7+
# Copyright:: Copyright (c) 2012-2014 Automatic Ruby Developers.
78
# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0.
89

910
require File.expand_path(File.dirname(__FILE__) + '../../../spec_helper')
@@ -51,4 +52,33 @@
5152
its(:run) { should have(1).feed }
5253
end
5354

55+
context "with feeds including full fields whose return feed" do
56+
subject {
57+
Automatic::Plugin::SubscriptionText.new(
58+
{ 'feeds' => [ {'title' => 'huge', 'url' => "http://hugehuge", 'description' => "aaa", 'comments' => "bbb", 'author' => "ccc" } ] }
59+
)
60+
}
61+
62+
its(:run) { should have(1).feed }
63+
end
64+
65+
context "with file whose return feed" do
66+
subject {
67+
Automatic::Plugin::SubscriptionText.new(
68+
{ 'files' => ["spec/fixtures/sampleFeeds.tsv"] }
69+
)
70+
}
71+
72+
its(:run) { should have(1).feed }
73+
end
74+
75+
context "with files whose return feed" do
76+
subject {
77+
Automatic::Plugin::SubscriptionText.new(
78+
{ 'files' => ["spec/fixtures/sampleFeeds.tsv", "spec/fixtures/sampleFeeds2.tsv"] }
79+
)
80+
}
81+
82+
its(:run) { should have(1).feed }
83+
end
5484
end

test/integration/test_text2feed.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ plugins:
1919
-
2020
title: 'Tumblr'
2121
url: 'http://www.tumblr.com/dashboard'
22+
description: 'aaa'
23+
comments: 'bbb'
24+
author: 'ccc'
25+
files:
26+
- 'spec/fixtures/sampleFeeds.tsv'
27+
files:
28+
- 'spec/fixtures/sampleFeeds.tsv'
29+
- 'spec/fixtures/sampleFeeds2.tsv'
2230

2331
- module: StorePermalink
2432
config:

0 commit comments

Comments
 (0)