# frozen_string_literal: true require_relative 'base' require_relative 'tag_group' class WCC::Media::Message < WCC::Media::Base active_record_shim do endpoint 'messages' filters %w[ on_or_after_date on_or_before_date tag_id playlist_id speaker_id scripture_book_id series_id title_like legacy_id hide_from_internal_search ].freeze end %w[ date duration title subtitle description series_position slug campus_names topic topics hide_from_internal_search archived ].each do |att| define_method att do raw[att] end end def hide_from_internal_search raw['flags']['hide_from_internal_search'] || false end def archived? raw['flags']['archived'] || false end def speakers (raw['speakers'] || []).map { |val| WCC::Media::Speaker.new(val) } end def playlists (raw['playlists'] || []).map { |val| WCC::Media::Playlist.new(val) } end def tags (raw['tags'] || []).map { |val| WCC::Media::Tag.new(val) } end %w[ scripture_references downloads ].each do |att| define_method att do (raw[att] || []).map { |val| OpenStruct.new(val) } end end def series WCC::Media::Series.new(raw['series']) if raw['series'] end %w[ meta embeds flags assets images language external_urls transcript sermon_guide _clips _formatted _stats ].each do |att| define_method att do OpenStruct.new(raw[att]) if raw[att] end end def has_video? # rubocop:disable Naming/PredicateName assets.each_pair .any? { |k, v| /video/.match(k) && v && !v.empty? } end def has_audio? # rubocop:disable Naming/PredicateName assets.each_pair .any? { |k, v| /^audio/.match(k) && v && !v.empty? } end def to_param "#{id}-#{slug}" end WCC::Media::TagGroup.all.each do |tag_group| method_name = tag_group.name.downcase.to_sym # except :meta (different definition above) next if instance_methods.include?(method_name) define_method(method_name) do tags.select { |t| t.tag_group == tag_group } end end # pluralize "campus" specifically alias campuses campus remove_method :campus end