Class: SimpleRSS

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/simple-rss.rb

Constant Summary collapse

VERSION =

@rbs!

include Enumerable[Hash[Symbol, untyped]]
"2.1.0".freeze
DATE_TAGS =
%i[pubDate lastBuildDate published updated expirationDate modified dc:date].freeze
STRIP_HTML_TAGS =
%i[author contributor skipHours skipDays].freeze
@@feed_tags =
%i[
  id
  title subtitle link
  description
  author webMaster managingEditor contributor
  pubDate lastBuildDate updated dc:date
  generator language docs cloud
  ttl skipHours skipDays
  image logo icon rating
  rights copyright
  textInput feedburner:browserFriendly
  itunes:author itunes:category
]
@@item_tags =
%i[
  id
  title link link+alternate link+self link+edit link+replies
  author contributor
  description summary content content:encoded comments
  pubDate published updated expirationDate modified dc:date
  category guid
  trackback:ping trackback:about
  dc:creator dc:title dc:subject dc:rights dc:publisher
  feedburner:origLink
  media:content#url media:content#type media:content#height media:content#width media:content#duration
  media:title media:thumbnail#url media:thumbnail#height media:thumbnail#width
  media:credit media:credit#role
  media:category media:category#scheme
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, options = {}) ⇒ SimpleRSS

Returns a new instance of SimpleRSS.



58
59
60
61
62
63
64
65
# File 'lib/simple-rss.rb', line 58

def initialize(source, options = {})
  @source = source.respond_to?(:read) ? source.read.to_s : source.to_s
  @items = [] #: Array[Hash[Symbol, untyped]]
  @options = {} #: Hash[Symbol, untyped]
  @options.update(options)

  parse
end

Instance Attribute Details

#etagObject (readonly)

: String?



23
24
25
# File 'lib/simple-rss.rb', line 23

def etag
  @etag
end

#itemsObject (readonly) Also known as: entries

Returns the value of attribute items.



21
22
23
# File 'lib/simple-rss.rb', line 21

def items
  @items
end

#last_modifiedObject (readonly)

: String?



24
25
26
# File 'lib/simple-rss.rb', line 24

def last_modified
  @last_modified
end

#sourceObject (readonly)

: String



22
23
24
# File 'lib/simple-rss.rb', line 22

def source
  @source
end

Class Method Details

.feed_tagsObject



134
135
136
# File 'lib/simple-rss.rb', line 134

def feed_tags
  @@feed_tags
end

.feed_tags=(ft) ⇒ Object



139
140
141
# File 'lib/simple-rss.rb', line 139

def feed_tags=(ft)
  @@feed_tags = ft
end

.fetch(url, options = {}) ⇒ Object

Fetch and parse a feed from a URL Returns nil if conditional GET returns 304 Not Modified

Raises:



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/simple-rss.rb', line 164

def fetch(url, options = {})
  require "net/http"
  require "uri"

  uri = URI.parse(url)
  response = perform_fetch(uri, options)

  return nil if response.is_a?(Net::HTTPNotModified)

  raise SimpleRSSError, "HTTP #{response.code}: #{response.message}" unless response.is_a?(Net::HTTPSuccess)

  body = response.body.force_encoding(Encoding::UTF_8)
  feed = parse(body, options)
  feed.instance_variable_set(:@etag, response["ETag"])
  feed.instance_variable_set(:@last_modified, response["Last-Modified"])
  feed
end

.item_tagsObject



144
145
146
# File 'lib/simple-rss.rb', line 144

def item_tags
  @@item_tags
end

.item_tags=(it) ⇒ Object



149
150
151
# File 'lib/simple-rss.rb', line 149

def item_tags=(it)
  @@item_tags = it
end

.parse(source, options = {}) ⇒ Object

The strict attribute is for compatibility with Ruby’s standard RSS parser



156
157
158
# File 'lib/simple-rss.rb', line 156

def parse(source, options = {})
  new source, options
end

Instance Method Details

#[](index) ⇒ Object

Access an item by index



87
88
89
# File 'lib/simple-rss.rb', line 87

def [](index)
  items[index]
end

#as_json(_options = {}) ⇒ Object Also known as: to_hash



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/simple-rss.rb', line 99

def as_json(_options = {})
  hash = {} #: Hash[Symbol, untyped]

  @@feed_tags.each do |tag|
    tag_cleaned = clean_tag(tag)
    value = instance_variable_get("@#{tag_cleaned}")
    hash[tag_cleaned] = serialize_value(value) if value
  end

  hash[:items] = items.map do |item|
    item.transform_values { |v| serialize_value(v) }
  end

  hash
end

#channelObject Also known as: feed



68
69
70
# File 'lib/simple-rss.rb', line 68

def channel
  self
end

#each(&block) ⇒ Object

Iterate over all items in the feed



77
78
79
80
81
82
# File 'lib/simple-rss.rb', line 77

def each(&block)
  return enum_for(:each) unless block

  items.each(&block)
  self
end

#latest(count = 10) ⇒ Object

Get the n most recent items, sorted by date



94
95
96
# File 'lib/simple-rss.rb', line 94

def latest(count = 10)
  items.sort_by { |item| item[:pubDate] || item[:updated] || Time.at(0) }.reverse.first(count)
end

#to_jsonObject



116
117
118
119
# File 'lib/simple-rss.rb', line 116

def to_json(*)
  require "json"
  JSON.generate(as_json)
end

#to_xml(format: :rss2) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/simple-rss.rb', line 124

def to_xml(format: :rss2)
  case format
  when :rss2 then to_rss2_xml
  when :atom then to_atom_xml
  else raise ArgumentError, "Unknown format: #{format}. Supported: :rss2, :atom"
  end
end