worker_tools 0.1.1 → 0.1.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: 7b0794f3ef2c1157bf4254ec78087218ff50dcc4
4
- data.tar.gz: 28cb0e939304f7d81becb39e6d223493b059c60e
3
+ metadata.gz: f8781038bd01108da06e6c7449cb29faaa45fdd8
4
+ data.tar.gz: eed78607ccda5d5b0ed7ad5030a42b8dc286fcea
5
5
  SHA512:
6
- metadata.gz: 78e98bd9a94c25f990f809b72d60257121467875154c2bda55a1b7ef65e25b7ecaba0faffebd932f22c775a213eb554886d87aab8080aab3458f3aaf8bc71ae8
7
- data.tar.gz: 2d910f18319f729d0b05ced99d6fc24886308439e12c01ea2a58a09ee1320e4ee83bd3d2e0f36aa1915fff11fab7d578d5134805de0da7cd3c7b7fefe2ebcd51
6
+ metadata.gz: cc5633debc66182adff1af028ae0034937be1d369abbaabdbc731da5e792480db6074b602e951efaf4ec1889d0586fd175b44df4945eff8819d637cdfec171c8
7
+ data.tar.gz: d6dcb02514729dcf459df1eaee5f40df1a792731336aea56a8b92541418163227b17144174285075e5365f98546c0a2607534c5b62845bee1e0982c94dd9bb39
data/README.md CHANGED
@@ -126,7 +126,11 @@ If you only want the logger functions, without worrying about persisting a model
126
126
 
127
127
  ## Module RocketchatErrorNotifier
128
128
 
129
- [recorder](/lib/worker_tools/rocketchat_error_notifier.rb)
129
+ [rocketchat_error_notifier](/lib/worker_tools/rocketchat_error_notifier.rb)
130
+
131
+ ## Module SlackErrorNotifier
132
+
133
+ [slack_error_notifier](/lib/worker_tools/slack_error_notifier.rb)
130
134
 
131
135
  ## Module CSV Input
132
136
 
@@ -0,0 +1,105 @@
1
+ require 'slack-notifier'
2
+
3
+ module WorkerTools
4
+ module SlackErrorNotifier
5
+ def with_wrapper_slack_error_notifier(&block)
6
+ block.yield
7
+ rescue StandardError => e
8
+ slack_error_notify(e) if slack_error_notifier_enabled
9
+ raise
10
+ end
11
+
12
+ def slack_error_notifier_enabled
13
+ Rails.env.production?
14
+ end
15
+
16
+ def slack_error_notifier_emoji
17
+ ':red_circle:'
18
+ end
19
+
20
+ def slack_error_notifier_channel
21
+ return SLACK_NOTIFIER_CHANNEL if defined?(SLACK_NOTIFIER_CHANNEL)
22
+
23
+ raise 'Define slack_error_notifier_channel or set SLACK_NOTIFIER_CHANNEL in an initializer'
24
+ end
25
+
26
+ def slack_error_notifier_webhook
27
+ return SLACK_NOTIFIER_WEBHOOK if defined?(SLACK_NOTIFIER_WEBHOOK)
28
+
29
+ raise 'Define slack_error_notifier_webhook or set SLACK_NOTIFIER_WEBHOOK in an initializer'
30
+ end
31
+
32
+ def slack_error_notifier_username
33
+ 'Notifier'
34
+ end
35
+
36
+ def slack_error_notifier_receivers
37
+ # Ex: '@all'
38
+ end
39
+
40
+ def slack_error_notifier_attachments_color
41
+ # good, warning, danger, hex color
42
+ 'danger'
43
+ end
44
+
45
+ def slack_error_notifier_title
46
+ # Example with a link:
47
+ #
48
+ # For urls a default_url_options[:host] might be necessary.
49
+ # In this example I just copy it from existing action_mailer defaults.
50
+ #
51
+ # import = slack_error_notifier_model
52
+ # host = Rails.application.config.action_mailer.default_url_options[:host]
53
+ # url = Rails.application.routes.url_helpers.import_url(import, host: host, protocol: :https)
54
+ # kind = I18n.t(import.kind, scope: 'import.kinds')
55
+ # text = "##{import.id} *#{kind}*"
56
+ # "[#{text}](#{url})"
57
+ klass = model.class.model_name.human
58
+ kind = I18n.t("activerecord.attributes.#{model.class.name.underscore}.kinds.#{model.kind}")
59
+ "#{klass} #{kind} ##{model.id}"
60
+ end
61
+
62
+ def slack_error_notifier_error_details(error)
63
+ error.backtrace[0..2].join("\n")
64
+ end
65
+
66
+ def slack_error_notifier_message
67
+ message = []
68
+ message << slack_error_notifier_receivers
69
+ message << slack_error_notifier_title
70
+ message.compact.join(' - ')
71
+ end
72
+
73
+ def slack_error_notifier_attachments(error)
74
+ [
75
+ { color: slack_error_notifier_attachments_color, fields: slack_error_notifier_attachments_fields },
76
+ {
77
+ title: [error.class, error.message].join(' : '),
78
+ color: slack_error_notifier_attachments_color,
79
+ text: slack_error_notifier_error_details(error)
80
+ }
81
+ ]
82
+ end
83
+
84
+ def slack_error_notifier_attachments_fields
85
+ [
86
+ { title: 'Application', value: Rails.application.class.parent_name, short: true },
87
+ { title: 'Environment', value: Rails.env, short: true }
88
+ ]
89
+ end
90
+
91
+ def slack_error_notifier
92
+ Slack::Notifier.new(slack_error_notifier_webhook)
93
+ end
94
+
95
+ def slack_error_notify(error)
96
+ slack_error_notifier.post(
97
+ username: slack_error_notifier_username,
98
+ channel: slack_error_notifier_channel,
99
+ icon_emoji: slack_error_notifier_emoji,
100
+ text: "*#{slack_error_notifier_message}*",
101
+ attachments: slack_error_notifier_attachments(error)
102
+ )
103
+ end
104
+ end
105
+ end
@@ -1,3 +1,3 @@
1
1
  module WorkerTools
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.1.2'.freeze
3
3
  end
