table_importer 0.0.10 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 604a32cfd4996ca0dd43059af2cf054c1801c33e
4
- data.tar.gz: 819ba6a0b752878a91b2904a69a5a85fd117ad00
3
+ metadata.gz: a911ef1e13dbe05d4d9eb22f6c43037368e58ef1
4
+ data.tar.gz: 2eeef08c316fcb46b3808e38f4db56fdbbedd53c
5
5
  SHA512:
6
- metadata.gz: 5a4eaaf07d5de0b172df16acb49c8bfb30076f41a4ed03ed2a4a3610be1cd05ebdb5e2af1737a708c4be8b649435fdc551ff45a1fc095c5b4c74ceec5531834b
7
- data.tar.gz: 4425cc65f6abfa31febd52f03c1f037749b1dc492af336e1c906aa155854d95b113304bd4f5f2845f4f953b287cd0b49dead9883fe197a7219c06b644417bc2a
6
+ metadata.gz: 139b56f04eaaee52f05b95d5d448a701c761dc6f120e9b9038cf33887fac2a3636db21d95ba8de131de74951149891849925b9d2a94dd955930c5665b85e4cd8
7
+ data.tar.gz: d1d12ab4f4c33c808c9b64d6dd1eb4d28cf90761fd808cdaae4d1e4932444ae0acdf731f7170ba5c2a29dc791574f6f78dfd5db5207b2d0c9c83145f1573acc2
@@ -0,0 +1,88 @@
1
+ module TableImporter
2
+
3
+ class Google < Source
4
+
5
+ def initialize(data)
6
+ begin
7
+ @headers_present = data[:headers_present]
8
+ @file = get_file(data[:content].split(", ")[1], data[:content].split(", ")[0])
9
+ @compulsory_headers = data[:compulsory_headers]
10
+ @delete_empty_columns = false #(File.size(@file_path) < 100000)
11
+ @mapping = !data[:user_headers].blank? ? data[:user_headers] : data[:headers]
12
+ raise TableImporter::EmptyFileImportError.new if !@file.first_row
13
+ if !data[:headers].nil?
14
+ @headers = data[:headers]
15
+ else
16
+ @headers = @headers_present ? @file.row(1).map.with_index { |header, index| header.present? ? header.to_sym : "column_#{index}"} : default_headers
17
+ end
18
+ rescue NoMethodError
19
+ raise TableImporter::HeaderMismatchError.new
20
+ end
21
+ end
22
+
23
+ def get_headers
24
+ @headers
25
+ end
26
+
27
+ def get_file(file_key, access_token)
28
+ begin
29
+ Roo::Google.new(file_key, {:access_token => access_token})
30
+ rescue TypeError
31
+ raise TableImporter::IncorrectFileError.new
32
+ end
33
+ end
34
+
35
+ def get_type
36
+ "google"
37
+ end
38
+
39
+ def get_preview_lines(start_point = 0, end_point = 10)
40
+ begin
41
+ @headers = @mapping.present? && @mapping != false ? convert_headers : @headers
42
+ lines = clean_chunks([get_lines(start_point, end_point)], @compulsory_headers)[0][:lines]
43
+ if lines.first.nil?
44
+ get_preview_lines(start_point+10, end_point+10)
45
+ else
46
+ lines[0..8]
47
+ end
48
+ rescue SystemStackError, NoMethodError
49
+ raise TableImporter::EmptyFileImportError.new
50
+ end
51
+ end
52
+
53
+ def get_lines(start, number_of_lines)
54
+ @last_row ||= @file.last_row
55
+ finish = [@last_row, start + number_of_lines].min
56
+ mapped_lines = []
57
+ (start...finish).each do |row_number|
58
+ mapped_lines << Hash[@headers.zip(@file.row(row_number))]
59
+ end
60
+ mapped_lines
61
+ end
62
+
63
+ def convert_headers
64
+ new_headers = @headers_present ? @file.row(1) : default_headers
65
+ new_headers = default_headers(new_headers.count)
66
+ return new_headers unless @mapping
67
+ @mapping.each do |key, value|
68
+ if value.to_i.to_s == value.to_s
69
+ new_headers[value.to_i] = key.to_sym
70
+ end
71
+ end
72
+ new_headers
73
+ end
74
+
75
+ def get_chunks(chunk_size)
76
+ @headers = convert_headers
77
+ @last_row ||= @file.last_row
78
+ chunks = []
79
+ start_point = @headers_present ? 2 : 1
80
+ while chunks.count <= @last_row/chunk_size
81
+ chunks << get_lines(start_point, chunk_size)
82
+ start_point += chunk_size
83
+ end
84
+ chunks.last << Hash[@headers.zip(@file.row(@last_row))]
85
+ clean_chunks(chunks, @compulsory_headers)
86
+ end
87
+ end
88
+ end
@@ -12,6 +12,8 @@ module TableImporter
12
12
  @source = CSV.new(data)
13
13
  when 'xls', 'xlsx'
14
14
  @source = Excel.new(data)
15
+ when 'google'
16
+ @source = Google.new(data)
15
17
  else
16
18
  raise TableImporter::IncorrectFileError.new
17
19
  end
@@ -168,3 +170,4 @@ end
168
170
  require 'table_importer/csv'
169
171
  require 'table_importer/copy_and_paste'
170
172
  require 'table_importer/excel'
173
+ require 'table_importer/google'
@@ -1,3 +1,3 @@
1
1
  module TableImporter
2
- VERSION = "0.0.10"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,10 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+ require 'roo'
4
+ require 'google_drive'
5
+
6
+ describe TableImporter::Source do
7
+
8
+ # Empty spec.
9
+
10
+ end
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_dependency "spreadsheet", "0.9.1"
22
22
  spec.add_dependency 'roo'
23
+ spec.add_dependency 'google_drive'
23
24
  spec.add_dependency 'smarter_csv'
24
25
 
25
26
  spec.add_development_dependency "bundler", "~> 1.3"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table_importer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Dowse
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-22 00:00:00.000000000 Z
11
+ date: 2014-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: spreadsheet
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: google_drive
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: smarter_csv
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -158,6 +172,7 @@ files:
158
172
  - lib/table_importer/csv.rb
159
173
  - lib/table_importer/excel.rb
160
174
  - lib/table_importer/exceptions.rb
175
+ - lib/table_importer/google.rb
161
176
  - lib/table_importer/source.rb
162
177
  - lib/table_importer/version.rb
163
178
  - spec/files/csv/edge_cases.csv
@@ -176,6 +191,7 @@ files:
176
191
  - spec/sources/copy_and_paste_spec.rb
177
192
  - spec/sources/csv_spec.rb
178
193
  - spec/sources/excel_spec.rb
194
+ - spec/sources/google_spec.rb
179
195
  - spec/spec_helper.rb
180
196
  - table_importer.gemspec
181
197
  - tasks/rspec.rake
@@ -221,4 +237,5 @@ test_files:
221
237
  - spec/sources/copy_and_paste_spec.rb
222
238
  - spec/sources/csv_spec.rb
223
239
  - spec/sources/excel_spec.rb
240
+ - spec/sources/google_spec.rb
224
241
  - spec/spec_helper.rb