Class: RedmineCLI::InputParser

Inherits:
Object
  • Object
show all
Extended by:
Helpers::Input
Defined in:
lib/redmine_cli/input_parser.rb

Overview

class with some methods for user input processing

Class Method Summary collapse

Methods included from Helpers::Input

ask, ask_for_object, ask_for_user, ask_from_text_editor, ask_url

Methods included from Helpers::Output

#erb, #message, #print_object_list, #print_prompt_message

Class Method Details

.parse_project(value) ⇒ Object

Processes string and tries to find project



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/redmine_cli/input_parser.rb', line 11

def self.parse_project(value)
  by_id = RedmineRest::Models::Project.find_by_id(value) if value.numeric?
  return by_id if by_id

  found_projects = RedmineRest::Models::Project.all.filter_by_name_substring(value)
  case found_projects.size
  when 0 then fail(ProjectNotFound)
  when 1 then found_projects.first
  else ask_for_object(found_projects)
  end

rescue ActiveResource::ResourceNotFound, ActiveResource::ForbiddenAccess
  raise ProjectNotFound
end

.parse_time(input) ⇒ Object

Parses time from user’s input. Formats: HH:MM; M; H.h

Parameters:



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/redmine_cli/input_parser.rb', line 58

def self.parse_time(input)
  fail(BadInputTime) unless input =~ /^\d+[\:\.]?\d*/

  if input.include?(':')
    h, m = input.split(':').map(&:to_i)
    (60 * h + m) / 60.0
  elsif input.include?('.')
    input.to_f
  else
    input.to_i
  end
end

.parse_user(value, project: nil) ⇒ RedmineRest::Models::User

Processes string and tries to find user

Returns:

  • (RedmineRest::Models::User)

Raises:



32
33
34
35
36
37
38
39
40
# File 'lib/redmine_cli/input_parser.rb', line 32

def self.parse_user(value, project: nil)
  return RedmineRest::Models::User.find(value) if value.numeric? || value == 'current'
  fail UserNotFound unless project

  user_from_project(project, value)

rescue ActiveResource::ResourceNotFound
  raise UserNotFound
end

.user_from_project(project, name_substring = '') ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/redmine_cli/input_parser.rb', line 42

def self.user_from_project(project, name_substring = '')
  found_members = project.members.filter_by_name_substring(name_substring)

  case found_members.size
  when 0 then fail(UserNotFound)
  when 1 then found_members.first
  else ask_for_object(found_members)
  end
end