module GPGStatusParser

Constants

STATUS_CODES

Public Class Methods

parse(file_or_string) { |parse_line(line)| ... } click to toggle source

Takes a file or string and optional block and either yields or returns a list of status messages

# File lib/gpg_status_parser.rb, line 13
def self.parse(file_or_string, &block)
  if block
    file_or_string.each_line { |line| yield parse_line(line)}
  else
    file_or_string.each_line.map{ |line| parse_line(line)}
  end
end
parse_line(line) click to toggle source

Parse a single line

# File lib/gpg_status_parser.rb, line 7
def self.parse_line(line)
  GPGStatusParser::StatusMessage.new(line.strip)
end
run_gpg(args, data=nil, &block) click to toggle source

Run a gpg command, and take care of setting up and tearing down the required temporary files. Just pass in the arguments you want to send to gpg:

GPGStatusParser.run_gpg("--verify foo.txt.asc foo.txt")

and not:

GPGStatusParser.run_gpg("gpg --verify foo.txt.asc foo.txt")

It is highly advised that you escape and/or scrub any user generated input to make sure it is safe. See Shellwords#shell_escape in the ruby standard library.

# File lib/gpg_status_parser.rb, line 34
def self.run_gpg args, data=nil, &block
  exit_status = nil
  status_file = Tempfile.new("status")

  full_gpg_command = "gpg --status-file #{status_file.path} #{args}"
  gpg_results = Open3.popen3(full_gpg_command) do |stdin, stdout, stderr, wait_thr|
    stdin.write data if data
    stdin.close
    exit_status = wait_thr.value
    parse(status_file, &block)
    out = stdout.read()
    err = stderr.read()
    {:status => exit_status, :stdout => out, :err => err}
  end
  gpg_results
ensure
  status_file.close
  status_file.unlink
end