From 772cfdc25ba6be0bc6a23174c6332b1129a4acf5 Mon Sep 17 00:00:00 2001 From: Marc Remolt Date: Sun, 5 Jun 2011 17:43:30 +0200 Subject: [PATCH] Started on attachment import * created Attachment * created Dump#attachments * specs * replaced require with autoload --- Gemfile.lock | 2 +- lib/wordpress.rb | 21 ++++++------- lib/wordpress/attachment.rb | 27 +++++++++++++++++ lib/wordpress/dump.rb | 6 ++++ spec/fixtures/wordpress_dump.xml | 43 +++++++++++++++++++++++---- spec/lib/wordpress/attachment_spec.rb | 10 +++++++ spec/lib/wordpress/author_spec.rb | 4 +-- spec/lib/wordpress/dump_spec.rb | 6 ++++ 8 files changed, 101 insertions(+), 18 deletions(-) create mode 100644 lib/wordpress/attachment.rb create mode 100644 spec/lib/wordpress/attachment_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index 1452ed2..1f3e0ff 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ./ specs: - refinerycms-wordpress-import (0.1.0) + refinerycms-wordpress-import (0.2.0) bundler (~> 1.0) nokogiri (~> 1.4.4) refinerycms (~> 1.0.0) diff --git a/lib/wordpress.rb b/lib/wordpress.rb index 71cfc9c..05c4244 100644 --- a/lib/wordpress.rb +++ b/lib/wordpress.rb @@ -1,15 +1,16 @@ +require 'nokogiri' +require "wordpress/railtie" + module Refinery module WordPress + autoload :Author, 'wordpress/author' + autoload :Tag, 'wordpress/tag' + autoload :Category, 'wordpress/category' + autoload :Page, 'wordpress/page' + autoload :Post, 'wordpress/post' + autoload :Comment, 'wordpress/comment' + autoload :Dump, 'wordpress/dump' + autoload :Attachment, 'wordpress/attachment' end end -require 'nokogiri' -require 'wordpress/author' -require 'wordpress/tag' -require 'wordpress/category' -require 'wordpress/page' -require 'wordpress/post' -require 'wordpress/comment' -require 'wordpress/dump' - -require "wordpress/railtie" diff --git a/lib/wordpress/attachment.rb b/lib/wordpress/attachment.rb new file mode 100644 index 0000000..ee5a6b2 --- /dev/null +++ b/lib/wordpress/attachment.rb @@ -0,0 +1,27 @@ +module Refinery + module WordPress + class Attachment + attr_reader :node + + def initialize(node) + @node = node + end + + def title + node.xpath("title").text + end + + def description + node.xpath("description").text + end + + def post_date + DateTime.parse node.xpath("wp:post_date").text + end + + def url + node.xpath("wp:attachment_url").text + end + end + end +end diff --git a/lib/wordpress/dump.rb b/lib/wordpress/dump.rb index 0c31b6e..c01600c 100644 --- a/lib/wordpress/dump.rb +++ b/lib/wordpress/dump.rb @@ -47,6 +47,12 @@ module Refinery Category.new(category.text) end end + + def attachments + doc.xpath("//item[wp:post_type = 'attachment']").collect do |attachment| + Attachment.new(attachment) + end + end end end end diff --git a/spec/fixtures/wordpress_dump.xml b/spec/fixtures/wordpress_dump.xml index c63aa72..0653e30 100644 --- a/spec/fixtures/wordpress_dump.xml +++ b/spec/fixtures/wordpress_dump.xml @@ -16,7 +16,7 @@ - + My test blog http://localhost/wordpress Just another WordPress site - Sat, 21 May 2011 12:27:19 +0000 + Sun, 05 Jun 2011 15:27:27 +0000 en 1.1 http://localhost/wordpress @@ -161,6 +161,8 @@ In hac habitasse platea dictumst. Nunc quis tortor sed libero hendrerit dapibu Integer interdum purus id erat. Duis nec velit vitae dolor mattis euismod. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Suspendisse pellentesque dignissim lacus. + + Nulla semper euismod arcu. Suspendisse egestas, erat a consectetur dapibus, felis orci cursus eros, et sollicitudin purus urna et metus. Integer eget est sed nunc euismod vestibulum. Integer nulla dui, tristique in, euismod et, interdum imperdiet, enim. Mauris at lectus. Sed egestas tortor nec mi.]]> 6 @@ -182,12 +184,12 @@ Nulla semper euismod arcu. Suspendisse egestas, erat a consectetur dapibus, fel _edit_last - + 2 admin@example.com - + http://www.example.com/ 127.0.0.1 2011-05-21 12:26:24 2011-05-21 12:26:24 @@ -201,7 +203,7 @@ Nulla semper euismod arcu. Suspendisse egestas, erat a consectetur dapibus, fel 3 admin@example.com - http://www.example.com/ + http://www.example.com/ 127.0.0.1 2011-05-21 12:26:30 2011-05-21 12:26:30 @@ -278,5 +280,36 @@ Class aptent taciti sociosqu ad litora torquent per conubia nostra, per incepto + + 200px-Tux.svg + http://localhost/wordpress/?attachment_id=13 + Sun, 05 Jun 2011 15:26:51 +0000 + admin + http://localhost/wordpress/wp-content/uploads/2011/05/200px-Tux.svg_.png + Tux, the Linux mascot + + + 13 + 2011-06-05 15:26:51 + 2011-06-05 15:26:51 + open + open + 200px-tux-svg + inherit + 6 + 0 + attachment + + 0 + http://localhost/wordpress/wp-content/uploads/2011/05/200px-Tux.svg_.png + + _wp_attached_file + + + + _wp_attachment_metadata + + + diff --git a/spec/lib/wordpress/attachment_spec.rb b/spec/lib/wordpress/attachment_spec.rb new file mode 100644 index 0000000..002a39b --- /dev/null +++ b/spec/lib/wordpress/attachment_spec.rb @@ -0,0 +1,10 @@ +require 'spec_helper' + +describe Refinery::WordPress::Attachment, :type => :model do + let(:attachment) { test_dump.attachments.first } + + specify { attachment.title.should == '200px-Tux.svg' } + specify { attachment.description.should == 'Tux, the Linux mascot' } + specify { attachment.url.should == 'http://localhost/wordpress/wp-content/uploads/2011/05/200px-Tux.svg_.png' } + specify { attachment.post_date.should == DateTime.new(2011, 6, 5, 15, 26, 51) } +end diff --git a/spec/lib/wordpress/author_spec.rb b/spec/lib/wordpress/author_spec.rb index 3eefc3e..d1a7aff 100644 --- a/spec/lib/wordpress/author_spec.rb +++ b/spec/lib/wordpress/author_spec.rb @@ -3,8 +3,8 @@ require 'spec_helper' describe Refinery::WordPress::Author, :type => :model do let(:author) { test_dump.authors.first } - it { author.login.should == 'admin' } - it { author.email.should == 'admin@example.com' } + specify { author.login.should == 'admin' } + specify { author.email.should == 'admin@example.com' } describe "#to_refinery" do before do diff --git a/spec/lib/wordpress/dump_spec.rb b/spec/lib/wordpress/dump_spec.rb index ff532f7..d19b1a6 100644 --- a/spec/lib/wordpress/dump_spec.rb +++ b/spec/lib/wordpress/dump_spec.rb @@ -66,4 +66,10 @@ describe Refinery::WordPress::Dump, :type => :model do dump.posts(true).should have(2).posts end end + + describe "#attachments" do + it "should return all attachments" do + dump.attachments.should have(1).attachment + end + end end