Class: Epodder::Add

Inherits:
Verb show all
Defined in:
lib/verb/add.rb

Instance Method Summary collapse

Methods inherited from Verb

#add_command, #each_argument, #lookup_args, #verb_struct

Constructor Details

#initializeAdd

Returns a new instance of Add.



3
4
5
6
7
# File 'lib/verb/add.rb', line 3

def initialize
    require 'feedme'
    require 'mechanize'
    @mechanize = Mechanize.new
end

Instance Method Details

#add(args) ⇒ Object



9
10
11
12
13
# File 'lib/verb/add.rb', line 9

def add(args)
    args.each do |url|
        lookup_podcast url
    end
end

#lookup_podcast(url) ⇒ Object



15
16
17
18
19
# File 'lib/verb/add.rb', line 15

def lookup_podcast(url)
    @mechanize.get(url) do |feed|
        save_podcast feed, url
    end
end

#save_podcast(feed, url) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/verb/add.rb', line 21

def save_podcast(feed, url)
    cast = FeedMe.parse feed.body
    cast.emulate_atom!
    podcast = Podcast.first_or_create(
        title: cast.title,
        uri: url
    )
    podcast.save

    podcast.errors.each do |error|
        @log.error error
    end
    puts "#{podcast.id} - #{podcast.title}"
end