OSDN Git Service

t#30073:importer add source
authoryasushiito <yas@pen-chan.jp>
Sun, 11 Nov 2012 05:40:27 +0000 (14:40 +0900)
committeryasushiito <yas@pen-chan.jp>
Sun, 11 Nov 2012 05:40:27 +0000 (14:40 +0900)
config/magic_number.yml
public/profile.json [new file with mode: 0644]
vendor/plugins/pettan_importer/lib/pettan_importer.rb
vendor/plugins/pettan_importer/test/import_spec.rb

index 9a6320e..de0a7b4 100644 (file)
@@ -5,3 +5,5 @@
 
   comic_visible_items: [['private', 0], ['public', 1]]
   ground_picture_repeat_items: ['repeat', 'repeat-x', 'repeat-y', 'no-repeat']
+
+  provider_sources: ['http://sourceforge.jp/projects/pettanr/wiki/ProviderSource/attach/provider_source.json']
diff --git a/public/profile.json b/public/profile.json
new file mode 100644 (file)
index 0000000..20d81ea
--- /dev/null
@@ -0,0 +1,7 @@
+{\r
+  "admin@penguinkingdom.com": {\r
+    "caption": "ペンギン王国",\r
+    "url": "http://localhost:3000/", \r
+    "description": "ペンギン王国はペンギン素材を豊富に用意したペンギンマニアのサイトです。"\r
+  }\r
+}\r
index fe9d071..ef79e29 100644 (file)
@@ -1,8 +1,9 @@
 #インポート処理
+require 'open-uri'
 require 'common'
 
-module ActiveRecord\r
-  class Base\r
+module ActiveRecord
+  class Base
     module PettanImporter
       def self.included(base)
         base.extend(ClassMethods)
@@ -52,12 +53,10 @@ module ActiveRecord
           r
         end
         
-        def import_text(data, &blk)
-          d = JSON.parse_no_except(data)
-          return false unless d
+        def import_list(attrs, &blk)
           res = []
           self.transaction do
-            d.each do |name, item|
+            attrs.each do |name, item|
               m = blk.call(name, item)
               res.push(m) unless m.valid?
             end
@@ -66,14 +65,33 @@ module ActiveRecord
           res
         end
         
+        def import_text(text, &blk)
+          attrs = JSON.parse(text)
+          import_list attrs, &blk
+        end
+        
         def import_file(filename, &blk)
-          t = nil
-          begin
-            t = File.open(filename, 'r').read
-          rescue
-            return false
+          text = File.open(filename, 'r').read
+          self.import_text text, &blk
+        end
+        
+        def import_url(url, &blk)
+          text = open(url).read
+          self.import_text text, &blk
+        end
+        
+        def import_urls(urls, &blk)
+          r = {}
+          urls.each do |url|
+            h = {}
+            begin
+              h[:validations] = import_url(url, &blk)
+            rescue
+              h[:exception] = {:location => $@, :message => $!}
+            end
+            r[url] = h
           end
-          self.import_text t, &blk
+          r
         end
         
       end
index 83052dd..7d3c1e0 100644 (file)
@@ -1,6 +1,6 @@
 # -*- encoding: utf-8 -*-
 require 'spec_helper'
-require File.expand_path(File.dirname(__FILE__) + '/import')\r
+require File.expand_path(File.dirname(__FILE__) + '/import')
 #インポート処理
 
 describe Import do
@@ -12,6 +12,7 @@ describe Import do
     @tes = '{"1": {"a": 0}, "2": {"a": 0}, "Z": {"a": 2, "b": "Z"}}'
     @jes = JSON.parse @tes
     @f = File.expand_path(File.dirname(__FILE__) + '/import.json')
+    @u = 'http://localhost:3000/profile.json'
   end
   
   describe 'システム画像インポートに於いて' do
@@ -220,64 +221,48 @@ describe Import do
     end
   end
   
-  describe 'テキスト取り込みに於いて' do
-    #成功でTrue、パース失敗でFalse、失敗は保存エラーのモデルを配列で返す
+  describe 'リストからのインポートに於いて' do
+    #インポート失敗データ(オブジェクト)のすべてを配列で返す
+    #成功したときは空の配列を返す 
     before do
     end
