Class: FeedParser::FeedItem

Inherits:
Object
  • Object
show all
Defined in:
lib/feedparser/feedparser.rb,
lib/feedparser/html-output.rb,
lib/feedparser/text-output.rb

Overview

an Item from a feed

Direct Known Subclasses

AtomItem, RSSItem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item = nil, feed = nil) ⇒ FeedItem

Returns a new instance of FeedItem.



179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/feedparser/feedparser.rb', line 179

def initialize(item = nil, feed = nil)
  @xml = item
  @feed = feed
  @title, @link, @content, @date, @subject = nil
  @links = []
  @creators = []
  @categories = []
  @enclosures = []

  @title = ""
  parse(item) if item
end

Instance Attribute Details

#cacheditemObject

Returns the value of attribute cacheditem.



165
166
167
# File 'lib/feedparser/feedparser.rb', line 165

def cacheditem
  @cacheditem
end

#categoriesObject

The item’s categories/tags. An array of strings.



169
170
171
# File 'lib/feedparser/feedparser.rb', line 169

def categories
  @categories
end

#contentObject

Returns the value of attribute content.



165
166
167
# File 'lib/feedparser/feedparser.rb', line 165

def content
  @content
end

#creatorsObject

Returns the value of attribute creators.



165
166
167
# File 'lib/feedparser/feedparser.rb', line 165

def creators
  @creators
end

#dateObject

Returns the value of attribute date.



165
166
167
# File 'lib/feedparser/feedparser.rb', line 165

def date
  @date
end

#enclosuresObject

The item’s enclosures childs. An array of (url, length, type) triplets.



172
173
174
# File 'lib/feedparser/feedparser.rb', line 172

def enclosures
  @enclosures
end

#feedObject (readonly)

Returns the value of attribute feed.



174
175
176
# File 'lib/feedparser/feedparser.rb', line 174

def feed
  @feed
end


234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/feedparser/feedparser.rb', line 234

def link
  if @link
    begin
      uri = URI.parse(@link)
    rescue URI::InvalidURIError
      return @link
    end
    if uri.hostname && uri.scheme
      @link
    elsif feed && feed.origin
      [feed.origin, @link].compact.join
    else
      @link
    end
  end
end

Returns the value of attribute links.



165
166
167
# File 'lib/feedparser/feedparser.rb', line 165

def links
  @links
end

#subjectObject

Returns the value of attribute subject.



165
166
167
# File 'lib/feedparser/feedparser.rb', line 165

def subject
  @subject
end

#titleObject

Returns the value of attribute title.



165
166
167
# File 'lib/feedparser/feedparser.rb', line 165

def title
  @title
end

#xmlObject (readonly)

REXML::Element for this item



177
178
179
# File 'lib/feedparser/feedparser.rb', line 177

def xml
  @xml
end

Instance Method Details

#creatorObject



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/feedparser/feedparser.rb', line 196

def creator
  case @creators.length
  when 0
    return nil
  when 1
    return creators[0]
  else
    sorted_creators = creators.sort
    return sorted_creators[0...-1].join(", ") + " and " + sorted_creators[-1]
  end
end

#parse(item) ⇒ Object



192
193
194
# File 'lib/feedparser/feedparser.rb', line 192

def parse(item)
  raise "parse() should be implemented by subclasses!"
end

#to_html(localtime = true) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/feedparser/html-output.rb', line 108

def to_html(localtime = true)
  s = <<-EOF
<table class="header">
  EOF
  r = ""
  r += "<a href=\"#{@feed.link}\">\n" if @feed.link
  if @feed.title
    r += @feed.title.escape_html
  elsif @feed.link
    r += @feed.link.escape_html
  else
    r += "Unnamed feed"
  end
  r += "</a>\n" if @feed.link
  headline = "<tr><th>%s</th>\n<td>%s</td></tr>"
  s += (headline % ["Feed:", r])

  r = ""
  r += "<a href=\"#{link}\">" if link
  if @title
    r += @title.escape_html
  elsif link
    r += link.escape_html
  end
  r += "</a>\n" if link
  s += (headline % ["Item:", r])
  s += "</table>\n"
  s += "\n"
  if @content and @content !~ /\A\s*</m
    s += "<br/>\n"
  end
  s += "#{@content}" if @content
  if @enclosures and @enclosures.length > 0
    s += <<-EOF
