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



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

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.



69
70
71
72
73
74
75
# File 'lib/poms/fields.rb', line 69

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

.broadcasters(item) ⇒ Object



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

def broadcasters(item)
  Array(item['broadcasters']).map do |key_value_pair|
    key_value_pair['value']
  end
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



115
116
117
# File 'lib/poms/fields.rb', line 115

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.



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

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



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

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

.image_order_index(image) ⇒ Object



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

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



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

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

.mid(item) ⇒ Object



51
52
53
# File 'lib/poms/fields.rb', line 51

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.



58
59
60
61
62
63
64
65
66
# File 'lib/poms/fields.rb', line 58

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.



93
94
95
96
97
98
99
# File 'lib/poms/fields.rb', line 93

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.



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

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



84
85
86
# File 'lib/poms/fields.rb', line 84

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



125
126
127
128
129
# File 'lib/poms/fields.rb', line 125

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