Class: SoPretty::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/so-pretty.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



18
19
20
21
22
# 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

Class Method Details

.from_cli(argv) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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

Instance Method Details

#close_streamsObject



92
93
94
95
96
97
98
# File 'lib/so-pretty.rb', line 92

def close_streams
  @instream.flush
  @instream.close

  @outstream.flush
  @outstream.close
end

#parsedObject

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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

#prettifyObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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

#stringObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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