Class: Huck::Generators::StdinGenerator

Inherits:
Huck::Generator show all
Defined in:
lib/huck/generators/stdin.rb

Overview

A generator to use from the command line by piping in text

Instance Attribute Summary

Attributes inherited from Huck::Generator

#config

Instance Method Summary collapse

Methods inherited from Huck::Generator

factory

Instance Method Details

#generateObject

Submit all data from stdin.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/huck/generators/stdin.rb', line 9

def generate
  data = ''

  # Try reading 1 byte first in non-blocking mode. If the call would block
  # during read(), then we just bail and raise. Otherwise, if at least 1
  # byte can be read, we proceed to read until EOF since we already have
  # our stream of data.
  begin
    data += STDIN.read_nonblock 1
  rescue Errno::EWOULDBLOCK
    raise Huck::Error, 'no data from stdin'
  end
  data += STDIN.read
  data
end