Class: Html2rss::AutoSource::Scraper::JsonState
- Inherits:
-
Object
- Object
- Html2rss::AutoSource::Scraper::JsonState
- Includes:
- Enumerable
- Defined in:
- lib/html2rss/auto_source/scraper/json_state.rb,
lib/html2rss/auto_source/scraper/json_state/value_finder.rb,
lib/html2rss/auto_source/scraper/json_state/document_scanner.rb,
lib/html2rss/auto_source/scraper/json_state/article_normalizer.rb,
lib/html2rss/auto_source/scraper/json_state/candidate_detector.rb
Overview
Scrapes JSON state blobs embedded in script tags such as Next.js, Nuxt,
or custom window globals. The scraper searches <script type="application/json">
tags and well-known JavaScript globals for arrays of article-like hashes
and normalises them to a structure compatible with Html2rss::Html::ArticleExtractor.
Defined Under Namespace
Modules: ArticleNormalizer, CandidateDetector, DocumentScanner, ValueFinder
Instance Attribute Summary collapse
-
#parsed_body ⇒ Object
readonly
Returns the value of attribute parsed_body.
Class Method Summary collapse
- .articles?(parsed_body) ⇒ Boolean
-
.json_documents(parsed_body) ⇒ Array<Hash, Array>
Parsed JSON documents discovered in the response body.
-
.options_key ⇒ Symbol
Scraper config key.
Instance Method Summary collapse
-
#each {|Hash{Symbol => Object}| ... } ⇒ Enumerator, void
Article enumerator when no block is given.
-
#extractable? ⇒ Boolean
True when the page contains article-like arrays in JSON state.
-
#initialize(parsed_body, url:, **_opts) ⇒ JsonState
constructor
A new instance of JsonState.
Constructor Details
#initialize(parsed_body, url:, **_opts) ⇒ JsonState
Returns a new instance of JsonState.
36 37 38 39 |
# File 'lib/html2rss/auto_source/scraper/json_state.rb', line 36 def initialize(parsed_body, url:, **_opts) @parsed_body = parsed_body @url = url end |
Instance Attribute Details
#parsed_body ⇒ Object (readonly)
Returns the value of attribute parsed_body.
41 42 43 |
# File 'lib/html2rss/auto_source/scraper/json_state.rb', line 41 def parsed_body @parsed_body end |
Class Method Details
.articles?(parsed_body) ⇒ Boolean
19 20 21 22 23 |
# File 'lib/html2rss/auto_source/scraper/json_state.rb', line 19 def articles?(parsed_body) return false unless parsed_body DocumentScanner.json_documents(parsed_body).any? { CandidateDetector.candidate_array?(_1) } end |
.json_documents(parsed_body) ⇒ Array<Hash, Array>
Returns parsed JSON documents discovered in the response body.
27 28 29 |
# File 'lib/html2rss/auto_source/scraper/json_state.rb', line 27 def json_documents(parsed_body) DocumentScanner.json_documents(parsed_body) end |
.options_key ⇒ Symbol
Returns scraper config key.
15 |
# File 'lib/html2rss/auto_source/scraper/json_state.rb', line 15 def self. = :json_state |
Instance Method Details
#each {|Hash{Symbol => Object}| ... } ⇒ Enumerator, void
Returns article enumerator when no block is given.
50 51 52 53 54 55 56 57 58 |
# File 'lib/html2rss/auto_source/scraper/json_state.rb', line 50 def each return enum_for(:each) unless block_given? json_documents.each do |document| discover_articles(document) do |article| yield article if article end end end |
#extractable? ⇒ Boolean
Returns true when the page contains article-like arrays in JSON state.
44 45 46 |
# File 'lib/html2rss/auto_source/scraper/json_state.rb', line 44 def extractable? json_documents.any? { CandidateDetector.candidate_array?(_1) } end |