Class: TableImporter::Google
- Inherits:
-
Source
- Object
- Source
- TableImporter::Google
show all
- Defined in:
- lib/table_importer/google.rb
Constant Summary
Constants inherited
from Source
Source::SEPARATORS
Instance Method Summary
collapse
Methods inherited from Source
#clean_chunks, #default_headers, #get_column_separator, #get_record_separator, #get_sep_count, #sort_separators
Constructor Details
#initialize(data) ⇒ Google
Returns a new instance of Google.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/table_importer/google.rb', line 5
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 @mapping = !data[:user_headers].blank? ? data[:user_headers] : data[:headers]
raise TableImporter::EmptyFileImportError.new if !@file.first_row
if !data[:headers].nil?
@headers = data[:headers]
else
@headers = @headers_present ? @file.row(1).map.with_index { |, index| .present? ? .to_sym : "column_#{index}"} :
end
rescue NoMethodError
raise TableImporter::HeaderMismatchError.new
end
end
|
Instance Method Details
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/table_importer/google.rb', line 63
def
= @headers_present ? @file.row(1) :
= (.count)
return unless @mapping
@mapping.each do |key, value|
if value.to_i.to_s == value.to_s
[value.to_i] = key.to_sym
end
end
end
|
#get_chunks(chunk_size) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/table_importer/google.rb', line 75
def get_chunks(chunk_size)
@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
|
#get_file(file_key, access_token) ⇒ Object
27
28
29
30
31
32
33
|
# File 'lib/table_importer/google.rb', line 27
def get_file(file_key, access_token)
begin
Roo::Google.new(file_key, {:access_token => access_token})
rescue TypeError
raise TableImporter::IncorrectFileError.new
end
end
|
23
24
25
|
# File 'lib/table_importer/google.rb', line 23
def
@headers
end
|
#get_lines(start, number_of_lines) ⇒ Object
53
54
55
56
57
58
59
60
61
|
# File 'lib/table_importer/google.rb', line 53
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
|
#get_preview_lines(start_point = 0, end_point = 10) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/table_importer/google.rb', line 39
def get_preview_lines(start_point = 0, end_point = 10)
begin
@headers = @mapping.present? && @mapping != false ? : @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
|
#get_type ⇒ Object
35
36
37
|
# File 'lib/table_importer/google.rb', line 35
def get_type
"google"
end
|