Class: SimpleRSS::JsonFeed
- Inherits:
-
Object
- Object
- SimpleRSS::JsonFeed
- Defined in:
- lib/simple-rss/json_feed.rb
Constant Summary collapse
- VERSIONS =
%w[https://jsonfeed.org/version/1 https://jsonfeed.org/version/1.1].freeze
- FEED_FIELDS =
%w[title description home_page_url feed_url icon favicon language author authors expired next_url hubs user_comment].freeze
- FEED_STRINGS =
%w[description home_page_url feed_url icon favicon next_url user_comment].freeze
- ITEM_STRINGS =
%w[url external_url title summary content_html content_text image banner_image].freeze
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
: Hash[String, untyped].
-
#items ⇒ Object
readonly
: Array[Hash[Symbol, untyped]].
Instance Method Summary collapse
-
#initialize(source) ⇒ JsonFeed
constructor
A new instance of JsonFeed.
- #normalized_entry(item, source_url: nil) ⇒ Object
Constructor Details
#initialize(source) ⇒ JsonFeed
Returns a new instance of JsonFeed.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/simple-rss/json_feed.rb', line 17 def initialize(source) source = source.b.sub(/\A\xEF\xBB\xBF/n, "").force_encoding(Encoding::UTF_8) raise SimpleRSSError, "Malformed JSON Feed: invalid UTF-8" unless source.valid_encoding? @document = JSON.parse(source) validate freeze_data(@document) @originals = {} #: Hash[Hash[Symbol, untyped], Hash[String, untyped]] @originals.compare_by_identity @items = @document.fetch("items").map do |original| item = legacy_item(original) @originals[item] = original item end rescue JSON::ParserError, EncodingError => e raise SimpleRSSError, "Malformed JSON Feed: #{e.}" end |
Instance Attribute Details
#document ⇒ Object (readonly)
: Hash[String, untyped]
11 12 13 |
# File 'lib/simple-rss/json_feed.rb', line 11 def document @document end |
#items ⇒ Object (readonly)
: Array[Hash[Symbol, untyped]]
12 13 14 |
# File 'lib/simple-rss/json_feed.rb', line 12 def items @items end |
Instance Method Details
#normalized_entry(item, source_url: nil) ⇒ Object
36 37 38 39 |
# File 'lib/simple-rss/json_feed.rb', line 36 def normalized_entry(item, source_url: nil) original = @originals[item] || raise(SimpleRSSError, "Cannot normalize an item without its original JSON source") SimpleRSS::JsonEntryNormalizer.new(original, document, source_url: source_url).entry end |