Class: PodcastIndex::Soundbite

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

Constant Summary collapse

FIND_MANY_ATTRIBUTES =
%i[recent].freeze

Class Method Summary collapse

Class Method Details

.find_all_by_recent(recent:, max: nil) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



17
18
19
20
# File 'lib/podcast_index/soundbite.rb', line 17

def find_all_by_recent(recent:, max: nil) # rubocop:disable Lint/UnusedMethodArgument
  response = Api::Recent.soundbites(max: max)
  from_response_collection(response)
end

.from_response_collection(response, collection_key = "items") ⇒ Object



22
23
24
25
26
27
# File 'lib/podcast_index/soundbite.rb', line 22

def from_response_collection(response, collection_key = "items")
  response[collection_key].map do |item|
    soundbite = item.transform_keys(&:underscore)
    new(JSON.parse(soundbite.to_json, object_class: OpenStruct)) # rubocop:disable Style/OpenStructUse
  end
end

.where(attributes) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
# File 'lib/podcast_index/soundbite.rb', line 9

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