Class: Jekyll::Podcast::EpisodeData

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/podcast/episode_data.rb

Overview

Class responsible for setting episode data on a post’s page

Instance Method Summary collapse

Constructor Details

#initialize(page, payload) ⇒ EpisodeData

Returns a new instance of EpisodeData.



8
9
10
11
12
# File 'lib/jekyll/podcast/episode_data.rb', line 8

def initialize(page, payload)
  @site = page.site
  @page = payload['page']
  @file_path = File.join(Jekyll::Podcast::Utils.episodes_dir(@site), @page['podcast']['file'])
end

Instance Method Details

#add_episode_dataObject



14
15
16
17
18
19
# File 'lib/jekyll/podcast/episode_data.rb', line 14

def add_episode_data
  @page['podcast'].merge!({ 'size' => size,
                            'size_in_megabytes' => size_in_megabytes,
                            'duration' => duration(seconds),
                            'guid' => guid })
end

#duration(seconds) ⇒ Object



41
42
43
44
# File 'lib/jekyll/podcast/episode_data.rb', line 41

def duration(seconds)
  format('%<hours>d:%<minutes>02d:%<seconds>02d',
         Jekyll::Podcast::Utils.duration(seconds))
end

#guidObject



46
47
48
# File 'lib/jekyll/podcast/episode_data.rb', line 46

def guid
  @page['podcast']['guid'] || "#{@site.config['url']}#{@page['url']}"
end

#secondsObject



33
34
35
36
37
38
39
# File 'lib/jekyll/podcast/episode_data.rb', line 33

def seconds
  if File.exist? @file_path
    Mp3Info.open(@file_path, &:length)
  else
    0
  end
end

#sizeObject



21
22
23
24
25
26
27
# File 'lib/jekyll/podcast/episode_data.rb', line 21

def size
  if File.exist? @file_path
    File.size(@file_path)
  else
    0
  end
end

#size_in_megabytesObject



29
30
31
# File 'lib/jekyll/podcast/episode_data.rb', line 29

def size_in_megabytes
  "#{(size / 1_000_000.0).round(1)} MB"
end