@@ -0,0 +1,124 @@
1
+ require 'rubyXL'
2
+ module WorkerTools
3
+ module XlsxOutput
4
+ # if defined, this file will be written to this destination (regardless
5
+ # of whether the model saves the file as well)
6
+ def xlsx_output_target
7
+ # Ex: Rails.root.join('shared', 'foo', 'bar.xlsx')
8
+ raise "xlsx_output_target has to be defined in #{self}"
9
+ end
10
+
11
+ def xlsx_output_content
12
+ {
13
+ sheet1: {
14
+ label: 'Sheet 1',
15
+ headers: xlsx_output_column_headers,
16
+ rows: xlsx_output_values,
17
+ column_style: xlsx_output_column_format
18
+ }
19
+ }
20
+ end
21
+
22
+ def xlsx_output_values
23
+ raise "xlsx_output_values has to be defined in #{self}"
24
+ end
25
+
26
+ def xlsx_output_column_headers
27
+ # These columns are used to set the headers, also
28
+ # to set the row values depending on your implementation.
29
+ #
30
+ # To ignore them set it to _false_
31
+ #
32
+ # Ex:
33
+ # @xlsx_output_column_headers ||= {
34
+ # foo: 'Foo Header',
35
+ # bar: 'Bar Header'
36
+ # }
37
+ raise "xlsx_output_column_headers has to be defined in #{self}"
38
+ end
39
+
40
+ def xlsx_output_column_format
41
+ # These columns are used to set the headers, also
42
+ # to set the row values depending on your implementation.
43
+ #
44
+ # To ignore them set it to _false_
45
+ #
46
+ # Ex:
47
+ # @xlsx_output_column_format ||= {
48
+ # foo: { width: 10, text_wrap: true },
49
+ # bar: { width: 20, text_wrap: false }
50
+ # }
51
+ {}
52
+ end
53
+
54
+ def xlsx_output_target_folder
55
+ @xlsx_output_target_folder ||= File.dirname(xlsx_output_target)
56
+ end
57
+
58
+ def xlsx_ensure_output_target_folder
59
+ FileUtils.mkdir_p(xlsx_output_target_folder) unless File.directory?(xlsx_output_target_folder)
60
+ end
61
+
62
+ def xlsx_insert_headers(spreadsheet, headers)
63
+ return unless headers
64
+ iterator =
65
+ if headers.is_a? Hash
66
+ headers.values
67
+ else
68
+ headers
69
+ end
70
+ iterator.each_with_index do |header, index|
71
+ spreadsheet.add_cell(0, index, header.to_s)
72
+ end
73
+ end
74
+
75
+ def xlsx_insert_rows(spreadsheet, rows, headers)
76
+ rows.each_with_index do |row, row_index|
77
+ xlsx_iterators(row, headers).each_with_index do |value, col_index|
78
+ spreadsheet.add_cell(row_index + 1, col_index, value.to_s)
79
+ end
80
+ end
81
+ end
82
+
83
+ def xlsx_iterators(iterable, compare_hash = nil)
84
+ if iterable.is_a? Hash
85
+ raise 'parameter compare_hash should be a hash, too.' if compare_hash.nil? || !compare_hash.is_a?(Hash)
86
+ iterable.values_at(*compare_hash.keys)
87
+ else
88
+ iterable
89
+ end
90
+ end
91
+
92
+ def xlsx_style_columns(spreadsheet, styles, headers)
93
+ return false unless headers
94
+
95
+ xlsx_iterators(styles, headers).each_with_index do |format, index|
96
+ next unless format
97
+ spreadsheet.change_column_width(index, format[:width])
98
+ spreadsheet.change_text_wrap(index, format[:text_wrap])
99
+ end
100
+ true
101
+ end
102
+
103
+ def xlsx_write_sheet(workbook, sheet_content, index)
104
+ sheet = workbook.worksheets[index]
105
+ sheet = workbook.add_worksheet(sheet_content[:label]) if sheet.nil?
106
+
107
+ sheet.sheet_name = sheet_content[:label]
108
+ xlsx_style_columns(sheet, sheet_content[:column_style], sheet_content[:headers])
109
+ xlsx_insert_headers(sheet, sheet_content[:headers])
110
+ xlsx_insert_rows(sheet, sheet_content[:rows], sheet_content[:headers])
111
+ end
112
+
113
+ def xlsx_write_output_target
114
+ xlsx_ensure_output_target_folder
115
+
116
+ book = RubyXL::Workbook.new
117
+ xlsx_output_content.each_with_index do |(_, object), index|
118
+ xlsx_write_sheet(book, object, index)
119
+ end
120
+
121
+ book.write xlsx_output_target
122
+ end
123
+ end
124
+ end
data/worker_tools.gemspec CHANGED
@@ -28,6 +28,8 @@ Gem::Specification.new do |spec|
28
28
  spec.add_dependency 'activesupport'
