Class: Slk::Services::UserPicker

Inherits:
Object
  • Object
show all
Defined in:
lib/slk/services/user_picker.rb

Overview

Disambiguates between multiple matching users. Prompts at a TTY; raises ApiError in non-interactive contexts so callers don’t silently get the wrong user when name resolution is ambiguous.

Instance Method Summary collapse

Constructor Details

#initialize(stdin: $stdin, prompt_io: $stderr) ⇒ UserPicker

Returns a new instance of UserPicker.



9
10
11
12
# File 'lib/slk/services/user_picker.rb', line 9

def initialize(stdin: $stdin, prompt_io: $stderr)
  @stdin = stdin
  @prompt_io = prompt_io
end

Instance Method Details

#pick(matches) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/slk/services/user_picker.rb', line 14

def pick(matches)
  return matches.first['id'] if matches.size == 1

  unless interactive?
    raise ApiError,
          "Ambiguous match (#{matches.size} users): #{ids(matches).join(', ')}. " \
          'Use --pick N or --all to disambiguate non-interactively.'
  end

  list(matches)
  matches[read_index(matches.size)]['id']
end