Class: Feed2Email::OPMLImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/feed2email/opml_importer.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ OPMLImporter

Returns a new instance of OPMLImporter.



40
41
42
# File 'lib/feed2email/opml_importer.rb', line 40

def initialize(io)
  @io = io
end

Class Method Details

.import(path, remove = false) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/feed2email/opml_importer.rb', line 5

def self.import(path, remove = false)
  require 'feed2email/feed'

  feeds = open(path) {|f| new(f).feeds }

  imported = 0

  feeds.each do |uri|
    if feed = Feed[uri: uri]
      warn "Feed already exists: #{feed}"
    else
      feed = Feed.new(uri: uri)

      if feed.save(raise_on_failure: false)
        puts "Imported feed: #{feed}"
        imported += 1
      else
        warn "Failed to import feed: #{feed}"
      end
    end
  end

  if remove
    Feed.exclude(uri: feeds).each do |feed|
      if feed.delete
        puts "Removed feed: #{feed}"
      else
        warn "Failed to remove feed: #{feed}"
      end
    end
  end

  imported
end

Instance Method Details

#feedsObject



44
45
46
47
48
# File 'lib/feed2email/opml_importer.rb', line 44

def feeds
  Nokogiri::XML(data).css('opml body outline').map {|outline|
    outline['xmlUrl']
  }.compact
end