-    context 'つつがなく終わるとき' do
-      it 'Json解析を依頼する' do
-        JSON.should_receive(:parse_no_except).with(any_args).exactly(1)
-        JSON.stub(:parse_no_except).with(any_args).and_return(@j)
-        Import.import_text(@t) {|name, attr| Import.new(attr)}
-      end
+    context '事前チェック' do
       it '更新を一回依頼する' do
-        JSON.stub(:parse_no_except).with(any_args).and_return(@j)
         Import.any_instance.stub(:valid?).with(any_args).and_return(true)
         #newでスタブを作るとインスタンス生成ができないので、テスト用スタブで
         Import.should_receive(:etest).with(any_args).exactly(1)
-        Import.import_text(@t) {|name, attr| Import.etest ; Import.new(attr) }
-      end
-      it '[]を返す' do
-        JSON.stub(:parse_no_except).with(any_args).and_return(@j)
-        Import.any_instance.stub(:valid?).with(any_args).and_return(true)
-        Import.import_text(@t) {|name, attr| Import.new(attr)}.should eq []
+        Import.import_list(@j) {|name, attr| Import.etest ; Import.new(attr) }
       end
     end
     context '複数データがつつがなく終わるとき' do
-      it 'ライセンス更新を二回依頼する' do
-        JSON.stub(:parse_no_except).with(any_args).and_return(@js)
+      it '更新を二回依頼する' do
         Import.any_instance.stub(:valid?).with(any_args).and_return(true)
         #newでスタブを作るとインスタンス生成ができないので、テスト用スタブで
         Import.should_receive(:etest).with(any_args).exactly(2)
-        Import.import_text(@ts) {|name, attr| Import.etest ; Import.new(attr)}.should eq []
-      end
-      it '[]を返す' do
-        Import.import_text(@ts) {|name, attr| Import.new(attr)}.should eq []
+        Import.import_list(@js) {|name, attr| Import.etest ; Import.new(attr)}
       end
     end
-    #例外ケース
-    context 'Json解析に失敗したとき' do
-      before do
-        JSON.stub(:parse_no_except).with(any_args).and_return(false)
-      end
-      it 'Falseを返す' do
-        Import.import_text(@t){|name, attr| Import.new(attr)}.should be_false
+    context 'つつがなく終わるとき' do
+      it '[]を返す' do
+        r = Import.import_list(@js) {|name, attr| Import.new(attr)}
+        r.should eq []
       end
     end
+    #注意ケース
     context '作成に失敗したとき' do
       before do
         Import.any_instance.stub(:valid?).with(any_args).and_return(false)
       end
       it '配列を返す' do
-        r = Import.import_text(@t){|name, attr| Import.new(attr)}
+        r = Import.import_list(@j){|name, attr| Import.new(attr)}
         r.is_a?(Array).should be_true
       end
       it '配列の中身は一件' do
-        r = Import.import_text(@t){|name, attr| Import.new(attr)}
+        r = Import.import_list(@j){|name, attr| Import.new(attr)}
         r.should have(1).items
       end
       it 'オブジェクトが入っている' do
-        r = Import.import_text(@t){|name, attr| Import.new(attr)}
+        r = Import.import_list(@j){|name, attr| Import.new(attr)}
         r.first.is_a?(Import).should be_true
       end
     end
@@ -285,22 +270,24 @@ describe Import do
       #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
       it '行の数に変化がない' do
         lambda {
-          Import.import_text(@tes){|name, attr| Import.create(attr)}
+          Import.import_list(@jes){|name, attr| Import.create(attr)}
         }.should_not change Import, :count
       end
       it '途中で保存に失敗しても全件更新依頼する' do
-        Import.import_text(@tes){|name, attr| Import.create(attr)}
+        #newでスタブを作るとインスタンス生成ができないので、テスト用スタブで
+        Import.should_receive(:etest).with(any_args).exactly(3)
+        Import.import_list(@jes){|name, attr| Import.etest ; Import.create(attr)}
       end
       it '配列を返す' do
