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.



11
12
13
# File 'lib/blog_logic/import.rb', line 11

def initialize(file)
  self.source_file = file
end

Instance Attribute Details

#source_fileObject

Returns the value of attribute source_file.



9
10
11
# File 'lib/blog_logic/import.rb', line 9

def source_file
  @source_file
end

Instance Method Details

#postsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/blog_logic/import.rb', line 29

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



48
49
50
# File 'lib/blog_logic/import.rb', line 48

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

#sourceObject



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

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

#to(blog) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/blog_logic/import.rb', line 15

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",
      :desired_slug => "#{post.title.to_s}"
    )
  end
end