ifocustt 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2bd3f3e307098164e09d05812cf24434f68d5e92
4
- data.tar.gz: d978c002f1d7749c19adcaa2e125310540127b0e
3
+ metadata.gz: 0dfe2365976e3a78b145968a9fab127547b5ac71
4
+ data.tar.gz: baa95c3b6ef52e3ce59fdb2b1dc8c3d3c44a61a3
5
5
  SHA512:
6
- metadata.gz: 28adf588ebdc02237f7ce06495e96c36e30ff009987f50b306699f7af9ae6697310a09be47c8ee3a6b07b36a82d4782d584ed8281e154632a8351fb0490cf9fa
7
- data.tar.gz: 80d221e8b0bdf32f793fcca8bf2f95e5b500e406ff5a6c67da230c4620f07da890303f3d89e869479eb368fb62f4a9174341ab59810ae3f7cf1d53faf873a4bd
6
+ metadata.gz: c40175f09eb1a141a0e9ed408b65749f619431070a422e5ee1a9bd5be45c1e42867dad4390cbbcdfd8d72992dfd5833e652df197494651a111b0304bdeffc69d
7
+ data.tar.gz: 616585264fe4dda64aa48cd16f07a795f17e69dbcaf9ac6137fa517a90c9d195660df4159ff8cb832d3b80d8493f482cca1aa6f03c76a655724bd9e6c4fb3546
@@ -6,6 +6,6 @@ actions:
6
6
 
7
7
  - OnBreak:
8
8
 
9
- - OnCompletion:
9
+ - Cleanup:
10
10
  - ToggleMacNotificationCenter:
11
11
  enabled: true
@@ -2,6 +2,8 @@ require "httparty"
2
2
  require "interactor"
3
3
 
4
4
  module Focus
5
+ class FailedActionError < StandardError; end
6
+
5
7
  class Action
6
8
  include Interactor
7
9
 
@@ -19,6 +21,10 @@ module Focus
19
21
  context.minutes.to_f * 60
20
22
  end
21
23
 
24
+ def seconds_focused
25
+ Time.now.to_i - context.focus_start
26
+ end
27
+
22
28
  def break_seconds
23
29
  focus_seconds * 0.2
24
30
  end
@@ -28,12 +34,21 @@ module Focus
28
34
  step = "Running #{action}..."
29
35
 
30
36
  Focus::STDOUT.step(step, quiet: context.quiet) do
31
- klass.call(args)
37
+ result = klass.call(args)
38
+ raise FailedActionError, error_message(result) unless result.success?
32
39
  end
33
40
  end
34
41
 
42
+ def error_message(obj)
43
+ "#{obj.action}: #{obj.error}"
44
+ end
45
+
35
46
  def debug_output(*args)
36
47
  Focus::STDOUT.debug_output args
37
48
  end
49
+
50
+ def fail_action!(opts = {})
51
+ context.fail!(opts.merge(action: self.class.to_s.split("::").last))
52
+ end
38
53
  end
39
54
  end
@@ -1,7 +1,7 @@
1
1
  module Focus
2
2
  class PostWorkLogToJira < Action
3
3
  def call
4
- return unless jira_ticket && focus_seconds >= 60
4
+ return unless jira_ticket && seconds_focused >= 60
5
5
  HTTParty.post(issue_url, options)
6
6
  end
7
7
 
@@ -20,7 +20,7 @@ module Focus
20
20
  end
21
21
 
22
22
  def body
23
- { timeSpentSeconds: focus_seconds, comment: context.comment.to_s }
23
+ { timeSpentSeconds: seconds_focused, comment: context.comment.to_s }
24
24
  end
25
25
 
26
26
  def auth
@@ -3,47 +3,43 @@ require "tty"
3
3
 
4
4
  module Focus
5
5
  class StartFocusTime < Action
6
- DEFAULT_CONTEXT_KEYS = %i(minutes target quiet daemonize).freeze
6
+ DEFAULT_CONTEXT_KEYS = %i(minutes target quiet daemonize focus_start).freeze
7
7
 
8
8
  attr_reader :action
9
9
 
10
10
  def call
11
11
  context.actions = ConfigLoader.load("actions")
12
+ context.daemonize ? fork { _actions } : _actions
13
+ end
14
+
15
+ private
16
+
17
+ def _actions
12
18
  focus
13
19
  take_break
20
+ rescue SystemExit, Interrupt
21
+ context.quiet = false
22
+ Focus::STDOUT.title "Shutting down gracefully..."
23
+ ensure
14
24
  cleanup
15
25
  happy_message
16
26
  end
17
27
 
18
- private
19
-
20
28
  def focus
21
29
  @action = :focus
22
-
23
- if context.daemonize
24
- fork { perform_actions "OnFocus" }
25
- else
26
- perform_actions "OnFocus"
27
- handle_progress_bar
28
- end
30
+ perform_actions "OnFocus"
31
+ context.focus_start = Time.now.to_i
32
+ handle_progress_bar
29
33
  end
