Class: Refinery::WordPress::Dump

Inherits:
Object
  • Object
show all
Defined in:
lib/wordpress/dump.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name) ⇒ Dump

Returns a new instance of Dump.



6
7
8
9
10
11
12
13
14
# File 'lib/wordpress/dump.rb', line 6

def initialize(file_name)
  file_name = File.absolute_path(file_name)

  raise "Given file '#{file_name}' no file or not readable." \
    unless File.file?(file_name) && File.readable?(file_name)
  
  file = File.open(file_name)
  @doc = Nokogiri::XML(file)
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



4
5
6
# File 'lib/wordpress/dump.rb', line 4

def doc
  @doc
end

Instance Method Details

#attachmentsObject



51
52
53
54
55
# File 'lib/wordpress/dump.rb', line 51

def attachments
  doc.xpath("//item[wp:post_type = 'attachment']").collect do |attachment|
    Attachment.new(attachment)
  end
end

#authorsObject



16
17
18
19
20
# File 'lib/wordpress/dump.rb', line 16

def authors
  doc.xpath("//wp:author").collect do |author|
    Author.new(author)
  end
end

#categoriesObject



45
46
47
48
49
# File 'lib/wordpress/dump.rb', line 45

def categories
  doc.xpath("//wp:category/wp:cat_name").collect do |category|
    Category.new(category.text)
  end
end

#pages(only_published = false) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/wordpress/dump.rb', line 22

def pages(only_published=false)
  pages = doc.xpath("//item[wp:post_type = 'page']").collect do |page|
    Page.new(page)
  end

  pages = pages.select(&:published?) if only_published
  pages
end

#posts(only_published = false) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/wordpress/dump.rb', line 31

def posts(only_published=false)
  posts = doc.xpath("//item[wp:post_type = 'post']").collect do |post|
    Post.new(post)
  end
  posts = posts.select(&:published?) if only_published
  posts
end

#tagsObject



39
40
41
42
43
# File 'lib/wordpress/dump.rb', line 39

def tags
  doc.xpath("//wp:tag/wp:tag_slug").collect do |tag|
    Tag.new(tag.text)
  end
end