class SoPretty::Base
Public Class Methods
from_cli( argv )
click to toggle source
# File lib/so-pretty.rb, line 100 def self.from_cli( argv ) options = { :instream => "-", :outstream => "-", :format => :yaml, } parser = OptionParser.new do |opts| opts.banner = "Usage: sp [options]" opts.separator "" opts.separator "Options:" opts.on( "-i", "--input [instream]", "Input stream, one of: '-', 'STDIN', or a file name. default: '-'" ) do |instream| options[:instream] = instream end opts.on( "-o", "--output [outstream]", "Output stream, one of: '-', 'STDOUT', 'STDERR' or a file name. default: '-'" ) do |outstream| options[:outstream] = outstream end opts.on( "-f", "--format [format]", "Output format, one of: 'yaml', 'json', 'ruby', 'minjson'") do |format| options[:format] = format.to_sym end opts.on("-V", "--version", "Show version") do puts VERSION exit end opts.separator "" opts.separator "Examples:" opts.separator EXAMPLES end parser.parse( argv ) return self.new( options ) end
new( options = {} )
click to toggle source
# File lib/so-pretty.rb, line 18 def initialize( options = {} ) @instream = to_stream( :input, options[:instream] ) @outstream = to_stream( :output, options[:outstream] ) @outformat = options[:format] end
Public Instance Methods
close_streams()
click to toggle source
# File lib/so-pretty.rb, line 92 def close_streams @instream.flush @instream.close @outstream.flush @outstream.close end
parsed()
click to toggle source
# File lib/so-pretty.rb, line 50 def parsed return @parsed if @parsed begin data = JSON.load( string ) return @parsed = data rescue JSON::ParserError => e end begin data = YAML::load( string ) return @parsed = data rescue Psych::SyntaxError => e end begin data = Kernel.eval( string ) return @parsed = data rescue StandardError => e end raise ArgumentError, "Input was unparsable as JSON or YAML." end
prettify()
click to toggle source
# File lib/so-pretty.rb, line 24 def prettify if @outformat == :yaml @outstream.puts YAML::dump( parsed ) return end if @outformat == :ruby if defined?( AwesomePrint ) @outstream.puts parsed.ai( plain: true, index: false ) else @outstream.puts parsed.inspect end return end if @outformat == :minjson @outstream.puts JSON.dump( parsed ) return end @outstream.puts JSON.pretty_generate( parsed ) ensure close_streams end
string()
click to toggle source
# File lib/so-pretty.rb, line 74 def string return @string if @string str = @instream.read zstream = Zlib::Inflate.new begin buf = zstream.inflate(str) zstream.finish zstream.close return @string = buf rescue Exception => e end return @string = str end
Private Instance Methods
to_stream( input_output, maybe_stream )
click to toggle source
# File lib/so-pretty.rb, line 140 def to_stream( input_output, maybe_stream ) case maybe_stream.downcase when "stdin" raise ArgumentError, "Output stream cannot be STDIN" return STDIN when "stdout" raise ArgumentError, "Input stream cannot be STDOUT" return STDOUT when "stderr" raise ArgumentError, "Input stream cannot be STDERR" return STDERR end if maybe_stream == "-" if input_output == :input return STDIN end return STDOUT end mode = input_output == :input ? "r" : "w" return File.open( maybe_stream, mode ) end