From 4cc134afb477c4b4ee63f2a1214fb1955f26e0a0 Mon Sep 17 00:00:00 2001
From: nishiki <nishiki@yaegashi.fr>
Date: Thu, 19 Mar 2015 22:52:33 +0100
Subject: [PATCH] fix bug in preview import

---
 lib/MPW.rb    | 45 ++++++++++++++++++++++++++++++++++++---------
 lib/UI/Cli.rb |  2 ++
 2 files changed, 38 insertions(+), 9 deletions(-)

diff --git a/lib/MPW.rb b/lib/MPW.rb
index c948606..45f355e 100644
--- a/lib/MPW.rb
+++ b/lib/MPW.rb
@@ -126,9 +126,9 @@ module MPW
 		def list(options={})
 			result = []
 	
-			search    = options[:search].to_s.downcase
-			group     = options[:group].to_s.downcase
-			protocol  = options[:protocol].to_s.downcase
+			search   = options[:search].to_s.downcase
+			group    = options[:group].to_s.downcase
+			protocol = options[:protocol].to_s.downcase
 
 			@data.each do |item|
 				next if not group.empty?    and not group.eql?(item.group.downcase)
@@ -259,22 +259,49 @@ module MPW
 		# @args: file -> path to file import
 		# @rtrn: a hash with the items to import, if there is an error return false
 		def import_preview(file, type=:yaml)
-			result = []
+			data = []
+
 			case type
 			when :csv
 				CSV.foreach(file, {headers: true}) do |row|
-					result << row
+					item = Item.new(name:     row['name'], 
+					                group:    row['group'],
+					                host:     row['host'],
+					                protocol: row['protocol'],
+					                user:     row['user'],
+					                password: row['password'],
+					                port:     row['port'],
+					                comment:  row['comment'],
+					               )
+
+					return false if item.empty?
+
+					data.push(item)
 				end
+
 			when :yaml
-				YAML::load_file(file).each do |k, row| 
-					result << row
+				YAML::load_file(file).each_value do |row| 
+					item = Item.new(name:     row['name'], 
+					                group:    row['group'],
+					                host:     row['host'],
+					                protocol: row['protocol'],
+					                user:     row['user'],
+					                password: row['password'],
+					                port:     row['port'],
+					                comment:  row['comment'],
+					               )
+
+					return false if item.empty?
+
+					data.push(item)
 				end
+
 			else
 				@error_msg = "#{I18n.t('error.export.unknown_type', type: type)}"
 				return false
 			end
-
-			return result
+	
+			return data
 		rescue Exception => e 
 			@error_msg = "#{I18n.t('error.import.read', file: file)}\n#{e}"
 			return false
diff --git a/lib/UI/Cli.rb b/lib/UI/Cli.rb
index 9352ed9..35f3399 100644
--- a/lib/UI/Cli.rb
+++ b/lib/UI/Cli.rb
@@ -298,8 +298,10 @@ class Cli
 
 		if not force
 			result = @mpw.import_preview(file, type)
+			puts result
 			if result.is_a?(Array) and not result.empty?
 				result.each do |r|
+					puts r.class
 					display_item(r)
 				end