Class: SimpleRSS
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
%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 ]
%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
-
#etag ⇒ Object
readonly
: String?.
-
#items ⇒ Object
(also: #entries)
readonly
Returns the value of attribute items.
-
#last_modified ⇒ Object
readonly
: String?.
-
#source ⇒ Object
readonly
: String.
Class Method Summary collapse
- .feed_tags ⇒ Object
- .feed_tags=(ft) ⇒ Object
-
.fetch(url, options = {}) ⇒ Object
Fetch and parse a feed from a URL Returns nil if conditional GET returns 304 Not Modified.
- .item_tags ⇒ Object
- .item_tags=(it) ⇒ Object
-
.parse(source, options = {}) ⇒ Object
The strict attribute is for compatibility with Ruby’s standard RSS parser.
Instance Method Summary collapse
-
#[](index) ⇒ Object
Access an item by index.
- #as_json(_options = {}) ⇒ Object (also: #to_hash)
- #channel ⇒ Object (also: #feed)
-
#each(&block) ⇒ Object
Iterate over all items in the feed.
-
#initialize(source, options = {}) ⇒ SimpleRSS
constructor
A new instance of SimpleRSS.
-
#latest(count = 10) ⇒ Object
Get the n most recent items, sorted by date.
- #to_json ⇒ Object
- #to_xml(format: :rss2) ⇒ Object
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, = {}) @source = source.respond_to?(:read) ? source.read.to_s : source.to_s @items = [] #: Array[Hash[Symbol, untyped]] @options = {} #: Hash[Symbol, untyped] @options.update() parse end |
Instance Attribute Details
#etag ⇒ Object (readonly)
: String?
23 24 25 |
# File 'lib/simple-rss.rb', line 23 def etag @etag end |
#items ⇒ Object (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_modified ⇒ Object (readonly)
: String?
24 25 26 |
# File 'lib/simple-rss.rb', line 24 def last_modified @last_modified end |
#source ⇒ Object (readonly)
: String
22 23 24 |
# File 'lib/simple-rss.rb', line 22 def source @source end |
Class Method Details
.feed_tags ⇒ Object
134 135 136 |
# File 'lib/simple-rss.rb', line 134 def @@feed_tags end |
.feed_tags=(ft) ⇒ Object
139 140 141 |
# File 'lib/simple-rss.rb', line 139 def (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
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, = {}) require "net/http" require "uri" uri = URI.parse(url) response = perform_fetch(uri, ) return nil if response.is_a?(Net::HTTPNotModified) raise SimpleRSSError, "HTTP #{response.code}: #{response.}" unless response.is_a?(Net::HTTPSuccess) body = response.body.force_encoding(Encoding::UTF_8) feed = parse(body, ) feed.instance_variable_set(:@etag, response["ETag"]) feed.instance_variable_set(:@last_modified, response["Last-Modified"]) feed end |
.item_tags ⇒ Object
144 145 146 |
# File 'lib/simple-rss.rb', line 144 def @@item_tags end |
.item_tags=(it) ⇒ Object
149 150 151 |
# File 'lib/simple-rss.rb', line 149 def (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, = {}) new source, 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( = {}) 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 |
#channel ⇒ Object 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_json ⇒ Object
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 |