29
29
  spec.add_dependency 'rocketchat-notifier', '>= 0.1.2'
30
30
  spec.add_dependency 'roo'
31
+ spec.add_dependency 'rubyXL'
32
+ spec.add_dependency 'slack-notifier'
31
33
 
32
34
  spec.add_development_dependency 'activerecord'
33
35
  spec.add_development_dependency 'bundler'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: worker_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - fsainz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-06-25 00:00:00.000000000 Z
11
+ date: 2019-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -52,6 +52,34 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubyXL
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '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'
69
+ - !ruby/object:Gem::Dependency
70
+ name: slack-notifier
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: activerecord
57
85
  requirement: !ruby/object:Gem::Requirement
@@ -228,8 +256,10 @@ files:
228
256
  - lib/worker_tools/csv_output.rb
229
257
  - lib/worker_tools/recorder.rb
230
258
  - lib/worker_tools/rocketchat_error_notifier.rb
259
+ - lib/worker_tools/slack_error_notifier.rb
231
260
  - lib/worker_tools/version.rb
232
261
  - lib/worker_tools/xlsx_input.rb
262
+ - lib/worker_tools/xlsx_output.rb
233
263
  - worker_tools.gemspec
234
264
  homepage: https://github.com/i22-digitalagentur/worker-tools
235
265
  licenses: