Class: Item::VideoRetriever

Inherits:
BaseService show all
Defined in:
app/services/item/video_retriever.rb

Overview

Retrieves all the videos associated with a particular item. Queries main video, product line main video,
video library for video tagged directly to the item or to the product line and its ancestor

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Methods inherited from BaseService

#log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger

Constructor Details

#initialize(options = {}) ⇒ VideoRetriever

==== Initialization options (pass as a hash)

  • +:tags+ - filter all video library videos retrieval by these tags, specify one or many to query on the presence of any of these tags
  • +:categories+ - filter all video by categories
  • +:series+ - filter all video by series
  • +:item_videos_query_limit+ - how many videos max to retrieve from the item library, default to 10
  • +:product_line_query_limit+ - how many videos max to retrieve from the item library linked to the product line or its ancestor, default is 5
  • +:max_videos+ - At max, return only this number of videos
  • +:ignore_individual_query_limits+ true/false, default: false, if true then all default limits are ignored
    ==== Examples

ir = Item::VideoRetriever.new(tags: 'for-product-page', item_videos_query_limit: 20)



23
24
25
# File 'app/services/item/video_retriever.rb', line 23

def initialize(options={})
  super(options)
end

Instance Method Details

#ignore_individual_query_limits?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'app/services/item/video_retriever.rb', line 47

def ignore_individual_query_limits?
  options[:ignore_individual_query_limits].present?
end

#item_videos_query(item) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'app/services/item/video_retriever.rb', line 51

def item_videos_query(item)
  item_videos = Video.active.by_item_ids(item.id)
  item_videos = item_videos.tagged_with(options[:tags]) if options[:tags].present?
  item_videos = item_videos.where(category: options[:categories]) if options[:categories].present?
  item_videos = item_videos.where(series: options[:series]) if options[:series].present?
  item_videos = item_videos.preload(:poster_image)
  item_videos = item_videos.order(Arel.sql('coalesce(digital_assets.air_date, digital_assets.created_at) desc nulls last'))
  item_videos = item_videos.limit(item_videos_query_limit) if item_videos_query_limit
  item_videos.to_a
end

#item_videos_query_limitObject



62
63
64
65
# File 'app/services/item/video_retriever.rb', line 62

def item_videos_query_limit
  return if ignore_individual_query_limits?
  options[:item_videos_query_limit] || 10
end

#process(item) ⇒ Object

Retrieves all product specifications available to a given item

==== Parameters

  • +item+ - The item for which you want to retrieve videos

==== Returns

A Result object with

  • +all_videos+ - an array of videos combined
  • +videos_grouped+ - hash wrapping all videos as a single group (origin grouping removed)


38
39
40
41
42
43
44
45
# File 'app/services/item/video_retriever.rb', line 38

def process(item)
  all_videos = []
  all_videos += item_videos_query(item)
  all_videos += product_line_query(item)
  all_videos = all_videos.compact.uniq
  all_videos = all_videos[0..(options[:max_videos] - 1)] if options[:max_videos]
  Result.new(all_videos: all_videos, videos_grouped: { nil => all_videos })
end

#product_line_query(item) ⇒ Object



67
68
69
70
# File 'app/services/item/video_retriever.rb', line 67

def product_line_query(item)
  return [] unless pl = item.primary_product_line
  ProductLine::VideoRetriever.new(options).process(pl, item.product_category).all_videos
end

#product_line_query_limitObject



72
73
74
75
# File 'app/services/item/video_retriever.rb', line 72

def product_line_query_limit
  return if ignore_individual_query_limits?
  options[:product_line_query_limit] || 10
end