-        r = Import.import_text(@tes){|name, attr| Import.create(attr)}
+        r = Import.import_list(@jes){|name, attr| Import.create(attr)}
         r.is_a?(Array).should be_true
       end
       it '配列の中身は2件' do
-        r = Import.import_text(@tes){|name, attr| Import.create(attr)}
+        r = Import.import_list(@jes){|name, attr| Import.create(attr)}
         r.should have(2).items
       end
       it '配列の中身は失敗したオブジェクトが入っている' do
-        r = Import.import_text(@tes){|name, attr| Import.create(attr)}
+        r = Import.import_list(@jes){|name, attr| Import.create(attr)}
         r[0].is_a?(Import).should be_true
         r[0]["a"].should eq 0
         r[1].is_a?(Import).should be_true
@@ -309,60 +296,157 @@ describe Import do
     end
   end
   
-  describe 'インポートエラーの表示に於いて' do
+  describe 'テキスト取り込みに於いて' do
+    #インポート失敗データ(オブジェクト)のすべてを配列で返す
+    #成功したときは空の配列を返す 
+    before do
+    end
+    context '事前チェック' do
+      it 'Json解析を依頼する' do
+        JSON.stub(:parse).with(@t).and_return(@j)
+        JSON.should_receive(:parse).with(@t).exactly(1)
+        Import.import_text(@t) {|name, attr| Import.new(attr)}
+      end
+      it 'リストからのインポートを依頼する' do
+        JSON.stub(:parse).with(@t).and_return(@j)
+        Import.any_instance.stub(:import_list).with(@j).and_return([])
+        Import.should_receive(:import_list).with(@j).exactly(1)
+        Import.import_text(@t) {|name, attr| Import.new(attr) }
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it '[]を返す' do
+        Import.import_text(@t) {|name, attr| Import.new(attr)}.should eq []
+      end
+    end
+    #例外ケース
+    context 'Json解析に失敗したとき' do
+      before do
+      end
+      it '処理を中断する' do
+        lambda {
+          Import.import_text(''){|name, attr| Import.new(attr)}
+        }.should raise_error
+      end
+    end
   end
   
   describe 'ファイル取り込みに於いて' do
     before do
-      Import.stub(:import).with(any_args).and_return(true)
     end
-    context 'つつがなく終わるとき' do
+    context '事前チェック' do
       before do
-        Import.stub(:import).with(any_args).and_return(true)
-      end
-      it 'ファイルを開いてテキストを読む' do
-        File.should_receive(:open).with(@f, 'r').exactly(1)
-        Import.import_file(@f){|name, attr| Import.new(attr) }
       end
       it 'テキスト取り込みを依頼する' do
         Import.should_receive(:import_text).with(any_args).exactly(1)
         Import.import_file(@f){|name, attr| Import.new(attr) }
       end
-      it 'ブロックがあるとき更新を一回依頼する' do
-        Import.any_instance.stub(:valid?).with(any_args).and_return(true)
-        #newでスタブを作るとインスタンス生成ができないので、テスト用スタブで
-        Import.should_receive(:etest).with(any_args).exactly(1)
-        Import.import_file(@f) {|name, attr| Import.etest ; Import.new(attr) }
-      end
-      #テキスト取り込み成功でTrueが返る
-      it 'Trueを返す' do
-        Import.import_file(@f){|name, attr| Import.new(attr) }.should be_true
+    end
+    context 'つつがなく終わるとき' do
+      it 'インポート結果を返す' do
+        Import.stub(:import_text).with(any_args).and_return([])
+        r = Import.import_file(@f){|name, attr| Import.new(attr) }
+        r.is_a?(Array).should be_true
+        r.should be_empty
       end
     end
     context 'ファイルが開けないとき' do
       before do
         File.stub(:open).with(any_args).and_raise('StandardError')
       end
