class Podrick::Episode

Attributes

images[R]
xml_doc[R]

Public Class Methods

new(xml_doc) click to toggle source
# File lib/podrick/episode.rb, line 5
def initialize xml_doc
  @xml_doc = xml_doc
  @images = []
end

Public Instance Methods

add_image(url, width = nil, height = nil) click to toggle source
# File lib/podrick/episode.rb, line 65
def add_image url, width = nil, height = nil
  image_struct = Struct.new(:url, :width, :height)

  @images << image_struct.new(url, width, height)
end
content() click to toggle source
# File lib/podrick/episode.rb, line 28
def content
  @content ||= content_without_images
end
description() click to toggle source
# File lib/podrick/episode.rb, line 18
def description
  @description ||= load_without_images("description")
end
enclosure() click to toggle source
# File lib/podrick/episode.rb, line 32
def enclosure
  @enclosure ||= begin
    enclosure_struct = Struct.new(:url, :length, :type)
    enclosure = xml_doc.at_xpath("enclosure")
    if enclosure
      enclosure_struct.new(
        enclosure["url"],
        enclosure["length"],
        enclosure["type"]
      )
    else
      enclosure_struct.new(nil, nil, nil)
    end
  end
end
itunes() click to toggle source
# File lib/podrick/episode.rb, line 48
def itunes
  @itunes ||= begin
    image = Struct.new(:href)
    itunes = Struct.new(:author, :duration, :subtitle, :summary, :keywords, :image)
    image_href = (node = xml_node_at("itunes:image")) ? node["href"] : nil

    itunes.new(
      xml_content_at("itunes:author"),
      xml_content_at("itunes:duration"),
      xml_content_at("itunes:subtitle"),
      xml_content_at("itunes:summary"),
      xml_content_at("itunes:keywords"),
      image.new(image_href)
    )
  end
end
pubDate()
Alias for: published_date
published_date() click to toggle source
# File lib/podrick/episode.rb, line 22
def published_date
  @published_date ||= Time.parse(xml_doc.at_xpath("pubDate").content)
end
Also aliased as: pubDate
xml_content_at(string) click to toggle source
# File lib/podrick/episode.rb, line 71
def xml_content_at string
  begin
    if node = xml_doc.at_xpath(string)
      node.content
    end
  rescue Nokogiri::XML::XPath::SyntaxError
    nil
  end
end
xml_node_at(string) click to toggle source
# File lib/podrick/episode.rb, line 81
def xml_node_at string
  if node = xml_doc.at_xpath(string)
    node
  end
end

Private Instance Methods

content_without_images() click to toggle source
# File lib/podrick/episode.rb, line 89
def content_without_images
  begin
    load_without_images("content:encoded")
  rescue Nokogiri::XML::XPath::SyntaxError
    nil
  end
end
load_without_images(node_name) click to toggle source
# File lib/podrick/episode.rb, line 97
def load_without_images node_name
  node = xml_node_at(node_name)

  return nil if node.nil?

  html = Nokogiri::HTML(node.content)
  html.xpath("//img").each do |img|
    if img.key?("src")
      url = img["src"]
      height = img["height"]
      width = img["width"]

      add_image(url, width, height)
    end

    img.remove
  end

  html.to_html
end