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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/podrb/commands/add/runner.rb', line 9
def call(feed, options = {})
parsed_feed = Infrastructure::FeedParser.call(feed)
parsed_options = parse_options(options)
if missing_data?(parsed_feed)
return build_failure_response(details: :badly_formatted)
end
db = Infrastructure::Storage::SQL.new(db: podrb_db_dir)
db.transaction do
podcast = parsed_feed.podcast
podcast_feed = parsed_options["sync_url"] || podcast.feed
return build_failure_response(details: :missing_sync_url) if podcast_feed.nil?
db.execute <<-SQL
insert into podcasts
(name, description, feed, website)
values (
"#{escape_double_quotes(podcast.name)}",
"#{escape_double_quotes(podcast.description)}",
"#{escape_double_quotes(podcast_feed)}",
"#{escape_double_quotes(podcast.website)}"
);
SQL
inserted_podcast_id = db.query("select id from podcasts order by id desc limit 1;").first.id
parsed_feed.episodes.each do |e|
db.execute <<-SQL
insert into episodes
(title, release_date, podcast_id, duration, link, external_id)
values (
"#{escape_double_quotes(e.title)}",
"#{escape_double_quotes(e.release_date)}",
#{inserted_podcast_id},
"#{escape_double_quotes(e.duration)}",
"#{escape_double_quotes(e.link)}",
"#{e.external_id}"
);
SQL
end
end
build_success_response(details: :successfully_added)
rescue Infrastructure::Storage::Exceptions::ConstraintViolation
build_failure_response(details: :already_added)
end
|