Class: PodcastIndex::Podcast

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

Constant Summary collapse

FIND_ONE_ATTRIBUTES =
%i[feed_url guid itunes_id].freeze
FIND_MANY_ATTRIBUTES =
%i[tag medium term title trending dead recent new newly_found].freeze

Class Method Summary collapse

Class Method Details

.find(id) ⇒ Object



11
12
13
14
15
16
# File 'lib/podcast_index/podcast.rb', line 11

def find(id)
  response = Api::Podcasts.by_feed_id(id: id)
  raise PodcastIndex::PodcastNotFound, response["description"] if response["feed"].empty?

  from_response(response)
end

.find_by(attributes) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
# File 'lib/podcast_index/podcast.rb', line 18

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

.where(attributes) ⇒ Object

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
# File 'lib/podcast_index/podcast.rb', line 26

def where(attributes)
  return find_all_music_by_term(**attributes) if attributes[:medium] == "music" && attributes[:term].present?

  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