Class: SuttyMigration::WordpressXml::Post

Inherits:
Object
  • Object
show all
Defined in:
lib/sutty_migration/wordpress_xml/post.rb

Overview

Represents a WordPress post

Direct Known Subclasses

Attachment

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wordpress:, item:) ⇒ Post

Returns a new instance of Post.

Parameters:



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

#itemObject (readonly)

Returns the value of attribute item.



10
11
12
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 10

def item
  @item
end

#wordpressObject (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

Returns:

  • (String)


166
167
168
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 166

def attribute_value(key)
  item.at_css(key).text
end

#authorHash

Author attributes.

Returns:

  • (Hash)


90
91
92
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 90

def author
  @author ||= wordpress.authors[attribute_value('creator')]
end

#categoriesHash

Categories with attributes.

Returns:

  • (Hash)


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

#contentString

Content as HTML, with site URL removed.

Returns:

  • (String)


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

#dateTime

Publication date.

WordPress can store this date in three different fields and sometimes they come empty or invalid.

Returns:



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

#descriptionString

Description

Returns:

  • (String)


47
48
49
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 47

def description
  @description ||= attribute_value('description')
end

#draft?Boolean

Publication status

Returns:

  • (Boolean)


159
160
161
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 159

def draft?
  @draft ||= attribute_value('status') == 'draft'
end

#idInteger

Post ID

Returns:

  • (Integer)


26
27
28
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 26

def id
  @id ||= attribute_value('post_id').to_i
end

#inspectObject



19
20
21
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 19

def inspect
  "#<SuttyMigration::WordpressXml::Post title=\"#{title}\">"
end

#last_modified_atTime

Modification date.

Returns:



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

#metaHash

Metadata. Plugins store useful information here. Duplicated keys are returned as an Array of values.

Returns:

  • (Hash)


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
  @meta ||= {}.tap do |meta|
    item.css('postmeta').each do |m|
      key = m.css('meta_key').text
      value = m.css('meta_value').text

      case meta[key]
      when nil then meta[key] = value
      when String then meta[key] = [meta[key], value]
      when Array then meta[key] << value
      end
    end
  end
end

#orderInteger

Order. Higher are sorted on top by jekyll-order.

Returns:

  • (Integer)


145
146
147
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 145

def order
  @order ||= attribute_value 'is_sticky'
end

#passwordString

Post password. Use with jekyll-crypto.

Returns:

  • (String)


97
98
99
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 97

def password
  @password ||= attribute_value 'post_password'
end

Permalink. Absolute URL to the post.

Returns:

  • (String)


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

Returns:

  • (Boolean)


152
153
154
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 152

def published?
  @published ||= attribute_value('status') == 'publish'
end

#slugString

Slug (“post name”)

Returns:

  • (String)


54
55
56
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 54

def slug
  @slug ||= attribute_value('post_name')
end

#tagsHash

Tags with attributes.

Returns:

  • (Hash)


104
105
106
107
108
109
110
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 104

def tags
  @tags ||= item.css('category').select do |c|
    c[:domain] == 'post_tag'
  end.map do |c|
    wordpress.tags[c[:nicename]]
  end
end

#titleString

Title

Returns:

  • (String)


40
41
42
# File 'lib/sutty_migration/wordpress_xml/post.rb', line 40

def title
  @title ||= attribute_value('title')
end