Sha256: a87e8006627fb900e9d5d958747eb66d9d91dd9367779467639017cf8d3f21f6

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 KB

Contents

require "ostruct"
require "delegate"

module PodcastIndex
  class Soundbite < SimpleDelegator
    class << self
      FIND_MANY_ATTRIBUTES = %i[recent].freeze

      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

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

      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
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
podcast_index-0.5.0 lib/podcast_index/soundbite.rb
podcast_index-0.4.0 lib/podcast_index/soundbite.rb
podcast_index-0.3.0 lib/podcast_index/soundbite.rb
podcast_index-0.2.1 lib/podcast_index/soundbite.rb
podcast_index-0.2.0 lib/podcast_index/soundbite.rb