Class: Metaphor::Input::StdinInput

Inherits:
Object
  • Object
show all
Defined in:
lib/metaphor/input/stdin_input.rb

Instance Method Summary collapse

Constructor Details

#initialize(source = STDIN, prompt = STDOUT) ⇒ StdinInput

Returns a new instance of StdinInput.



4
5
6
7
# File 'lib/metaphor/input/stdin_input.rb', line 4

def initialize(source = STDIN, prompt = STDOUT)
  @source = source
  @prompt = prompt
end

Instance Method Details

#getObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/metaphor/input/stdin_input.rb', line 9

def get
  @prompt.puts "Enter headers in the format header:value."
  @prompt.puts "Each header:value pair should be followed by a newline."
  @prompt.puts "When you're done enter a blank line."
  headers = {}
  loop do
    line = @source.readline.to_s.strip 
    break if line == ""
    header, value = line.split(/:/, 2).map{|s|s.strip}
    headers[header] = value
  end

  @prompt.puts "Enter the message body."
  @prompt.puts "When you're done enter a blank line."
  body = []
  loop do
    line = @source.readline.to_s.strip
    break if line == ""
    body << line
  end

  [ headers, body.join("\n") ]
end