beam 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4537b1cefd669c45e4db6e2feee8ba8644405fc7
4
- data.tar.gz: aea8de9386ece2ccf66636201dd97c43ebf00830
3
+ metadata.gz: acd3a8c0a75cf6f919a96814829bbe6d77c05898
4
+ data.tar.gz: e84bbadfac36ad545ac651725681851d05b15cf6
5
5
  SHA512:
6
- metadata.gz: 571b1f9e6e89fde10ace7122e58425248a3ba14e13414b7fdf19cee2a3aeb7e328a19f3b5cbf8d0eaf4a1edbffbd1f292fc7a52133948b25dfb04af771cac0f7
7
- data.tar.gz: 25b4e17a6e0bf135e5343cf54bf84a29e8987f248afbfcb3623ea2f8b3f238c4a295dd015b630d9a0fb6c1e7d06c42d0ae880e210113eebfacce1e886a497374
6
+ metadata.gz: 3644af866a139d8737ac62a83718768240061849c0ba38d3239aa721e46e4cb3ff3d024f41991950310b65ecacdca1d4a9892fe7f1ce109d035c3113eddf5178
7
+ data.tar.gz: 4afcf1ad50f32f32eb170f9d466cbb4e163c8a7f1a94a4b2e98d6a206dbf955281250be5b816ca01edf104345092328de3fefb26a7063f67a8014c7c3b439817
@@ -1,2 +1,5 @@
1
+ ### 0.0.2
2
+ - Added batch processing with activerecord-import
3
+
1
4
  ### 0.0.1
2
5
  - Basic version of csv uploader
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- beam (0.0.1)
4
+ beam (0.0.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,15 +1,32 @@
1
1
  # Beam
2
2
 
3
- A rubygem to simplifiy repetitive csv upload process for ActiveRecord models
3
+ A rubygem to simplifiy repetitive csv upload process for ActiveRecord models.
4
+ Supports bulk upload with [activerecord-import](http://rubygems.org/gems/activerecord-import)
4
5
 
5
6
  ## Usage
6
7
 
7
- 1. Add it to the model you want to import csv file
8
+ 1. Add it the application's Gemfile:
9
+ ```ruby
10
+ gem 'beam'
11
+ ```
12
+
13
+ Add [activerecord-import gem](http://rubygems.org/gems/activerecord-import) to the application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'activerecord-import', '0.4.1' # for rails-4.1 app
17
+ gem 'activerecord-import', '0.4.0' # for rails-4.0 app
18
+ gem 'activerecord-import', '0.3.1' # for rails-3.1+ app
19
+ ```
8
20
 
21
+ 2. Add it to the model you want to import csv file
22
+
23
+ ```ruby
9
24
  extend Beam::Upload
25
+ ```
10
26
 
11
- 2. Upload zipped csv file, e.g. users.csv.zip
12
-
27
+ 3. Upload zipped csv file, e.g. users.csv.zip
28
+
29
+ ```ruby
13
30
  Model.upload_file(file_name, file_path)
14
31
 
15
32
  # where users.csv has headers and rows, e.g.:
@@ -18,13 +35,17 @@ A rubygem to simplifiy repetitive csv upload process for ActiveRecord models
18
35
  # Test2,test2@test.com
19
36
  # Test3,test3@test.com
20
37
  # Test4,test4@test.com
38
+ ```
21
39
 
22
- 3. Get the output as:
23
-
24
- - response hash, e.g.
40
+ 4. Get the output as:
41
+
42
+ ```ruby
43
+ # response hash, e.g.
25
44
  {:errors=>1, :status=>200, :total_rows=>4, :error_rows=>[["Test1", nil, "is invalid"]]}
26
- - error file, e.g.
45
+ # error file, e.g.
27
46
  for users.csv file, it creates errors_users.csv at the same path
47
+ # see records being saved in batch(by default) of 1_000 with activerecord-import gem
48
+ ```
28
49
 
29
50
  See beam/upload.rb for more details
30
51
 
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
+
23
24
  end
@@ -6,13 +6,17 @@ module Beam
6
6
  # file_path: path/to/zip-file, e.g. Rails.root/tmp
7
7
  # error_file_needed:true if you need error file to be created, default is true
8
8
  # callback_method: method which does the job to load records, default is parse method
9
- def upload_file(file_name, file_path, error_file_needed=true, callback_method = "parse")
9
+ # batch_process: defailt is true, set to false if you do not use activerecord-import
10
+ # for batch processing
11
+ def upload_file(file_name, file_path, error_file_needed=true, callback_method = "parse", batch_process=true)
10
12
  status = {}
11
- @file_name = file_name
12
- @file_path = file_path
13
+ @file_name = file_name
14
+ @file_path = file_path
13
15
  @error_file_needed = error_file_needed
14
16
  @original_zip_file = "#{@file_path}/#{@file_name}"
15
17
  @csv_file_name = @file_name.gsub(/\.zip/,'')
18
+ @batch_process = batch_process
19
+ @batch_size = 1_1000
16
20
 
17
21
  begin
18
22
  delete_csv_if_present
@@ -59,11 +63,12 @@ module Beam
59
63
  end
60
64
 
61
65
  # parses the csv file, creates record for the model and returns response hash,e.g.
62
- # {:errors=>1, :status=>200, :total_rows=>4, :error_rows=>[["Test1", nil, "is invalid"]]}
66
+ # \{:errors=>1, :status=>200, :total_rows=>4, :error_rows=>[["Test1", nil, "is invalid"]]\}
63
67
  # also, it creates error file to consume
64
68
  def parse
65
69
  response = { errors: 0, status: 200, total_rows: 0, error_rows: []}
66
70
  index = 0
71
+ batch = []
67
72
 
68
73
  begin
69
74
  CSV.foreach("#{@file_path}/#{@csv_file_name}", :encoding => 'iso-8859-1:UTF-8', headers: true) do |row|
@@ -72,8 +77,10 @@ module Beam
72
77
  response[:total_rows] += 1
73
78
  begin
74
79
  record, response[:errors], error_row = validate_record(response[:errors], row_hash, index)
75
- if error_row
76
- response[:error_rows] << error_row
80
+ response[:error_rows] << error_row if error_row
81
+
82
+ if @batch_process
83
+ batch = add_to_batch(batch, record)
77
84
  else
78
85
  record.save!
79
86
  end
@@ -82,6 +89,7 @@ module Beam
82
89
  response[:error_rows] << log_and_return_error(row_hash, e, index)
83
90
  end
84
91
  end
92
+ import_from_batch(batch)
85
93
  rescue Exception => e
86
94
  response[:errors] = 1
87
95
  log_and_return_error({}, e, '')
@@ -96,6 +104,19 @@ module Beam
96
104
  response
97
105
  end
98
106
 
107
+ def add_to_batch(batch, record)
108
+ batch << record if record
109
+ if batch.size >= @batch_size
110
+ import_from_batch(batch, true)
111
+ batch = []
112
+ end
113
+ batch
114
+ end
115
+
116
+ def import_from_batch(batch)
117
+ import(batch, :validate => false) if @batch_process
118
+ end
119
+
99
120
  def log_and_return_validation_error(record, row_hash, index)
100
121
  error_msg = record.errors.messages.values.join(', ')
101
122
  Rails.logger.error("Error on #{index}: \n #{row_hash.values + [error_msg]}")
@@ -1,3 +1,3 @@
1
1
  module Beam
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beam
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gourav Tiwari
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-15 00:00:00.000000000 Z
11
+ date: 2013-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler