zenaton 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Style/ClassVars
4
+ require 'active_support'
5
+ require 'zenaton/exceptions'
6
+ require 'zenaton/traits/with_duration'
7
+
8
+ module Zenaton
9
+ module Traits
10
+ # Module to calculate unix timestamps for events
11
+ module WithTimestamp
12
+ include WithDuration
13
+ extend ActiveSupport::Concern
14
+
15
+ # Array of weekdays as symbols from ActiveSupport
16
+ WEEKDAYS = DateAndTime::Calculations::DAYS_INTO_WEEK.keys
17
+ MODE_AT = 'AT' # When specifying a time
18
+ MODE_WEEK_DAY = 'WEEK_DAY' # When specifying a day of the week
19
+ MODE_MONTH_DAY = 'MONTH_DAY' # When specifying a day of the month
20
+ MODE_TIMESTAMP = 'TIMESTAMP' # When specifying a unix timestamp
21
+
22
+ included do
23
+ @@_timezone = nil
24
+ end
25
+
26
+ # Calculates the timestamp based on either timestamp or duration methods
27
+ # @return Array<Integer, NilClass>
28
+ def _get_timestamp_or_duration
29
+ return [nil, nil] unless @buffer
30
+ now, now_dup = _init_now_then
31
+ @_mode = nil
32
+ @buffer.each do |time_unit, time_value|
33
+ now_dup = _apply(time_unit, time_value, now, now_dup)
34
+ end
35
+ return [nil, diff_in_seconds(now, now_dup)] if @_mode.nil?
36
+ [now_dup.to_i, nil]
37
+ end
38
+
39
+ %i[
40
+ timestamp at on_day monday tuesday wednesday thursday
41
+ friday saturday sunday
42
+ ]. each do |method_name|
43
+ define_method method_name do |value = 1|
44
+ _push(method_name, value)
45
+ self
46
+ end
47
+ end
48
+
49
+ private
50
+
51
+ # rubocop:disable Metrics/MethodLength
52
+ def _apply(method, value, now, now_dup)
53
+ if WEEKDAYS.include?(method)
54
+ _weekday(value, method, now_dup)
55
+ elsif method == :timestamp
56
+ _timestamp(value)
57
+ elsif method == :at
58
+ _at(value, now, now_dup)
59
+ elsif method == :on_day
60
+ _on_day(value, now, now_dup)
61
+ else
62
+ _apply_duration(method, value, now_dup)
63
+ end
64
+ end
65
+ # rubocop:enable Metrics/MethodLength
66
+
67
+ def _weekday(value, day, now_dup)
68
+ _set_mode(MODE_WEEK_DAY)
69
+ value.times { |_n| now_dup = now_dup.next_occurring(day) }
70
+ now_dup
71
+ end
72
+
73
+ def _timestamp(timestamp)
74
+ _set_mode(MODE_TIMESTAMP)
75
+ timestamp
76
+ end
77
+
78
+ def _at(time, now, now_dup)
79
+ _set_mode(MODE_AT)
80
+ hour, min, sec = time.split(':').map(&:to_i)
81
+ now_dup = now_dup.change(hour: hour, min: min, sec: sec || 0)
82
+ now_dup += delay if now > now_dup
83
+ now_dup
84
+ end
85
+
86
+ def delay
87
+ case @_mode
88
+ when MODE_AT
89
+ 1.day
90
+ when MODE_WEEK_DAY
91
+ 1.week
92
+ when MODE_MONTH_DAY
93
+ 1.month
94
+ else
95
+ raise InternalError "Unknown mode: #{@_mode}"
96
+ end
97
+ end
98
+
99
+ def _on_day(day, now, now_dup)
100
+ _set_mode(MODE_MONTH_DAY)
101
+ now_dup = now_dup.change(day: day)
102
+ now_dup += 1.month if now > now_dup
103
+ now_dup
104
+ end
105
+
106
+ def _set_mode(mode)
107
+ error = 'Incompatible definition in Wait methods'
108
+ raise ExternalError, error if mode == @_mode
109
+ raise ExternalError, error if timestamp_mode_set?(mode)
110
+ @_mode = mode if @_mode.nil? || @_mode == MODE_AT
111
+ end
112
+
113
+ def timestamp_mode_set?(mode)
114
+ (!@_mode.nil? && MODE_TIMESTAMP == mode) || (@_mode == MODE_TIMESTAMP)
115
+ end
116
+
117
+ class_methods do
118
+ def timezone=(timezone)
119
+ error = 'Unknown timezone'
120
+ raise ExternalError, error unless valid_timezone?(timezone)
121
+ @@_timezone = timezone
122
+ end
123
+
124
+ def valid_timezone?(timezone)
125
+ timezone.nil? || ActiveSupport::TimeZone::MAPPING.value?(timezone)
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
131
+ # rubocop:enable Style/ClassVars
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require 'zenaton/engine'
5
+ require 'zenaton/query/builder'
6
+
7
+ module Zenaton
8
+ # Reusable modules from the Zenaton library
9
+ module Traits
10
+ # Module to be included in tasks and workflows
11
+ module Zenatonable
12
+ extend ActiveSupport::Concern
13
+ # Sends self as the single job to be executed to the engine and returns
14
+ # the result
15
+ def execute
16
+ Engine.instance.execute([self])[0]
17
+ end
18
+
19
+ # Sends self as the single job to be dispatched to the engine and returns
20
+ # the result
21
+ def dispatch
22
+ Engine.instance.dispatch([self])
23
+ end
24
+
25
+ class_methods do
26
+ # Search for workflows to interact with.
27
+ # For available methods, see {Zenaton::Query::Builder}
28
+ # @param id [String] (Optional) ID for a given worflow
29
+ # @return [Zenaton::Query::Builder] a query builder object
30
+ def where_id(id)
31
+ Query::Builder.new(self).where_id(id)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Zenaton
4
+ # This gem's current version
5
+ VERSION = '0.1.0'
6
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'zenaton/exceptions'
4
+ require 'zenaton/interfaces/workflow'
5
+ require 'zenaton/traits/zenatonable'
6
+
7
+ module Zenaton
8
+ # Module for the workflow manager and the versioned workflows
9
+ module Workflows
10
+ # @abstract Subclass and override {#versions} to create your own versionned
11
+ # workflows
12
+ class Version < Interfaces::Workflow
13
+ include Traits::Zenatonable
14
+
15
+ # @return [Array<Class>] an array containing the class name for each
16
+ # version, ordered from the oldest to the most recent version
17
+ def versions
18
+ raise NotImplemented,
19
+ "Please override the `versions' method in your subclass"
20
+ end
21
+
22
+ def initialize(*args)
23
+ @args = args
24
+ end
25
+
26
+ # Calls handle on the current implementation
27
+ def handle
28
+ current_implementation.handle
29
+ end
30
+
31
+ # Get the current implementation class
32
+ # @return [Class]
33
+ def current
34
+ _get_versions[-1]
35
+ end
36
+
37
+ # Get the first implementation class
38
+ # @return [Class]
39
+ def initial
40
+ _get_versions[0]
41
+ end
42
+
43
+ # Returns an instance of the current implementation
44
+ # @return [Zenaton::Interfaces::Workflow]
45
+ def current_implementation
46
+ current.new(*@args)
47
+ end
48
+
49
+ protected
50
+
51
+ def _get_versions
52
+ raise ExternalError unless versions.is_a? Array
53
+ raise ExternalError unless versions.any?
54
+ versions.each do |version|
55
+ raise ExternalError unless version < Interfaces::Workflow
56
+ end
57
+ versions
58
+ end
59
+ end
60
+ end
61
+ end
data/zenaton.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'zenaton/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'zenaton'
9
+ spec.version = Zenaton::VERSION
10
+ spec.authors = ['Zenaton']
11
+ spec.email = ['contact@zenaton.com']
12
+
13
+ spec.summary = 'Zenaton ruby library'
14
+ spec.description = 'Zenaton ruby library'
15
+ spec.homepage = 'https://zenaton.com'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_runtime_dependency 'activesupport'
26
+ spec.add_runtime_dependency 'httparty'
27
+ spec.add_runtime_dependency 'tzinfo-data'
28
+
29
+ spec.add_development_dependency 'bundler', '~> 1.16'
30
+ spec.add_development_dependency 'pry'
31
+ spec.add_development_dependency 'rake', '~> 10.0'
32
+ spec.add_development_dependency 'rspec', '~> 3.0'
33
+ spec.add_development_dependency 'rubocop'
34
+ spec.add_development_dependency 'rubocop-rspec'
35
+ spec.add_development_dependency 'simplecov'
36
+ spec.add_development_dependency 'timecop'
37
+ spec.add_development_dependency 'vcr'
38
+ spec.add_development_dependency 'webmock'
39
+ end
metadata ADDED
@@ -0,0 +1,260 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zenaton
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Zenaton
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-08-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: httparty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: tzinfo-data
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'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.16'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.16'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop-rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: simplecov
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: timecop
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: vcr
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: webmock
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ description: Zenaton ruby library
196
+ email:
197
+ - contact@zenaton.com
198
+ executables: []
199
+ extensions: []
200
+ extra_rdoc_files: []
201
+ files:
202
+ - ".circleci/config.yml"
203
+ - ".gitignore"
204
+ - ".rspec"
205
+ - ".rubocop.yml"
206
+ - ".yardopts"
207
+ - CODE_OF_CONDUCT.md
208
+ - Gemfile
209
+ - Gemfile.lock
210
+ - LICENSE.txt
211
+ - README.md
212
+ - Rakefile
213
+ - bin/console
214
+ - bin/setup
215
+ - lib/zenaton.rb
216
+ - lib/zenaton/client.rb
217
+ - lib/zenaton/engine.rb
218
+ - lib/zenaton/exceptions.rb
219
+ - lib/zenaton/interfaces/event.rb
220
+ - lib/zenaton/interfaces/job.rb
221
+ - lib/zenaton/interfaces/task.rb
222
+ - lib/zenaton/interfaces/workflow.rb
223
+ - lib/zenaton/parallel.rb
224
+ - lib/zenaton/processor.rb
225
+ - lib/zenaton/query/builder.rb
226
+ - lib/zenaton/services/http.rb
227
+ - lib/zenaton/services/properties.rb
228
+ - lib/zenaton/services/serializer.rb
229
+ - lib/zenaton/tasks/wait.rb
230
+ - lib/zenaton/traits/with_duration.rb
231
+ - lib/zenaton/traits/with_timestamp.rb
232
+ - lib/zenaton/traits/zenatonable.rb
233
+ - lib/zenaton/version.rb
234
+ - lib/zenaton/workflows/version.rb
235
+ - zenaton.gemspec
236
+ homepage: https://zenaton.com
237
+ licenses:
238
+ - MIT
239
+ metadata: {}
240
+ post_install_message:
241
+ rdoc_options: []
242
+ require_paths:
243
+ - lib
244
+ required_ruby_version: !ruby/object:Gem::Requirement
245
+ requirements:
246
+ - - ">="
247
+ - !ruby/object:Gem::Version
248
+ version: '0'
249
+ required_rubygems_version: !ruby/object:Gem::Requirement
250
+ requirements:
251
+ - - ">="
252
+ - !ruby/object:Gem::Version
253
+ version: '0'
254
+ requirements: []
255
+ rubyforge_project:
256
+ rubygems_version: 2.7.6
257
+ signing_key:
258
+ specification_version: 4
259
+ summary: Zenaton ruby library
260
+ test_files: []