Module: Poms::Fields

Defined in:
lib/poms/fields.rb

Overview

This module contains functions to extract things from a Poms hash that may be harder to access, or are accessed in multiple places.

Constant Summary collapse

IMAGE_TYPE_PRIORITY =
%w(PROMO_LANDSCAPE PICTURE).freeze

Class Method Summary collapse

Class Method Details

.age_rating(item) ⇒ Object

Returns the NICAM age rating of the item or ALL if no age rating exists



102
103
104
# File 'lib/poms/fields.rb', line 102

def age_rating(item)
  item.fetch('ageRating', 'ALL')
end

.available_until(item) ⇒ Object

Returns the enddate of the publication of an internet vod if present.



63
64
65
66
67
68
69
# File 'lib/poms/fields.rb', line 63

def available_until(item)
  return if item['predictions'].blank?
  internetvod = item['predictions']
    .find { |p| p['platform'] == 'INTERNETVOD' }
  return unless internetvod
  Timestamp.to_datetime(internetvod['publishStop'])
end

.content_ratings(item) ⇒ Object

Returns an array containing zero or more content ratings of the item Possible content ratings are: ANGST, DISCRIMINATIE, DRUGS_EN_ALCOHOL, GEWELD, GROF_TAALGEBRUIK and SEKS



109
110
111
# File 'lib/poms/fields.rb', line 109

def content_ratings(item)
  item.fetch('contentRatings', [])
end

.description(item, type = 'MAIN') ⇒ Object

Returns the description, main by default



17
18
19
# File 'lib/poms/fields.rb', line 17

def description(item, type = 'MAIN')
  value_of_type(item, 'descriptions', type)
end

.first_image_id(item) ⇒ Object

Returns the id of the first image or nil if there are none.



40
41
42
43
# File 'lib/poms/fields.rb', line 40

def first_image_id(item)
  return unless images(item)
  image_id(images(item).first)
end

.image_id(image) ⇒ Object

Extracts the image id from an image hash Expects a hash of just an image from POMS



30
31
32
33
# File 'lib/poms/fields.rb', line 30

def image_id(image)
  return unless image['imageUri']
  image['imageUri'].split(':').last
end

.image_order_index(image) ⇒ Object



35
36
37
# File 'lib/poms/fields.rb', line 35

def image_order_index(image)
  IMAGE_TYPE_PRIORITY.index(image['type']) || IMAGE_TYPE_PRIORITY.size
end

.images(item) ⇒ Object

Returns the images from the hash



22
23
24
25
26
# File 'lib/poms/fields.rb', line 22

def images(item)
  item['images'].try(:sort_by) do |i|
    image_order_index(i)
  end
end

.mid(item) ⇒ Object



45
46
47
# File 'lib/poms/fields.rb', line 45

def mid(item)
  item['mid']
end

.odi_streams(item) ⇒ Object

Returns an array of odi stream types. Note: this code is copied from Broadcast and it is assumed it was working there.



52
53
54
55
56
57
58
59
60
# File 'lib/poms/fields.rb', line 52

def odi_streams(item)
  locations = item['locations']
  return [] if locations.nil? || locations.empty?
  odi_streams = locations.select { |l| l['programUrl'].match(/^odi/) }
  streams = odi_streams.map do |l|
    l['programUrl'].match(%r{^[\w+]+\:\/\/[\w\.]+\/video\/(\w+)\/\w+})[1]
  end
  streams.uniq
end

.parent(item, midref: nil) ⇒ Object

Finds a parent the data is “member of”. If :midref is given, it will look for the parent that matches that mid and return nil if not found. Without the :midref it will return the first parent.

Parameters:

  • item

    The Poms Hash

  • optional

    midref The midref of parent we seek.



87
88
89
90
91
92
93
# File 'lib/poms/fields.rb', line 87

def parent(item, midref: nil)
  if midref
    parents(item).find { |parent| parent['midRef'] == midref }
  else
    parents(item).first
  end
end

.parents(item) ⇒ Object

Returns the parents that the element is member of. Will always return an array.



97
98
99
# File 'lib/poms/fields.rb', line 97

def parents(item)
  Array(item['memberOf'])
end

.position(item, member_of: nil) ⇒ Object

Returns the index at which it is in the parent. When no :member_of keyword is given, it will return the first found index. Else, when a parent is found with matching member_of midref, it returns that index. Else returns nil. seek the index

Parameters:

  • item

    The Poms Hash

  • optional

    :member_of The midref of parent for which we



78
79
80
# File 'lib/poms/fields.rb', line 78

def position(item, member_of: nil)
  parent(item, midref: member_of).try(:[], 'index')
end

.schedule_events(item) ⇒ Object

Returns an array of start and end times for the scheduled events for this item. It returns an empty array if no events are found. You can pass in a block to filter the events on data that is not returned, like channel.

Parameters:

  • item

    The Poms hash



119
120
121
122
123
# File 'lib/poms/fields.rb', line 119

def schedule_events(item)
  events = item.fetch('scheduleEvents', [])
  events = yield(events) if block_given?
  events.map { |event| hash_event(event) }
end

.title(item, type = 'MAIN') ⇒ Object

Returns the title, main by default



12
13
14
# File 'lib/poms/fields.rb', line 12

def title(item, type = 'MAIN')
  value_of_type(item, 'titles', type)
end