Last active
August 29, 2015 14:07
-
-
Save jeffkreeftmeijer/41af6c5c89412f6ad1a3 to your computer and use it in GitHub Desktop.
Import
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PATH | |
remote: . | |
specs: | |
import (0.1.0) | |
activemodel (~> 4.1) | |
nokogiri (~> 1.6.2) | |
GEM | |
specs: | |
activemodel (4.1.6) | |
activesupport (= 4.1.6) | |
builder (~> 3.1) | |
activesupport (4.1.6) | |
i18n (~> 0.6, >= 0.6.9) | |
json (~> 1.7, >= 1.7.7) | |
minitest (~> 5.1) | |
thread_safe (~> 0.1) | |
tzinfo (~> 1.1) | |
builder (3.2.2) | |
i18n (0.6.11) | |
json (1.8.1) | |
mini_portile (0.6.0) | |
minitest (5.4.2) | |
nokogiri (1.6.2.1) | |
mini_portile (= 0.6.0) | |
thread_safe (0.3.4) | |
tzinfo (1.2.2) | |
thread_safe (~> 0.1) | |
PLATFORMS | |
ruby | |
DEPENDENCIES | |
import! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Gem::Specification.new do |s| | |
s.name = 'importer' | |
s.version = '0.1.0' | |
s.platform = Gem::Platform::RUBY | |
s.author = 'Jeff Kreeftmeijer' | |
s.email = 'jeff@kreeftmeijer.nl' | |
s.summary = 'Importer' | |
s.description = 'Imports comments' | |
s.files = ['importer.rb'] | |
s.test_file = 'importer_test.rb' | |
s.require_path = '.' | |
s.add_dependency('activemodel', ['~> 4.1']) | |
s.add_dependency('nokogiri', ['~> 1.6.2']) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_model' | |
require 'nokogiri' | |
class Importer | |
class Base | |
include ActiveModel::AttributeMethods | |
attr_reader :attributes | |
def initialize(attributes = {}) | |
@attributes = attributes | |
end | |
def attribute(attr) | |
attributes[attr.to_sym] | |
end | |
end | |
class Discussion < Base | |
define_attribute_methods %w(url) | |
def comments | |
attributes[:comments].map do |comment_attributes| | |
Comment.new(comment_attributes) | |
end | |
end | |
end | |
class User < Base | |
define_attribute_methods %w(name handle provider remote_id) | |
end | |
class Comment < Base | |
define_attribute_methods %(message) | |
def user | |
User.new(attributes[:user]) | |
end | |
end | |
class Disqus | |
def initialize(xml) | |
@document = Nokogiri::XML(xml) | |
end | |
def discussions | |
@document.css('thread').map do |thread| | |
thread_id = thread.attributes['id'].value | |
posts = @document.css('post').select do |post| | |
post.css('thread').first.attributes['id'].value == thread_id | |
end | |
Discussion.new(thread, posts) | |
end | |
end | |
class Discussion < Importer::Discussion | |
def initialize(thread, posts) | |
@posts = posts | |
super(url: thread.css('link').text) | |
end | |
def comments | |
@posts.map do |post| | |
Comment.new(post) | |
end | |
end | |
end | |
class User < Importer::User | |
def initialize(author) | |
super( | |
name: author.css('name').text, | |
handle: author.css('username').text, | |
email: author.css('email').text, | |
provider: 'disqus' | |
) | |
end | |
end | |
class Comment < Importer::Comment | |
def initialize(post) | |
@post = post | |
super( | |
message: post.css('message').text | |
) | |
end | |
def user | |
User.new(@post.css('author').first) | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'minitest/autorun' | |
require_relative 'importer' | |
def disqus_import_xml | |
'<?xml version="1.0" encoding="utf-8"?> | |
<disqus xmlns="http://disqus.com" xmlns:dsq="http://disqus.com/disqus-internals" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://disqus.com/api/schemas/1.0/disqus.xsd http://disqus.com/api/schemas/1.0/disqus-internals.xsd"> | |
<category dsq:id="303939"> | |
<forum>jeffkreeftmeijer</forum> | |
<title>General</title> | |
<isDefault>true</isDefault> | |
</category> | |
<thread dsq:id="3001004008"> | |
<id /> | |
<forum>jeffkreeftmeijer</forum> | |
<category dsq:id="303939" /> | |
<link>http://jeffkreeftmeijer.com/2014/using-test-fixtures-with-carrierwave/</link> | |
<title>Using test fixtures with CarrierWave – Jeff Kreeftmeijer</title> | |
<message /> | |
<createdAt>2014-09-09T06:08:19Z</createdAt> | |
<author> | |
<email>jeff@example.com</email> | |
<name>Jeff Kreeftmeijer</name> | |
<isAnonymous>false</isAnonymous> | |
<username>jkreeftmeijer</username> | |
</author> | |
<ipAddress>91.189.97.249</ipAddress> | |
<isClosed>false</isClosed> | |
<isDeleted>false</isDeleted> | |
</thread> | |
<post dsq:id="1606512267"> | |
<id /> | |
<message><![CDATA[<p>Thanks for this article. I came here from your StackOverflow response. I followed these steps and the file was assigned to my model.</p><p>The problem is that I get the validation error "image_field can\'t be blank", even though everything seems in order. Have you encountered this problem?</p>]]></message> | |
<createdAt>2014-09-26T08:37:39Z</createdAt> | |
<isDeleted>false</isDeleted> | |
<isSpam>false</isSpam> | |
<author> | |
<email>felix@example.com</email> | |
<name>felix</name> | |
<isAnonymous>false</isAnonymous> | |
<username>flx</username> | |
</author> | |
<ipAddress>92.58.178.3</ipAddress> | |
<thread dsq:id="3001004008" /> | |
</post> | |
</disqus>' | |
end | |
def disqus_import_document | |
Nokogiri::XML(disqus_import_xml) | |
end | |
def disqus_import_thread | |
disqus_import_document.css('thread').first | |
end | |
def disqus_import_posts | |
disqus_import_document.css('post') | |
end | |
def disqus_import_post | |
disqus_import_posts.first | |
end | |
def disqus_import_user | |
disqus_import_post.css('author') | |
end | |
def discussion_params | |
{ | |
url: 'http://jeffkreeftmeijer.com/2014/using-test-fixtures-with-carrierwave', | |
comments: [comment_params] | |
} | |
end | |
def comment_params | |
{ | |
message: 'foo', | |
user: user_params | |
} | |
end | |
def user_params | |
{ | |
name: 'Jeff Kreeftmeijer', | |
handle: 'jkreeftmeijer', | |
provider: 'twitter', | |
remote_id: '8284992' | |
} | |
end | |
describe Importer::Discussion do | |
before do | |
@discussion = Importer::Discussion.new(discussion_params) | |
end | |
it "has an URL" do | |
assert @discussion.url, 'http://jeffkreeftmeijer.com/2014/using-test-fixtures-with-carrierwave' | |
end | |
it "has comments" do | |
comment = @discussion.comments.first | |
assert comment.message, 'foo' | |
end | |
end | |
describe Importer::User do | |
before do | |
@user = Importer::User.new(user_params) | |
end | |
it "has a name" do | |
assert_equal 'Jeff Kreeftmeijer', @user.name | |
end | |
it "has a handle" do | |
assert_equal 'jkreeftmeijer', @user.handle | |
end | |
it "has a provider" do | |
assert_equal 'twitter', @user.provider | |
end | |
it "has a remote_id" do | |
assert_equal '8284992', @user.remote_id | |
end | |
end | |
describe Importer::Comment do | |
before do | |
@comment = Importer::Comment.new(comment_params) | |
end | |
it "has a message" do | |
assert_equal 'foo', @comment.message | |
end | |
it "has a user" do | |
user = @comment.user | |
assert_equal 'Jeff Kreeftmeijer', user.name | |
end | |
end | |
describe Importer::Disqus do | |
before do | |
@import = Importer::Disqus.new(disqus_import_xml) | |
@discussion = @import.discussions.first | |
end | |
it "has discussions" do | |
assert_equal 'http://jeffkreeftmeijer.com/2014/using-test-fixtures-with-carrierwave/', | |
@discussion.url | |
end | |
it "has comments" do | |
comment = @discussion.comments.first | |
assert_equal '<p>Thanks for this article. I came here from your StackOverflow response. I followed these steps and the file was assigned to my model.</p><p>The problem is that I get the validation error "image_field can\'t be blank", even though everything seems in order. Have you encountered this problem?</p>', | |
comment.message | |
end | |
end | |
describe Importer::Disqus::Discussion do | |
before do | |
@disqus_discussion = Importer::Disqus::Discussion.new( | |
disqus_import_thread, disqus_import_posts | |
) | |
end | |
it "has a url" do | |
assert_equal 'http://jeffkreeftmeijer.com/2014/using-test-fixtures-with-carrierwave/', | |
@disqus_discussion.url | |
end | |
it "has comments" do | |
comment = @disqus_discussion.comments.first | |
assert_equal '<p>Thanks for this article. I came here from your StackOverflow response. I followed these steps and the file was assigned to my model.</p><p>The problem is that I get the validation error "image_field can\'t be blank", even though everything seems in order. Have you encountered this problem?</p>', | |
comment.message | |
end | |
end | |
describe Importer::Disqus::User do | |
before do | |
@disqus_user = Importer::Disqus::User.new(disqus_import_user) | |
end | |
it "has a name" do | |
assert_equal 'felix', @disqus_user.name | |
end | |
it "has a handle" do | |
assert_equal 'flx', @disqus_user.handle | |
end | |
it "has a provider" do | |
assert_equal 'disqus', @disqus_user.provider | |
end | |
end | |
describe Importer::Disqus::Comment do | |
before do | |
@comment = Importer::Disqus::Comment.new(disqus_import_post) | |
end | |
it "has a message" do | |
assert_equal '<p>Thanks for this article. I came here from your StackOverflow response. I followed these steps and the file was assigned to my model.</p><p>The problem is that I get the validation error "image_field can\'t be blank", even though everything seems in order. Have you encountered this problem?</p>', @comment.message | |
end | |
it "has a user" do | |
user = @comment.user | |
assert_equal 'felix', user.name | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment