Class: TempestTime::Command

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Helpers::FormattingHelper, Helpers::GitHelper, Helpers::TimeHelper
Defined in:
lib/tempest_time/command.rb

Instance Method Summary collapse

Methods included from Helpers::GitHelper

#automatic_issue

Methods included from Helpers::FormattingHelper

#braced, #with_percent_sign

Methods included from Helpers::TimeHelper

#beginning_of_week, #dates_in_range, #end_of_week, #formatted_date, #formatted_date_range, #formatted_time, #formatted_time_for_input, #formatted_time_long, #future_dates, #parsed_time, #past_dates, #past_week_selections, #week_beginnings, #week_dates

Instance Method Details

#commandObject



33
34
35
36
# File 'lib/tempest_time/command.rb', line 33

def command
  require 'tty-command'
  TTY::Command.new
end

#current_userObject



118
119
120
# File 'lib/tempest_time/command.rb', line 118

def current_user
  @current_user ||= JiraAPI::Requests::GetCurrentUser.new.send_request.user
end

#date_prompt(message, days_before: 3, days_after: 3) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/tempest_time/command.rb', line 58

def date_prompt(message, days_before: 3, days_after: 3)
  require 'tty-prompt'
  dates = dates_in_range(days_before: days_before, days_after: days_after)
  TTY::Prompt.new.multi_select(
    message,
    dates,
    per_page: 5,
    default: dates.find_index { |k, v| v == Date.today } + 1
  )
end

#executeObject



20
21
22
23
24
# File 'lib/tempest_time/command.rb', line 20

def execute(*)
  execute!
rescue TTY::Reader::InputInterrupt
  prompt.say(pastel.yellow("\nGoodbye!"))
end

#execute!Object

Raises:

  • (NotImplementedError)


26
27
28
29
30
31
# File 'lib/tempest_time/command.rb', line 26

def execute!(*)
  raise(
    NotImplementedError,
    "#{self.class}##{__method__} must be implemented"
  )
end

#find_user(query) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/tempest_time/command.rb', line 96

def find_user(query)
  return current_user unless query

  users = JiraAPI::Requests::SearchUsers.new(query: query).send_request.users
  case users.count
  when 0
    abort(
      pastel.red('User not found!') + ' ' \
      'Please check your query and try again.'
    )
  when 1
    return users.first
  else
    require 'tty-prompt'
    TTY::Prompt.new.select(
      pastel.yellow('Multiple users match your query. Please select a user.'),
      users.map { |user| { "#{user.name}: #{user.email}" => user } },
      per_page: 10
    )
  end
end

#pastel(**options) ⇒ Object



38
39
40
41
# File 'lib/tempest_time/command.rb', line 38

def pastel(**options)
  require 'pastel'
  Pastel.new(options)
end

#prompt(**options) ⇒ Object



43
44
45
46
# File 'lib/tempest_time/command.rb', line 43

def prompt(**options)
  require 'tty-prompt'
  TTY::Prompt.new(options)
end

#spinnerObject



69
70
71
72
# File 'lib/tempest_time/command.rb', line 69

def spinner
  require 'tty-spinner'
  TTY::Spinner
end

#tableObject



74
75
76
77
# File 'lib/tempest_time/command.rb', line 74

def table
  require 'tty-table'
  TTY::Table
end

#week_prompt(message, past_weeks: 51) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/tempest_time/command.rb', line 48

def week_prompt(message, past_weeks: 51)
  require 'tty-prompt'
  weeks = past_week_selections(past_weeks)
  TTY::Prompt.new.select(
    message,
    weeks,
    per_page: 5
  )
end

#with_spinner(message, format: :pong) {|s| ... } ⇒ Object

Yields:

  • (s)


79
80
81
82
83
# File 'lib/tempest_time/command.rb', line 79

def with_spinner(message, format: :pong)
  s = spinner.new(":spinner #{message}", format: format)
  s.auto_spin
  yield(s)
end

#with_success_fail_spinner(message, format: :spin_3) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/tempest_time/command.rb', line 85

def with_success_fail_spinner(message, format: :spin_3)
  s = spinner.new(":spinner #{message}", format: format)
  s.auto_spin
  response = yield
  if response.success?
    s.success(pastel.green(response.message))
  else
    s.error(pastel.red(response.message))
  end
end