Class: YAPI::Commands::Fetch

Inherits:
Object
  • Object
show all
Defined in:
lib/yapi/commands/fetch.rb

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ Fetch

Returns a new instance of Fetch.



10
11
12
13
14
15
16
17
18
19
# File 'lib/yapi/commands/fetch.rb', line 10

def initialize(arg)
  match = arg.match(/(.*?):?(\d+)?$/)
  if match
    _, @filename, @line_number = match.to_a
  else
    @filename = arg
  end
rescue StandardError => e
  puts e
end

Instance Method Details

#configObject



52
53
54
# File 'lib/yapi/commands/fetch.rb', line 52

def config
  @config ||= YAPI::Config.new(@filename)
end

#find_route_nameObject



33
34
35
36
37
38
39
40
# File 'lib/yapi/commands/fetch.rb', line 33

def find_route_name
  row = @line_number.to_i
  while row > 0 do
    match = lines[row - 1].match(/(^\S.*):$/)
    return match[1] if match
    row -= 1
  end
end

#linesObject



48
49
50
# File 'lib/yapi/commands/fetch.rb', line 48

def lines
  @lines ||= File.readlines(@filename)
end

#performObject



21
22
23
# File 'lib/yapi/commands/fetch.rb', line 21

def perform
  YAPI::Fetcher.new(config, route_name).perform
end

#promptObject



56
57
58
# File 'lib/yapi/commands/fetch.rb', line 56

def prompt
  @prompt ||= TTY::Prompt.new(active_color: :bold, interrupt: :exit)
end

#prompt_route_nameObject



42
43
44
45
46
# File 'lib/yapi/commands/fetch.rb', line 42

def prompt_route_name
  prompt.select("Choose request to run (Ctrl-C to exit):",
                config.requests,
                filter: true)
end

#route_nameObject



25
26
27
28
29
30
31
# File 'lib/yapi/commands/fetch.rb', line 25

def route_name
  if @line_number
    find_route_name
  else
    prompt_route_name
  end
end