Class: Podrick::Episode

Inherits:
Object
  • Object
show all
Defined in:
lib/podrick/episode.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml_doc) ⇒ Episode

Returns a new instance of Episode.



5
6
7
8
# File 'lib/podrick/episode.rb', line 5

def initialize xml_doc
  @xml_doc = xml_doc
  @images = []
end

Instance Attribute Details

#imagesObject (readonly)

Returns the value of attribute images.



3
4
5
# File 'lib/podrick/episode.rb', line 3

def images
  @images
end

#xml_docObject (readonly)

Returns the value of attribute xml_doc.



3
4
5
# File 'lib/podrick/episode.rb', line 3

def xml_doc
  @xml_doc
end

Instance Method Details

#add_image(url, width = nil, height = nil) ⇒ Object



65
66
67
68
69
# 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

#contentObject



28
29
30
# File 'lib/podrick/episode.rb', line 28

def content
  @content ||= content_without_images
end

#descriptionObject



18
19
20
# File 'lib/podrick/episode.rb', line 18

def description
  @description ||= load_without_images("description")
end

#enclosureObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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

#itunesObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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

#published_dateObject Also known as: pubDate



22
23
24
# File 'lib/podrick/episode.rb', line 22

def published_date
  @published_date ||= Time.parse(xml_doc.at_xpath("pubDate").content)
end

#xml_content_at(string) ⇒ Object



71
72
73
74
75
76
77
78
79
# 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) ⇒ Object



81
82
83
84
85
# File 'lib/podrick/episode.rb', line 81

def xml_node_at string
  if node = xml_doc.at_xpath(string)
    node
  end
end