OSDN Git Service

fix panel editor
[pettanr/pettanr.git] / vendor / plugins / pettan_importer / lib / pettan_importer.rb
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