Class: HTTY::CLI::Commands::Reuse

Inherits:
HTTY::CLI::Command show all
Includes:
Display
Defined in:
lib/htty/cli/commands/reuse.rb

Overview

Encapsulates the reuse command.

Constant Summary

Constant Summary

Constants included from Display

Display::FORMATS

Instance Attribute Summary

Attributes inherited from HTTY::CLI::Command

#arguments, #session

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Display

#break, #format, #formatted_prompt_for, #indent, #logotype, #normal, #notice, #pluralize, #rescuing_from, #say, #say_goodbye, #say_header, #say_hello, #show_headers, #show_request, #show_response, #strong, #word_wrap, #word_wrap_indented

Methods inherited from HTTY::CLI::Command

#add_request_if_new, alias_for, aliases, build_for, command_line, complete_for?, #initialize, notify_if_cookies_cleared, raw_name, sanitize_arguments

Constructor Details

This class inherits a constructor from HTTY::CLI::Command

Class Method Details

.categoryObject

Returns the name of a category under which help for the reuse command should appear.



10
11
12
# File 'lib/htty/cli/commands/reuse.rb', line 10

def self.category
  'Navigation'
end

.command_line_argumentsObject

Returns the arguments for the command-line usage of the reuse command.



15
16
17
# File 'lib/htty/cli/commands/reuse.rb', line 15

def self.command_line_arguments
  'INDEX'
end

.helpObject

Returns the help text for the reuse command.



20
21
22
# File 'lib/htty/cli/commands/reuse.rb', line 20

def self.help
  'Copies a previous request by the index number shown in history'
end

.help_extendedObject

Returns the extended help text for the reuse command.



25
26
27
28
29
30
31
32
# File 'lib/htty/cli/commands/reuse.rb', line 25

def self.help_extended
  'Copies the properties of a previous request to be used for the request, ' +
  'using the request index number shown in history. Does not communicate '   +
  "with the host.\n"                                                         +
  "\n"                                                                       +
  'The argument is an index number that appears when you type '              +
  "#{strong HTTY::CLI::Commands::History.command_line}."
end

.see_also_commandsObject

Returns related command classes for the reuse command.



35
36
37
# File 'lib/htty/cli/commands/reuse.rb', line 35

def self.see_also_commands
  [HTTY::CLI::Commands::History, HTTY::CLI::Commands::HistoryVerbose]
end

Instance Method Details

#performObject

Performs the reuse command.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/htty/cli/commands/reuse.rb', line 40

def perform
  unless arguments.length == 1
    raise ArgumentError,
          "wrong number of arguments (#{arguments.length} for 1)"
  end

  requests = session.requests
  requests_with_responses = requests.select do |r|
    r.response
  end
  raise 'no requests in history' if requests_with_responses.empty?

  index = arguments.first.to_i
  unless (1..requests_with_responses.length).include?(index)
    if requests_with_responses.length == 1
      raise ArgumentError,
            "index must be 1"
    else
      raise ArgumentError,
            "index must be between 1 and #{requests_with_responses.length}"
    end
  end

  add_request_if_new do |request|
    session.requests.pop if request.response.nil?

    requests[index - 1].send(:dup_without_response)
  end

  puts notice("Using a copy of request ##{index}")
  self
end