Sha256: 6426b38d78710ed23e6ccb4fd3e9a86507d1fcecb73845227d52f2a59c22c1ce

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module Jekyll
  # Define duration method in Jekyll::Podcast to convert from seconds to string for feed
  module Podcast
    # Class responsible for setting episode data on a post's page
    class EpisodeData
      def initialize(page, payload)
        @site = page.site
        @page = payload['page']
        @file_path = File.join(Jekyll::Podcast::Utils.episodes_dir(@site), @page['podcast']['file'])
      end

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

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

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

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

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

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

Jekyll::Hooks.register :posts, :pre_render, priority: 'high' do |page, payload|
  Jekyll::Podcast::EpisodeData.new(page, payload).add_episode_data
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jekyll-podcast-0.9.1 lib/jekyll/podcast/episode_data.rb
jekyll-podcast-0.9.0 lib/jekyll/podcast/episode_data.rb