Class: HTTY::CLI::Commands::BodyEdit

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

Overview

Encapsulates the body-edit command.

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, command_line_arguments, 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 body-edit command should appear.



11
12
13
# File 'lib/htty/cli/commands/body_edit.rb', line 11

def self.category
  'Building Requests'
end

.helpObject

Returns the help text for the body-edit command.



16
17
18
# File 'lib/htty/cli/commands/body_edit.rb', line 16

def self.help
  'Sets the body of the request using a text editor'
end

.help_extendedObject

Returns the extended help text for the body-edit command.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/htty/cli/commands/body_edit.rb', line 21

def self.help_extended
  <<-EOH.gsub(/^(?: |\t)+/, '')
  Sets the body content used for the request. Does not communicate with
  the host.

  Uses the editor specified in EDITOR environment variable. Your editor will
  open with a temporary file, fill the file with the request body. When you
  are done save the file and quit the editor. The exact content of the
  temporary file will be used as the request body. The temporary file will be
  removed after this command.

  If you want to choose your editor, start htty with `EDITOR=vim htty`
  replacing 'vim' with your editor of choice.
  EOH
end

.see_also_commandsObject

Returns related command classes for the body-edit command.



38
39
40
41
42
43
# File 'lib/htty/cli/commands/body_edit.rb', line 38

def self.see_also_commands
  [ HTTY::CLI::Commands::BodyRequest,
    HTTY::CLI::Commands::BodyUnset,
    HTTY::CLI::Commands::BodySet
  ]
end

Instance Method Details

#performObject

Performs the body-edit command.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/htty/cli/commands/body_edit.rb', line 46

def perform
  add_request_if_new do |request|
    request.body_set(with_editor do |editor|
      file = Tempfile.new('htty')
      if last_request_body = session.requests.last.body
        File.open(file.path, 'w+') {|f| f.write(last_request_body)}
      end
      result = system(editor + ' ' + file.path)
      if not result
        return empty_body_because("Unable to use '#{editor}' to edit request's body")
      end
      body = File.read(file.path)
      file.unlink
      body
    end)
  end
end