xls_reporter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 74b6a4b5417f9fb10df890a37dc87c19f31bce8a
4
+ data.tar.gz: ef56353dc21c4da727ab68d76239475c384cfd26
5
+ SHA512:
6
+ metadata.gz: 1dd4beaf15c64d7929df6ecc59e3a903c49be42f0b6b76f62362903decd3dbeff9a9de14273a82312de0265d21ea5f1d60d20cade619704c52de68616bb9f79b
7
+ data.tar.gz: f1520bd2777aead6d6d835589ed13938d2b552a5690667512adff7fcb44fa70bdb82bceb6dea99ab09f2769c450719cc3c2e8e44e81547f7d79b90b6fc4d25e8
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in xls_reporter.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 fcano
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,74 @@
1
+ # XlsReporter
2
+
3
+ XlsReporter lets you to export a collection of data to an Excel file, in a **Ruby** or **Ruby on Rails** project. Only need to specify the way to show data in the report.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'xls_reporter'
11
+ ```
12
+ And then execute:
13
+ ```sh
14
+ $ bundle install
15
+ ```
16
+ Or install it yourself as:
17
+ ```sh
18
+ $ gem install xls_reporter
19
+ ```
20
+
21
+ ## Usage
22
+ To export data:
23
+ ```ruby
24
+ # create instance of reporter class
25
+ table_reporter = XlsReporter::DataExport.new
26
+
27
+ # generate report file
28
+ table_reporter.generate_excel(collection, header_params, body_params,
29
+ filename, filepath, batch_iteration, retry_methods)
30
+ ```
31
+ with the follow parameters:
32
+ - *collection*: set of iterable data (`ActiveRecord::Relation`, `Array` of objects, etc.).
33
+ - *header_params*: `Array` of hashes, indicate the title of each column:
34
+ ```ruby
35
+ header_params = [
36
+ {:index => 0, :title => "SKU"},
37
+ {:index => 1, :title => "Event"},
38
+ {:index => 2, :title => I18n.t(model.attribute)},
39
+ ]
40
+ ```
41
+ - *body_params*: `Array` of hashes, indicate the methods (and their params) to apply in each element of collection to show in report:
42
+ ```ruby
43
+ body_params = [
44
+ # one method
45
+ {:index => 0, :object_methods => :get_sku },
46
+ # one method with one param
47
+ {:index => 1, :object_methods => {:get_event => Time.now } },
48
+ # method chaining with params
49
+ {:index => 2, :object_methods => [
50
+ # one method with multiple params
51
+ {:method_name=>:values_by_ids, :params_values => [1,2,4]},
52
+ # one method
53
+ :get_color ,
54
+ #one method with one param
55
+ {:method_name =>:set_size, :params_values => "large"}
56
+ ]
57
+ },
58
+ ]
59
+ ```
60
+ - *filename*: `String` with the name of the exported file (ie: *report.xlsx*).
61
+ - *filepath*: `String` with the path for save the report file.
62
+ - *batch_iteration*: this `boolean` option permit iterate data in [batches](http://api.rubyonrails.org/classes/ActiveRecord/Batches.html#method-i-find_each) if you use `ActiveRecord`. Default is **true**.
63
+ - *retry_methods*: this `boolean` option is used to manage exceptions when calling methods in data elements, and retry them. Default is **true**.
64
+
65
+ ## TO DO
66
+ Write tests and validate code.
67
+
68
+ ## Contributing
69
+
70
+ 1. Fork it ( https://github.com/[my-github-username]/xls_reporter/fork )
71
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
72
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
73
+ 4. Push to the branch (`git push origin my-new-feature`)
74
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,115 @@
1
+ require "xls_reporter/version"
2
+
3
+ module XlsReporter
4
+ class DataExport
5
+ require 'write_xlsx'
6
+
7
+ NUMBER_OF_RETRIES = 5
8
+
9
+ # generate_excel: Main method to generate file
10
+ # ----------------------------------------------------------------------------
11
+ # Valid header_params format:
12
+ # header_params = [
13
+ # {:index => 0, :title => I18n.t(model.attributes)},
14
+ # {:index => 1, :title => "Event"},
15
+ # {:index => 2, :title => "SKU"},
16
+ # ]
17
+ # ----------------------------------------------------------------------------
18
+ # Valid body_params format:
19
+ # body_params = [
20
+ # {:index => 0, :object_methods => [ {:method_name=>:values_by_ids, :params_values => [1,2,4]}, :get_color , {:method_name =>:set_size, :params_values => "large"}] },
21
+ # {:index => 1, :object_methods => {:get_event => Time.now } },
22
+ # {:index => 2, :object_methods => :get_sku },
23
+ # ]
24
+ def generate_excel collection, header_params, body_params, filename="report_#{Time.now.strftime("%Y%m%d%H%M")}.xlsx", filepath = default_path, batch_iteration = true, retry_methods = true
25
+
26
+ fullpath= File.join(filepath, filename)
27
+ puts "#{Time.now} Generating #{fullpath}"
28
+
29
+ workbook = WriteXLSX.new(fullpath)
30
+ worksheet = workbook.add_worksheet
31
+ row = 0
32
+
33
+ # Set Headers
34
+ if header_params
35
+ header_params.each do |param|
36
+ worksheet.write(row, param[:index], param[:title])
37
+ end
38
+ row = row + 1
39
+ end
40
+
41
+ # Use batch iteration if ActiveRecord is defined and param is set
42
+ iterate_method = defined?(ActiveRecord) && collection.is_a?(ActiveRecord::Relation) && batch_iteration ? "find_each" : "each"
43
+
44
+ if body_params
45
+ collection.send(iterate_method) do |object|
46
+ body_params.each do |param|
47
+
48
+ # Get object column value depending of datatype of args
49
+ object_value = if param[:object_methods].is_a? Array #Multiple methods
50
+ #Chaining methods across the array
51
+ param[:object_methods].reduce(object) do |method_params, chained_value|
52
+ call_method chained_value, method_params, retry_methods ? 0 : -1
53
+ end
54
+ else
55
+ call_method object, param[:object_methods], retry_methods ? 0 : -1
56
+ end
57
+
58
+ worksheet.write(row, param[:index], object_value)
59
+ end
60
+
61
+ row = row + 1
62
+ end
63
+ end
64
+
65
+ workbook.close
66
+ puts "#{Time.now} Exporting to #{fullpath} done!"
67
+
68
+ end
69
+
70
+
71
+ private
72
+
73
+ # Call methods for each object and retry if fail
74
+ def call_method object, method_params, retry_count = -1
75
+ begin
76
+ if method_params.is_a? Hash
77
+ if method_params.key?(:method_name) && method_params.key?(:params_values) # {:method _name=>:values_by_ids, :params_values => [1,2,4]}
78
+ method_name = method_params[:method_name]
79
+ object.send method_name, *method_params[:params_values]
80
+ else # {:get_event => Time.now }
81
+ method_name = method_params.flatten.first
82
+ object.send method_name.to_sym, method_params[method_name]
83
+ end
84
+ else # method_name
85
+ method_name = method_params
86
+ object.send method_name.to_sym
87
+ end
88
+ rescue Exception => e
89
+ puts "Error calling method method_name. #{e}"
90
+
91
+ if retry_count > 0 && retry_count <= NUMBER_OF_RETRIES
92
+ seconds_to_sleep = (Random.rand(5) + 5)*(retry_count+1) # (5 + 0..5) x (#retries + 1) seconds
93
+ puts "Retry call method in #{seconds_to_sleep} seconds"
94
+
95
+ sleep seconds_to_sleep
96
+ call_method object, method_params, retry_count+1
97
+ else
98
+ puts "Retries finished or ignored, filling with blank"
99
+ ""
100
+ end
101
+ end
102
+ end
103
+
104
+ # Rails defined?
105
+ def rails?
106
+ defined? Rails
107
+ end
108
+
109
+ #Default values of main method
110
+ def default_path
111
+ rails? ? Rails.public_path : File.expand_path(File.dirname(__FILE__))
112
+ end
113
+
114
+ end
115
+ end
@@ -0,0 +1,3 @@
1
+ module XlsReporter
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'xls_reporter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "xls_reporter"
8
+ spec.version = XlsReporter::VERSION
9
+ spec.authors = ["Felipe Cano"]
10
+ spec.email = ["felicanoo@gmail.com"]
11
+ spec.summary = "Export data to Excel file."
12
+ spec.description = "Export data to Excel file. Iterate in a collection and map values in each row to fill Excel spreadsheet"
13
+ spec.homepage = "https://github.com/facano"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "pry"
24
+
25
+ spec.add_dependency "write_xlsx", "~> 0.76.0"
26
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xls_reporter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Felipe Cano
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-10 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: write_xlsx
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.76.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.76.0
69
+ description: Export data to Excel file. Iterate in a collection and map values in
70
+ each row to fill Excel spreadsheet
71
+ email:
72
+ - felicanoo@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/xls_reporter.rb
83
+ - lib/xls_reporter/version.rb
84
+ - xls_reporter.gemspec
85
+ homepage: https://github.com/facano
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.4.6
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Export data to Excel file.
109
+ test_files: []