beam 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4537b1cefd669c45e4db6e2feee8ba8644405fc7
4
+ data.tar.gz: aea8de9386ece2ccf66636201dd97c43ebf00830
5
+ SHA512:
6
+ metadata.gz: 571b1f9e6e89fde10ace7122e58425248a3ba14e13414b7fdf19cee2a3aeb7e328a19f3b5cbf8d0eaf4a1edbffbd1f292fc7a52133948b25dfb04af771cac0f7
7
+ data.tar.gz: 25b4e17a6e0bf135e5343cf54bf84a29e8987f248afbfcb3623ea2f8b3f238c4a295dd015b630d9a0fb6c1e7d06c42d0ae880e210113eebfacce1e886a497374
@@ -0,0 +1,2 @@
1
+ ### 0.0.1
2
+ - Basic version of csv uploader
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sleek_charts.gemspec
4
+ gemspec
@@ -0,0 +1,17 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ beam (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (10.1.0)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ beam!
16
+ bundler (~> 1.3)
17
+ rake
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Gourav Tiwari
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ # Beam
2
+
3
+ A rubygem to simplifiy repetitive csv upload process for ActiveRecord models
4
+
5
+ ## Usage
6
+
7
+ 1. Add it to the model you want to import csv file
8
+
9
+ extend Beam::Upload
10
+
11
+ 2. Upload zipped csv file, e.g. users.csv.zip
12
+
13
+ Model.upload_file(file_name, file_path)
14
+
15
+ # where users.csv has headers and rows, e.g.:
16
+ # name,email
17
+ # Test1,
18
+ # Test2,test2@test.com
19
+ # Test3,test3@test.com
20
+ # Test4,test4@test.com
21
+
22
+ 3. Get the output as:
23
+
24
+ - response hash, e.g.
25
+ {:errors=>1, :status=>200, :total_rows=>4, :error_rows=>[["Test1", nil, "is invalid"]]}
26
+ - error file, e.g.
27
+ for users.csv file, it creates errors_users.csv at the same path
28
+
29
+ See beam/upload.rb for more details
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'beam/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "beam"
8
+ spec.version = Beam::VERSION
9
+ spec.authors = ["Gourav Tiwari"]
10
+ spec.email = ["gouravtiwari21@gmail.com"]
11
+ spec.homepage = "https://github.com/gouravtiwari/beam"
12
+ spec.summary = "CSV uploader library for fast and easy upload"
13
+ spec.description = "CSV uploader library for fast and easy upload"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require "beam/version"
3
+ require 'beam/extensions/exception'
4
+ require 'beam/upload'
@@ -0,0 +1,10 @@
1
+ class Exception
2
+ def formatted_exception(error_message)
3
+ <<-ERROR
4
+ An error occurred while #{error_message}:
5
+ Type: #{self.class.name}
6
+ Message: #{self.message}
7
+ Backtrace:\n#{self.backtrace.join("\n")}
8
+ ERROR
9
+ end
10
+ end
@@ -0,0 +1,118 @@
1
+ require 'csv'
2
+ module Beam
3
+ module Upload
4
+ # params:
5
+ # file_name: zip file name, e.g. users.csv.zip
6
+ # file_path: path/to/zip-file, e.g. Rails.root/tmp
7
+ # error_file_needed:true if you need error file to be created, default is true
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")
10
+ status = {}
11
+ @file_name = file_name
12
+ @file_path = file_path
13
+ @error_file_needed = error_file_needed
14
+ @original_zip_file = "#{@file_path}/#{@file_name}"
15
+ @csv_file_name = @file_name.gsub(/\.zip/,'')
16
+
17
+ begin
18
+ delete_csv_if_present
19
+ `unzip #{@file_path}/#{@file_name} -d #{@file_path}`
20
+
21
+ status = self.send(callback_method)
22
+ rescue Exception => e
23
+ error_in_upload([[invalid_file_message]]) if @error_file_needed
24
+ Rails.logger.error e.formatted_exception("Uploading file #{@file_name} failed!")
25
+ status = { errors: 1, status: 500}
26
+ end
27
+ status
28
+ end
29
+
30
+ # deletes csv file upfront before we unzip the file
31
+ def delete_csv_if_present
32
+ File.delete "#{@file_path}/#{@csv_file_name}" if File.exists? "#{@file_path}/#{@csv_file_name}"
33
+ end
34
+
35
+ # Creates error file with error rows, e.g.
36
+ # for users.csv file, it creates errors_users.csv
37
+ def error_in_upload(rows)
38
+ csv_file_path = "#{@file_path}/errors_#{@csv_file_name}"
39
+ begin
40
+ CSV.open(csv_file_path, "wb") do |csv|
41
+ rows.each do |row|
42
+ csv << row
43
+ end
44
+ end
45
+ rescue Exception => e
46
+ Rails.logger.error e.formatted_exception("Building error file for #{@file_name} failed!")
47
+ end
48
+ end
49
+
50
+ # Validates each record and returns error count if record is invalid
51
+ def validate_record(errors_count, row_hash, index)
52
+ record = new(row_hash)
53
+ unless record.valid?
54
+ errors_count += 1
55
+ [nil, errors_count, log_and_return_validation_error(record, row_hash, index)]
56
+ else
57
+ [record, errors_count, nil]
58
+ end
59
+ end
60
+
61
+ # 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"]]}
63
+ # also, it creates error file to consume
64
+ def parse
65
+ response = { errors: 0, status: 200, total_rows: 0, error_rows: []}
66
+ index = 0
67
+
68
+ begin
69
+ CSV.foreach("#{@file_path}/#{@csv_file_name}", :encoding => 'iso-8859-1:UTF-8', headers: true) do |row|
70
+ index += 1
71
+ row_hash = row.to_hash
72
+ response[:total_rows] += 1
73
+ begin
74
+ record, response[:errors], error_row = validate_record(response[:errors], row_hash, index)
75
+ if error_row
76
+ response[:error_rows] << error_row
77
+ else
78
+ record.save!
79
+ end
80
+ rescue Exception => e
81
+ response[:errors] +=1
82
+ response[:error_rows] << log_and_return_error(row_hash, e, index)
83
+ end
84
+ end
85
+ rescue Exception => e
86
+ response[:errors] = 1
87
+ log_and_return_error({}, e, '')
88
+ end
89
+
90
+ if response[:error_rows].blank?
91
+ post_parse_success_calls
92
+ elsif @error_file_needed
93
+ error_in_upload(response[:error_rows])
94
+ end
95
+
96
+ response
97
+ end
98
+
99
+ def log_and_return_validation_error(record, row_hash, index)
100
+ error_msg = record.errors.messages.values.join(', ')
101
+ Rails.logger.error("Error on #{index}: \n #{row_hash.values + [error_msg]}")
102
+ row_hash.values + [error_msg]
103
+ end
104
+
105
+ def log_and_return_error(row_hash, e, index)
106
+ Rails.logger.error e.formatted_exception("Error on #{index}: \n #{row_hash.values}")
107
+ row_hash.values + [invalid_file_message]
108
+ end
109
+
110
+ def invalid_file_message
111
+ "Please upload the right template and verify data before upload"
112
+ end
113
+
114
+ def post_parse_success_calls
115
+ # abstract method
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,3 @@
1
+ module Beam
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: beam
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Gourav Tiwari
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: CSV uploader library for fast and easy upload
42
+ email:
43
+ - gouravtiwari21@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - CHANGELOG.md
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - beam.gemspec
55
+ - lib/beam.rb
56
+ - lib/beam/extensions/exception.rb
57
+ - lib/beam/upload.rb
58
+ - lib/beam/version.rb
59
+ homepage: https://github.com/gouravtiwari/beam
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.0.6
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: CSV uploader library for fast and easy upload
83
+ test_files: []