Class: PrettyJSON

Inherits:
Object
  • Object
show all
Defined in:
lib/pretty_json.rb,
lib/pretty_json/version.rb

Overview

Represents a pretty JSON string.

Constant Summary collapse

VERSION =
'1.0.1'

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ PrettyJSON

Returns a new instance of PrettyJSON.

Parameters:

  • input (String)

    either a file path or a JSON string.



9
10
11
12
# File 'lib/pretty_json.rb', line 9

def initialize(input)
  json_hash = send("#{input.class.name.downcase}_to_hash", input)
  @json = JSON.pretty_unparse(json_hash)
end

Instance Method Details

#to_file(out_file) ⇒ String

Writes pretty formatted JSON to the specified file and returns the same content as a string.

Parameters:

  • out_file (String)

    to be written.

Returns:

  • (String)

    pretty formatted JSON.



25
26
27
28
# File 'lib/pretty_json.rb', line 25

def to_file(out_file)
  File.write(out_file, @json)
  @json
end

#to_sString

Returns pretty formatted JSON.

Returns:

  • (String)

    pretty formatted JSON.



16
17
18
# File 'lib/pretty_json.rb', line 16

def to_s
  @json
end