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.

Class Method Summary collapse

Class Method Details

.available_until(item) ⇒ Object

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



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

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

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

Returns the description, main by default



15
16
17
# File 'lib/poms/fields.rb', line 15

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

.first_image_id(item) ⇒ Object

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



32
33
34
35
# File 'lib/poms/fields.rb', line 32

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



26
27
28
29
# File 'lib/poms/fields.rb', line 26

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

.images(item) ⇒ Object

Returns the images from the hash



20
21
22
# File 'lib/poms/fields.rb', line 20

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

.mid(item) ⇒ Object



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

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.



49
50
51
52
53
54
55
56
57
# File 'lib/poms/fields.rb', line 49

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.



84
85
86
87
88
89
90
# File 'lib/poms/fields.rb', line 84

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.



94
95
96
# File 'lib/poms/fields.rb', line 94

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



75
76
77
# File 'lib/poms/fields.rb', line 75

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

.rev(item) ⇒ Object

Returns the revision from a Poms hash.



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

def rev(item)
  item['_rev'].to_i
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



104
105
106
107
108
# File 'lib/poms/fields.rb', line 104

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



10
11
12
# File 'lib/poms/fields.rb', line 10

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