Class: ArticleJSON::Article
- Inherits:
-
Object
- Object
- ArticleJSON::Article
- Defined in:
- lib/article_json/article.rb
Instance Attribute Summary collapse
-
#additional_elements ⇒ Object
readonly
Returns the value of attribute additional_elements.
-
#article_elements ⇒ Object
readonly
Returns the value of attribute article_elements.
Class Method Summary collapse
-
.from_google_doc_html(html) ⇒ ArticleJSON::Article
Build a new article from a Google Doc HTML export.
-
.from_hash(hash) ⇒ ArticleJSON::Article
Build a new article from hash (like the one generated by #to_h).
-
.from_json(json) ⇒ ArticleJSON::Article
Build a new article from JSON (like the one generated by #to_json).
Instance Method Summary collapse
-
#amp_exporter ⇒ ArticleJSON::Export::AMP::Exporter
Exporter instance for AMP.
-
#elements ⇒ Array[ArticleJSON::Elements::Base]
All elements of this article with optional additional elements placed in between.
-
#facebook_instant_article_exporter ⇒ ArticleJSON::Export::FacebookInstantArticle::Exporter
Exporter instance for FacebookInstantArticle.
-
#html_exporter ⇒ ArticleJSON::Export::HTML::Exporter
Exporter instance for HTML.
-
#initialize(elements) ⇒ Article
constructor
A new instance of Article.
-
#place_additional_elements(additional_elements) ⇒ Object
Distribute passed elements evenly throughout the article.
-
#plain_text_exporter ⇒ ArticleJSON::Export::PlainText::Exporter
Exporter instance for plain text.
-
#to_amp ⇒ String
AMP export of the article.
-
#to_facebook_instant_article ⇒ String
FacebookInstantArticle export of the article.
-
#to_h ⇒ Hash
Hash representation of the article.
-
#to_html ⇒ String
HTML export of the article.
-
#to_json ⇒ String
JSON representation of the article.
-
#to_plain_text ⇒ String
Plain text export of the article.
Constructor Details
#initialize(elements) ⇒ Article
Returns a new instance of Article.
6 7 8 9 |
# File 'lib/article_json/article.rb', line 6 def initialize(elements) @article_elements = elements @additional_elements = [] end |
Instance Attribute Details
#additional_elements ⇒ Object (readonly)
Returns the value of attribute additional_elements.
3 4 5 |
# File 'lib/article_json/article.rb', line 3 def additional_elements @additional_elements end |
#article_elements ⇒ Object (readonly)
Returns the value of attribute article_elements.
3 4 5 |
# File 'lib/article_json/article.rb', line 3 def article_elements @article_elements end |
Class Method Details
.from_google_doc_html(html) ⇒ ArticleJSON::Article
Build a new article from a Google Doc HTML export
116 117 118 119 |
# File 'lib/article_json/article.rb', line 116 def from_google_doc_html(html) parser = ArticleJSON::Import::GoogleDoc::HTML::Parser.new(html) new(parser.parsed_content) end |
.from_hash(hash) ⇒ ArticleJSON::Article
Build a new article from hash (like the one generated by #to_h)
103 104 105 106 |
# File 'lib/article_json/article.rb', line 103 def from_hash(hash) hash = { content: hash } if hash.is_a?(Array) new(ArticleJSON::Elements::Base.parse_hash_list(hash[:content])) end |
.from_json(json) ⇒ ArticleJSON::Article
Build a new article from JSON (like the one generated by #to_json)
110 111 112 |
# File 'lib/article_json/article.rb', line 110 def from_json(json) from_hash(JSON.parse(json, symbolize_names: true)) end |
Instance Method Details
#amp_exporter ⇒ ArticleJSON::Export::AMP::Exporter
Exporter instance for AMP
55 56 57 |
# File 'lib/article_json/article.rb', line 55 def amp_exporter ArticleJSON::Export::AMP::Exporter.new(elements) end |
#elements ⇒ Array[ArticleJSON::Elements::Base]
All elements of this article with optional additional elements placed in between
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/article_json/article.rb', line 14 def elements @elements ||= begin if @additional_elements.any? ArticleJSON::Utils::AdditionalElementPlacer .new(@article_elements, @additional_elements) .merge_elements else @article_elements end end end |
#facebook_instant_article_exporter ⇒ ArticleJSON::Export::FacebookInstantArticle::Exporter
Exporter instance for FacebookInstantArticle
67 68 69 |
# File 'lib/article_json/article.rb', line 67 def facebook_instant_article_exporter ArticleJSON::Export::FacebookInstantArticle::Exporter.new(elements) end |
#html_exporter ⇒ ArticleJSON::Export::HTML::Exporter
Exporter instance for HTML
43 44 45 |
# File 'lib/article_json/article.rb', line 43 def html_exporter ArticleJSON::Export::HTML::Exporter.new(elements) end |
#place_additional_elements(additional_elements) ⇒ Object
Distribute passed elements evenly throughout the article. All passed elements need to have an exporter to be represented in the rendered article. If the method is called multiple times, the order of additional elements is maintained.
94 95 96 97 98 |
# File 'lib/article_json/article.rb', line 94 def place_additional_elements(additional_elements) # Reset the `#elements` method memoization @elements = nil @additional_elements.concat(additional_elements) end |
#plain_text_exporter ⇒ ArticleJSON::Export::PlainText::Exporter
Exporter instance for plain text
79 80 81 |
# File 'lib/article_json/article.rb', line 79 def plain_text_exporter ArticleJSON::Export::PlainText::Exporter.new(elements) end |
#to_amp ⇒ String
AMP export of the article
61 62 63 |
# File 'lib/article_json/article.rb', line 61 def to_amp amp_exporter.html end |
#to_facebook_instant_article ⇒ String
FacebookInstantArticle export of the article
73 74 75 |
# File 'lib/article_json/article.rb', line 73 def to_facebook_instant_article facebook_instant_article_exporter.html end |
#to_h ⇒ Hash
Hash representation of the article
28 29 30 31 32 33 |
# File 'lib/article_json/article.rb', line 28 def to_h { article_json_version: VERSION, content: elements.map(&:to_h), } end |
#to_html ⇒ String
HTML export of the article
49 50 51 |
# File 'lib/article_json/article.rb', line 49 def to_html html_exporter.html end |
#to_json ⇒ String
JSON representation of the article
37 38 39 |
# File 'lib/article_json/article.rb', line 37 def to_json to_h.to_json end |
#to_plain_text ⇒ String
Plain text export of the article
85 86 87 |
# File 'lib/article_json/article.rb', line 85 def to_plain_text plain_text_exporter.text end |