Class: SuttyMigration::WordpressXml::Post
- Inherits:
-
Object
- Object
- SuttyMigration::WordpressXml::Post
- Defined in:
- lib/sutty_migration/wordpress_xml/post.rb
Overview
Represents a WordPress post
Direct Known Subclasses
Instance Attribute Summary collapse
-
#item ⇒ Object
readonly
Returns the value of attribute item.
-
#wordpress ⇒ Object
readonly
Returns the value of attribute wordpress.
Instance Method Summary collapse
-
#attribute_value(key) ⇒ String
Get a value from the attribute.
-
#author ⇒ Hash
Author attributes.
-
#categories ⇒ Hash
Categories with attributes.
-
#content ⇒ String
Content as HTML, with site URL removed.
-
#date ⇒ Time
Publication date.
-
#description ⇒ String
Description.
-
#draft? ⇒ Boolean
Publication status.
-
#id ⇒ Integer
Post ID.
-
#initialize(wordpress:, item:) ⇒ Post
constructor
A new instance of Post.
- #inspect ⇒ Object
-
#last_modified_at ⇒ Time
Modification date.
-
#meta ⇒ Hash
Metadata.
-
#order ⇒ Integer
Order.
-
#password ⇒ String
Post password.
-
#permalink ⇒ String
Permalink.
-
#published? ⇒ Boolean
Publication status.
-
#slug ⇒ String
Slug (“post name”).
-
#tags ⇒ Hash
Tags with attributes.
-
#title ⇒ String
Title.
Constructor Details
#initialize(wordpress:, item:) ⇒ Post
Returns a new instance of Post.
14 15 16 17 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 14 def initialize(wordpress:, item:) @wordpress = wordpress @item = item end |
Instance Attribute Details
#item ⇒ Object (readonly)
Returns the value of attribute item.
10 11 12 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 10 def item @item end |
#wordpress ⇒ Object (readonly)
Returns the value of attribute wordpress.
10 11 12 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 10 def wordpress @wordpress end |
Instance Method Details
#attribute_value(key) ⇒ String
Get a value from the attribute
166 167 168 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 166 def attribute_value(key) item.at_css(key).text end |
#author ⇒ Hash
Author attributes.
90 91 92 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 90 def @author ||= wordpress.[attribute_value('creator')] end |
#categories ⇒ Hash
Categories with attributes.
115 116 117 118 119 120 121 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 115 def categories @categories ||= item.css('category').select do |c| c[:domain] == 'category' end.map do |c| wordpress.categories[c[:nicename]] end end |
#content ⇒ String
Content as HTML, with site URL removed.
81 82 83 84 85 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 81 def content @content ||= WordpressFormatting::Wpautop.wpautop(attribute_value('encoded')).gsub( / (href|src)="#{wordpress.url}/, ' \\1="' ) end |
#date ⇒ Time
Publication date.
WordPress can store this date in three different fields and sometimes they come empty or invalid.
64 65 66 67 68 69 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 64 def date @date ||= %w[pubDate post_date_gmt post_date].map do |date_attr| ::Jekyll::Utils.parse_date attribute_value(date_attr) rescue StandardError end.compact.first end |
#description ⇒ String
Description
47 48 49 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 47 def description @description ||= attribute_value('description') end |
#draft? ⇒ Boolean
Publication status
159 160 161 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 159 def draft? @draft ||= attribute_value('status') == 'draft' end |
#id ⇒ Integer
Post ID
26 27 28 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 26 def id @id ||= attribute_value('post_id').to_i end |
#inspect ⇒ Object
19 20 21 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 19 def inspect "#<SuttyMigration::WordpressXml::Post title=\"#{title}\">" end |
#last_modified_at ⇒ Time
Modification date.
74 75 76 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 74 def last_modified_at @last_modified_at ||= ::Jekyll::Utils.parse_date attribute_value('post_modified_gmt') end |
#meta ⇒ Hash
Metadata. Plugins store useful information here. Duplicated keys are returned as an Array of values.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 127 def @meta ||= {}.tap do || item.css('postmeta').each do |m| key = m.css('meta_key').text value = m.css('meta_value').text case [key] when nil then [key] = value when String then [key] = [[key], value] when Array then [key] << value end end end end |
#order ⇒ Integer
Order. Higher are sorted on top by jekyll-order.
145 146 147 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 145 def order @order ||= attribute_value 'is_sticky' end |
#password ⇒ String
Post password. Use with jekyll-crypto.
97 98 99 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 97 def password @password ||= attribute_value 'post_password' end |
#permalink ⇒ String
Permalink. Absolute URL to the post.
33 34 35 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 33 def permalink @permalink ||= attribute_value('link').sub(wordpress.url, '') end |
#published? ⇒ Boolean
Publication status
152 153 154 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 152 def published? @published ||= attribute_value('status') == 'publish' end |
#slug ⇒ String
Slug (“post name”)
54 55 56 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 54 def slug @slug ||= attribute_value('post_name') end |
#tags ⇒ Hash
Tags with attributes.
104 105 106 107 108 109 110 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 104 def @tags ||= item.css('category').select do |c| c[:domain] == 'post_tag' end.map do |c| wordpress.[c[:nicename]] end end |
#title ⇒ String
Title
40 41 42 |
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 40 def title @title ||= attribute_value('title') end |