Class: BlogLogic::Import::WordPress

Inherits:
Object
  • Object
show all
Defined in:
lib/blog_logic/import.rb

Defined Under Namespace

Classes: Post

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ WordPress

Returns a new instance of WordPress.



7
8
9
# File 'lib/blog_logic/import.rb', line 7

def initialize(file)
  self.source_file = file
end

Instance Attribute Details

#source_fileObject

Returns the value of attribute source_file.



5
6
7
# File 'lib/blog_logic/import.rb', line 5

def source_file
  @source_file
end

Instance Method Details

#postsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/blog_logic/import.rb', line 24

def posts
  @posts ||= []
  if @posts.empty?
    raw_posts.each do |item|
      @posts << Post.new(
        :title => item.xpath('.//title').first.content,
        :date => item.xpath('.//pubDate').first.content,
        :content => item.xpath('.//contents').children.first.content,
        :summary => item.xpath('.//excerpt').first.try(:content)
      )
    end
  end
  @posts
end

#raw_postsObject



43
44
45
# File 'lib/blog_logic/import.rb', line 43

def raw_posts
  self.source.xpath('//item')
end

#sourceObject



39
40
41
# File 'lib/blog_logic/import.rb', line 39

def source
  @source ||= Nokogiri::XML(File.open(self.source_file))
end

#to(blog) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/blog_logic/import.rb', line 11

def to(blog)
  self.posts.each do |post|
    blog.posts.create(
      :title => post.title,
      :author => blog.default_author,
      :publication_date => post.publication_date,
      :content => post.content,
      :summary => post.content.truncate(255),
      :state => 'published'
    )
  end
end