30
34
 
31
35
  def take_break
32
36
  @action = :break
33
- if context.daemonize
34
- fork { perform_actions "OnBreak" }
35
- else
36
- perform_actions "OnBreak"
37
- handle_progress_bar
38
- end
37
+ perform_actions "OnBreak"
38
+ handle_progress_bar
39
39
  end
40
40
 
41
41
  def cleanup
42
- if context.daemonize
43
- fork { perform_actions "OnCompletion" }
44
- else
45
- perform_actions "OnCompletion"
46
- end
42
+ perform_actions "Cleanup"
47
43
  end
48
44
 
49
45
  def progress_bar
@@ -75,18 +71,22 @@ module Focus
75
71
 
76
72
  while Time.now < end_time
77
73
  timestamp = Time.now
78
- yield
74
+ yield if block_given?
79
75
  interval = 1 - (Time.now - timestamp)
80
76
  sleep(interval) if interval.positive?
81
77
  end
82
78
  end
83
79
 
84
80
  def handle_progress_bar
85
- every_second { progress_bar.advance unless context.daemonize }
81
+ if context.daemonize
82
+ every_second # sleep
83
+ else
84
+ every_second { progress_bar.advance }
85
+ end
86
86
  end
87
87
 
88
88
  def perform_actions(event)
89
- actions = context.actions.shift[event]
89
+ actions = actions_to_perform(event)
90
90
  return unless actions
91
91
  Focus::STDOUT.print_line "Starting #{event}...\r", quiet: context.quiet
92
92
 
@@ -100,6 +100,11 @@ module Focus
100
100
  end
101
101
  end
102
102
 
103
+ def actions_to_perform(event)
104
+ actions = context.actions.find { |x| x.keys.include? event }
105
+ actions[event] if actions
106
+ end
107
+
103
108
  def with_default_context(args)
104
109
  hsh = args.to_h
105
110
  hsh.merge default_hsh
@@ -125,8 +130,7 @@ module Focus
125
130
 
126
131
  def happy_message
127
132
  return if context.quiet
128
- Focus::STDOUT.puts_line nil
129
- Focus::STDOUT.puts_line "Complete!"
133
+ Focus::STDOUT.puts_line "\nProcess complete."
130
134
  end
131
135
 
132
136
  def constantize(str)
@@ -1,15 +1,22 @@
1
1
  require "dotenv"
2
2
 
3
3
  module Focus
4
+ class MissingConfiguration < ArgumentError; end
5
+
4
6
  class Config < OpenStruct
5
7
  class << self
6
8
  def method_missing(m, *args, &block) # rubocop:disable MethodMissing
7
- ENV[m.to_s.upcase] || config.send(m, *args, &block)
9
+ config.env(m) || config.send(m, *args, &block) || raise_undefined_config(m)
8
10
  end
9
11
 
10
12
  def config
11
13
  @config ||= new
12
14
  end
15
+
16
+ def raise_undefined_config(m)
17
+ error = "(#{m}) neither `ENV['#{m.to_s.upcase}']` or `.focus.yml#config.#{m}` are defined"
18
+ raise MissingConfiguration, error
19
+ end
13
20
  end
14
21
 
15
22
  def initialize
@@ -18,6 +25,10 @@ module Focus
18
25
  ingest _hardcoded
19
26
  end
20
27
 
28
+ def env(m)
29
+ ENV[m.to_s.upcase]
30
+ end
31
+
21
32
  private
22
33
 
23
34
  def configurations
@@ -61,5 +72,6 @@ module Focus
61
72
  return unless File.exist?(path)
62
73
  Dotenv.load path
63
74
  end
75
+
64
76
  end
65
77
  end
@@ -9,7 +9,7 @@ module Focus
9
9
  end
10
10
 
11
11
  def error(error)
12
- red("!!! Error:") + " #{error}"
12
+ " " + red("!!! Error:") + " #{error}"
13
13
  end
14
14
 
15
15
  def blue(string)
@@ -14,8 +14,17 @@ module Focus
14
14
  def step(string, opts = { quiet: false })
15
15
  quiet = opts[:quiet]
16
16
  print_line(Focus::Formatter.step(string), quiet: quiet)
17
- yield
17
+ yield if block_given?
18
18
  puts_line Focus::Formatter.ok, quiet: quiet
19
+ rescue FailedActionError, MissingConfiguration => error
20
+ puts_line Focus::Formatter.error error.to_s
21
+ end
22
+
23
+ def title(string)
24
+ puts
25
+ puts "-" * 50
26
+ puts string
27
+ puts "-" * 50
19
28
  end
20
29
 
21
30
  def debug_output(str)
@@ -1,3 +1,3 @@
1
1
  module Focus
2
- VERSION = "0.0.3".freeze
2
+ VERSION = "0.0.4".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ifocustt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Mason