Class: ArticleJSON::Article

Inherits:
Object
  • Object
show all
Defined in:
lib/article_json/article.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements) ⇒ Article

Returns a new instance of Article.

Parameters:



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_elementsObject (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_elementsObject (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_exporterArticleJSON::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

#elementsArray[ArticleJSON::Elements::Base]

All elements of this article with optional additional elements placed in between

Returns:



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_exporterArticleJSON::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_exporterArticleJSON::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.

Parameters:

  • additional_elements (Object)


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_exporterArticleJSON::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_ampString

AMP export of the article

Returns:

  • (String)


61
62
63
# File 'lib/article_json/article.rb', line 61

def to_amp
  amp_exporter.html
end

#to_facebook_instant_articleString

FacebookInstantArticle export of the article

Returns:

  • (String)


73
74
75
# File 'lib/article_json/article.rb', line 73

def to_facebook_instant_article
  facebook_instant_article_exporter.html
end

#to_hHash

Hash representation of the article

Returns:

  • (Hash)


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_htmlString

HTML export of the article

Returns:

  • (String)


49
50
51
# File 'lib/article_json/article.rb', line 49

def to_html
  html_exporter.html
end

#to_jsonString

JSON representation of the article

Returns:

  • (String)


37
38
39
# File 'lib/article_json/article.rb', line 37

def to_json
  to_h.to_json
end

#to_plain_textString

Plain text export of the article

Returns:

  • (String)


85
86
87
# File 'lib/article_json/article.rb', line 85

def to_plain_text
  plain_text_exporter.text
end