Class: PodcastIndex::Episode

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/podcast_index/episode.rb

Constant Summary collapse

FIND_ONE_ATTRIBUTES =
%i[guid].freeze
FIND_MANY_ATTRIBUTES =
%i[feed_id feed_url podcast_guid live itunes_id person recent].freeze

Class Method Summary collapse

Class Method Details

.find(id, fulltext: nil) ⇒ Object



10
11
12
13
# File 'lib/podcast_index/episode.rb', line 10

def find(id, fulltext: nil)
  response = Api::Episodes.by_id(id: id, fulltext: fulltext)
  from_response(response)
end

.find_by(attributes) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
# File 'lib/podcast_index/episode.rb', line 15

def find_by(attributes)
  match = (attributes.keys & FIND_ONE_ATTRIBUTES)
  raise ArgumentError, "Must supply one of the attributes: #{FIND_ONE_ATTRIBUTES}" unless match.present?
  raise ArgumentError, "Must supply only one of the attributes: #{FIND_ONE_ATTRIBUTES}" if match.length > 1

  send("find_by_#{match.first}", attributes[match.first])
end

.sample(max: nil, lang: nil, categories: [], exclude_categories: [], fulltext: nil) ⇒ Object



31
32
33
34
35
# File 'lib/podcast_index/episode.rb', line 31

def sample(max: nil, lang: nil, categories: [], exclude_categories: [], fulltext: nil)
  response = Api::Episodes.random(max: max, lang: lang, cat: categories.join(","),
                                  notcat: exclude_categories.join(","), fulltext: fulltext)
  from_response_collection(response, "episodes")
end

.where(attributes) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
# File 'lib/podcast_index/episode.rb', line 23

def where(attributes)
  match = (attributes.keys & FIND_MANY_ATTRIBUTES)
  raise ArgumentError, "Must supply one of the attributes: #{FIND_MANY_ATTRIBUTES}" unless match.present?
  raise ArgumentError, "Must supply only one of the attributes: #{FIND_MANY_ATTRIBUTES}" if match.length > 1

  send("find_all_by_#{match.first}", **attributes)
end