Class: HTTY::CLI::Commands::BodySet

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

Overview

Encapsulates the body-set 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-set command should appear.



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

def self.category
  'Building Requests'
end

.helpObject

Returns the help text for the body-set command.



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

def self.help
  'Sets the body of the request'
end

.help_extendedObject

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



21
22
23
24
25
26
# File 'lib/htty/cli/commands/body_set.rb', line 21

def self.help_extended
  'Sets the body content used for the request. Does not communicate with ' +
  "the host.\n"                                                            +
  "\n"                                                                     +
  'Enter two blank lines, or hit Ctrl-D, to signify the end of the body.'
end

.see_also_commandsObject

Returns related command classes for the body-set command.



29
30
31
# File 'lib/htty/cli/commands/body_set.rb', line 29

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

Instance Method Details

#performObject

Performs the body-set command.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/htty/cli/commands/body_set.rb', line 34

def perform
  add_request_if_new do |request|
    puts notice('Enter two blank lines, or hit Ctrl-D, to signify the end ' +
                'of the body')
    lines            = []
    empty_line_count = 0
    while empty_line_count < 2 do
      if (input = Readline.readline).nil?
        break
      end
      lines << input.chomp
      empty_line_count = lines.last.empty? ? (empty_line_count + 1) : 0
    end
    request.body_set lines.join("\n").gsub(/[\r\n]+$/, '')
    body = lines.join("\n")
    while body.chomp != body
      body.chomp!
    end
    request.body_set body
  end
end