Sha256: 0e23710acec8ee6be3160ad6cdf4cb68bcd8910b70f116c1449328401e306b59

Contents?: true

Size: 1.53 KB

Versions: 19

Compression:

Stored size: 1.53 KB

Contents

# NOTE: search-and-replace content> with contents> before importing

module BlogLogic

  module Import

    class WordPress

      attr_accessor :source_file

      def initialize(file)
        self.source_file = file
      end

      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

      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

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

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

      class Post
        attr_accessor :title, :date, :content
        def initialize(args)
          args.each{|k,v| self.send("#{k}=",v) if self.respond_to?(k)}
        end
        def publication_date
          DateTime.parse(self.date)
        end
      end

    end

  end

end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
blog_logic-1.2.1 lib/blog_logic/import.rb
blog_logic-1.2.0 lib/blog_logic/import.rb
blog_logic-1.1.9 lib/blog_logic/import.rb
blog_logic-1.1.8 lib/blog_logic/import.rb
blog_logic-1.1.7 lib/blog_logic/import.rb
blog_logic-1.1.6 lib/blog_logic/import.rb
blog_logic-1.1.5 lib/blog_logic/import.rb
blog_logic-1.1.4 lib/blog_logic/import.rb
blog_logic-1.1.3 lib/blog_logic/import.rb
blog_logic-1.1.2 lib/blog_logic/import.rb
blog_logic-1.1.1 lib/blog_logic/import.rb
blog_logic-1.1.0 lib/blog_logic/import.rb
blog_logic-1.0.0 lib/blog_logic/import.rb
blog_logic-0.7.7 lib/blog_logic/import.rb
blog_logic-0.7.6 lib/blog_logic/import.rb
blog_logic-0.7.5 lib/blog_logic/import.rb
blog_logic-0.7.4 lib/blog_logic/import.rb
blog_logic-0.7.3 lib/blog_logic/import.rb
blog_logic-0.7.2 lib/blog_logic/import.rb