-      it 'ファイルエラーのメッセージを出力する' do
-        pending
-      end
-      it 'Falseを返す' do
-        Import.import_file(@f){|name, attr| Import.new(attr) }.should be_false
+      it '処理を中断する' do
+        lambda {
+          Import.import_file(@f){|name, attr| Import.new(attr)}
+        }.should raise_error
       end
     end
-    #失敗したときは、失敗したライセンスが配列で返る
-    context 'テキスト取り込みが失敗したとき' do
+  end
+  
+  describe 'Urlからのインポートに於いて' do
+    before do
+    end
+    context '事前チェック' do
       before do
-        Import.stub(:import_text).with(any_args).and_return(false)
       end
-      it '各コモンライセンスのエラーメッセージを出力する' do
-        pending
+      it 'テキスト取り込みを依頼する' do
+        Import.should_receive(:import_text).with(any_args).exactly(1)
+        Import.import_url(@u){|name, attr| Import.new(attr) }
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it 'インポート結果を返す' do
+        Import.stub(:import_text).with(any_args).and_return([])
+        r = Import.import_url(@u){|name, attr| Import.new(attr) }
+        r.is_a?(Array).should be_true
+        r.should be_empty
       end
-      it 'Falseを返す' do
-        Import.import_file(@f) {|name, attr| Import.new(attr) }.should be_false
+    end
+    context 'ファイルが開けないとき' do
+      it '処理を中断する' do
+        lambda {
+          Import.import_url('http://localhost:3000/noroute'){|name, attr| Import.new(attr)}
+        }.should raise_error
       end
     end
   end
   
+  describe '文献からのインポートに於いて' do
+    before do
+      @urls = ['urla', 'urlb']
+    end
+    context '事前チェック' do
+      before do
+      end
+      it 'Urlからのインポートを依頼する' do
+        Import.stub(:import_url).with(any_args).and_return([])
+        Import.should_receive(:import_url).with(any_args).exactly(2)
+        Import.import_urls(@urls){|name, attr| Import.new(attr) }
+      end
+    end
+    #戻り
+    #各urlのインポート結果
+    #  Hash:キー値はurl、値はHash
+    #    キー値がexceptionのとき、値はHashで、location例外発生場所とmessage例外発生理由
+    #    キー値がvalidationsのとき、値は配列で、インポート失敗データ(オブジェクト)のすべて 
+    context 'つつがなく終わるとき' do
+      it 'インポート結果を返す' do
+        Import.stub(:import_url).with(any_args).and_return([])
+        r = Import.import_urls(@urls){|name, attr| Import.new(attr) }
+        r.is_a?(Hash).should be_true
+        r.should have(2).items
+        r['urla'].is_a?(Hash).should be_true
+        r['urla'].should have(1).items
+        r['urla'][:validations].is_a?(Array).should be_true
+        r['urla'][:validations].should be_empty
+        r['urlb'].is_a?(Hash).should be_true
+        r['urlb'].should have(1).items
+        r['urlb'][:validations].is_a?(Array).should be_true
+        r['urlb'][:validations].should be_empty
+      end
+    end
+    context 'Urlからのインポートで何らかの例外が発生したとき' do
+      before do
+        Import.stub(:import_url).with(any_args).and_raise('StandardError')
+      end
+      it 'インポート結果Hashにexceptionをキーを追加する' do
+        r = Import.import_urls(@urls){|name, attr| Import.new(attr) }
+        r.is_a?(Hash).should be_true
+        r.should have(2).items
+        r['urla'].is_a?(Hash).should be_true
+        r['urla'].should have(1).items
+        r['urlb'].is_a?(Hash).should be_true
+        r['urlb'].should have(1).items
+      end
+      it '例外発生場所と例外発生理由を追加する' do
+        r = Import.import_urls(@urls){|name, attr| Import.new(attr) }
+        r['urla'][:exception].is_a?(Hash).should be_true
+        r['urla'][:exception][:location].should_not be_nil
+        r['urla'][:exception][:message].should_not be_nil
+        r['urlb'][:exception].is_a?(Hash).should be_true
+        r['urlb'][:exception][:location].should_not be_nil
+        r['urlb'][:exception][:message].should_not be_nil
+      end
+    end
+  end
 end