Skip to content

Commit

Permalink
Moved shared logic of Excel and Roo into separate file.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdowse committed Sep 25, 2014
1 parent 3c38d18 commit 561f726
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 109 deletions.
55 changes: 1 addition & 54 deletions lib/table_importer/excel.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module TableImporter

class Excel < Source
class Excel < RooSpreadsheetSource

def initialize(data)
begin
Expand All @@ -22,10 +22,6 @@ def initialize(data)
end
end

def get_headers
@headers
end

def get_file
begin
if @type == "xls"
Expand All @@ -41,54 +37,5 @@ def get_file
def get_type
"xls"
end

def get_preview_lines(start_point = 0, end_point = 10)
begin
@headers = @mapping.present? && @mapping != false ? convert_headers : @headers
lines = clean_chunks([get_lines(start_point, end_point)], @compulsory_headers)[0][:lines]
if lines.first.nil?
get_preview_lines(start_point+10, end_point+10)
else
lines[0..8]
end
rescue SystemStackError, NoMethodError
raise TableImporter::EmptyFileImportError.new
end
end

def get_lines(start, number_of_lines)
@last_row ||= @file.last_row
finish = [@last_row, start + number_of_lines].min
mapped_lines = []
(start...finish).each do |row_number|
mapped_lines << Hash[@headers.zip(@file.row(row_number))]
end
mapped_lines
end

def convert_headers
new_headers = @headers_present ? @file.row(1) : default_headers
new_headers = default_headers(new_headers.count)
return new_headers unless @mapping
@mapping.each do |key, value|
if value.to_i.to_s == value.to_s
new_headers[value.to_i] = key.to_sym
end
end
new_headers
end

def get_chunks(chunk_size)
@headers = convert_headers
@last_row ||= @file.last_row
chunks = []
start_point = @headers_present ? 2 : 1
while chunks.count <= @last_row/chunk_size
chunks << get_lines(start_point, chunk_size)
start_point += chunk_size
end
chunks.last << Hash[@headers.zip(@file.row(@last_row))]
clean_chunks(chunks, @compulsory_headers)
end
end
end
57 changes: 2 additions & 55 deletions lib/table_importer/google.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module TableImporter

class Google < Source
class Google < RooSpreadsheetSource

def initialize(data)
begin
@headers_present = data[:headers_present]
@file = get_file(data[:content].split(", ")[1], data[:content].split(", ")[0])
@compulsory_headers = data[:compulsory_headers]
@delete_empty_columns = false #(File.size(@file_path) < 100000)
@delete_empty_columns = false
@mapping = !data[:user_headers].blank? ? data[:user_headers] : data[:headers]
raise TableImporter::EmptyFileImportError.new if !@file.first_row
if !data[:headers].nil?
Expand All @@ -20,10 +20,6 @@ def initialize(data)
end
end

def get_headers
@headers
end

def get_file(file_key, access_token)
begin
Roo::Google.new(file_key, {:access_token => access_token})
Expand All @@ -35,54 +31,5 @@ def get_file(file_key, access_token)
def get_type
"google"
end

def get_preview_lines(start_point = 0, end_point = 10)
begin
@headers = @mapping.present? && @mapping != false ? convert_headers : @headers
lines = clean_chunks([get_lines(start_point, end_point)], @compulsory_headers)[0][:lines]
if lines.first.nil?
get_preview_lines(start_point+10, end_point+10)
else
lines[0..8]
end
rescue SystemStackError, NoMethodError
raise TableImporter::EmptyFileImportError.new
end
end

def get_lines(start, number_of_lines)
@last_row ||= @file.last_row
finish = [@last_row, start + number_of_lines].min
mapped_lines = []
(start...finish).each do |row_number|
mapped_lines << Hash[@headers.zip(@file.row(row_number))]
end
mapped_lines
end

def convert_headers
new_headers = @headers_present ? @file.row(1) : default_headers
new_headers = default_headers(new_headers.count)
return new_headers unless @mapping
@mapping.each do |key, value|
if value.to_i.to_s == value.to_s
new_headers[value.to_i] = key.to_sym
end
end
new_headers
end

def get_chunks(chunk_size)
@headers = convert_headers
@last_row ||= @file.last_row
chunks = []
start_point = @headers_present ? 2 : 1
while chunks.count <= @last_row/chunk_size
chunks << get_lines(start_point, chunk_size)
start_point += chunk_size
end
chunks.last << Hash[@headers.zip(@file.row(@last_row))]
clean_chunks(chunks, @compulsory_headers)
end
end
end
58 changes: 58 additions & 0 deletions lib/table_importer/roo_spreadsheet_source.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module TableImporter

class RooSpreadsheetSource < Source

def get_headers
@headers
end

def get_preview_lines(start_point = 0, end_point = 10)
begin
@headers = @mapping.present? && @mapping != false ? convert_headers : @headers
lines = clean_chunks([get_lines(start_point, end_point)], @compulsory_headers)[0][:lines]
if lines.first.nil?
get_preview_lines(start_point+10, end_point+10)
else
lines[0..8]
end
rescue SystemStackError, NoMethodError
raise TableImporter::EmptyFileImportError.new
end
end

def get_lines(start, number_of_lines)
@last_row ||= @file.last_row
finish = [@last_row, start + number_of_lines].min
mapped_lines = []
(start...finish).each do |row_number|
mapped_lines << Hash[@headers.zip(@file.row(row_number))]
end
mapped_lines
end

def convert_headers
new_headers = @headers_present ? @file.row(1) : default_headers
new_headers = default_headers(new_headers.count)
return new_headers unless @mapping
@mapping.each do |key, value|
if value.to_i.to_s == value.to_s
new_headers[value.to_i] = key.to_sym
end
end
new_headers
end

def get_chunks(chunk_size)
@headers = convert_headers
@last_row ||= @file.last_row
chunks = []
start_point = @headers_present ? 2 : 1
while chunks.count <= @last_row/chunk_size
chunks << get_lines(start_point, chunk_size)
start_point += chunk_size
end
chunks.last << Hash[@headers.zip(@file.row(@last_row))]
clean_chunks(chunks, @compulsory_headers)
end
end
end
1 change: 1 addition & 0 deletions lib/table_importer/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,6 @@ def remove_empty_columns(chunks, headers)
end
require 'table_importer/csv'
require 'table_importer/copy_and_paste'
require 'table_importer/roo_spreadsheet_source'
require 'table_importer/excel'
require 'table_importer/google'

0 comments on commit 561f726

Please sign in to comment.