<table class="attachments">
    EOF
    s += '<tr><th>Files:</th></tr>'
    s += "\n"
    @enclosures.each do |e|
      s += "<tr><td><a href=\"#{e[0]}\">#{e[0].split('/')[-1]}</a> (#{e[1].to_i.to_human_readable}, #{e[2]})</td></tr>\n"
    end
    s += "</table>\n"
  end
  s += "\n<hr/>\n"
  s += '<table class="metadata">' + "\n"
  l = '<tr><th>%s</th><td>%s</td></tr>' + "\n"
  if @date
    if localtime
      s += l % [ 'Date:', @date.to_s ]
    else
      s += l % [ 'Date:', @date.getutc.to_s ]
    end
  end
  s += l % [ 'Author:', creator.escape_html ] if creator
  s += l % [ 'Subject:', @subject.escape_html ] if @subject
  s += l % [ 'Filed under:', @categories.join(', ').escape_html ] unless @categories.empty?
  s += "</table>\n"
  s
end

#to_html_with_headers(localtime = true) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/feedparser/html-output.rb', line 93

def to_html_with_headers(localtime = true)
  s = "<!doctype html>\n"
  s += '<html lang="en">'
  s += '<head>'
  s += '<meta charset="utf-8"/>'
  s += "<title>#{@title.escape_html}</title>\n"
  s += FeedParser::STYLESHEET
  s += '</head>'
  s += '<body>'
  s += to_html(localtime)
  s += "</body>"
  s += "</html>"
  s
end

#to_s(localtime = true) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/feedparser/feedparser.rb', line 208

def to_s(localtime = true)
  s = "--------------------------------\n" +
    "Title: #{@title}\nLink: #{link}\n"
  if localtime or @date.nil?
    s += "Date: #{@date.to_s}\n"
  else
    s += "Date: #{@date.getutc.to_s}\n"
  end
  s += "Creator: #{creator}\n" +
    "Subject: #{@subject}\n"
  if defined?(@categories) and @categories.length > 0
    s += "Filed under: " + @categories.join(', ') + "\n"
  end
  s += "Content:\n#{content}\n"
  if defined?(@enclosures) and @enclosures.length > 0
    s2 = "Enclosures:\n"
    @enclosures.each do |e|
      s2 += e.join(' ') + "\n"
    end
    s += s2
  end
  return s
end

#to_text(localtime = true, wrapto = false, header = true) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/feedparser/text-output.rb', line 58

def to_text(localtime = true, wrapto = false, header = true)
  s = ""
  if header
    s += "Item: "
    s += @title if @title
    s += "\n<#{link}>" if link
    if @date
      if localtime
        s += "\nDate: #{@date.to_s}"
      else
        s += "\nDate: #{@date.getutc.to_s}"
      end
    end
    s += "\n"
  else
    s += "<#{link}>\n\n" if link
  end
  s += "#{@content.html2text(wrapto).chomp}\n" if @content
  if @enclosures and @enclosures.length > 0
    s += "\nFiles:"
    @enclosures.each do |e|
      s += "\n #{e[0]} (#{e[1].to_i.to_human_readable}, #{e[2]})"
    end
  end
  if not header
    s += "-- "
  end
  s += "\nFeed: "
  s += @feed.title if @feed.title
  s += "\n<#{@feed.link}>" if @feed.link
  if not header
    s += "\nItem: "
    s += @title if @title
    s += "\n<#{link}>" if link
    if @date
      if localtime
        s += "\nDate: #{@date.to_s}"
      else
        s += "\nDate: #{@date.getutc.to_s}"
      end
    end
  end
  s += "\nAuthor: #{creator}" if creator
  s += "\nSubject: #{@subject}" if @subject
  s += "\nFiled under: #{@categories.join(', ')}" unless @categories.empty?
  s += "\n" # final newline, for compat with history
  s 
end