ppjson 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YzI1OTEwNTg4ZDUwYWEwODhiOGEwNDEwYzQxODkzZTIzN2VmNmEzNA==
4
+ NTc1ZGQ1NmY0ODlhMGFjMTg1YWRjNTVjZjc0NzU1ODQ2MGE4N2M5ZQ==
5
5
  data.tar.gz: !binary |-
6
- OGQ2N2U4Y2I0ODY0NGFhMzRkZmJlMTgzMTYzNDNhNjZjNDEwNDZmYQ==
6
+ NzFjMzgxMzA1N2RlOTMzZjkwOGJiMzVkMjA2YTcwNGNhMDhhN2M3Ng==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- Y2IwZmZlNjkzMTZjMDBmODFkYjI3NTgwMTBkZjBkMGNmZDA0ZTdlZTA0OGE3
10
- NGM0NmVhOWEzMDE0MDAxNzQ2YTliNzUzZDM5YWNhZDgzMzMxZDRjNzJhODBj
11
- Mjg5NGY0ZmY3NGNmOTcwNjNkNDY2OTgzYjViNWI3NWMwYzdmYzc=
9
+ YzUzNzJhNDkwNDlkOGZhZjJiMzJiZjI4MjgwZjM4NmEwY2FmZmQyNTBhNjhm
10
+ MTk2ZWMyMDNlMDJhNmU3NThiOTY3NWY1YjgxOTAzODRhNzZjNmU5YTQ0ZjZi
11
+ ZjRhOTNmYmY5N2JhOTNlYmFhMzk1NzNiMmRkNTg5NTUzNzY0NDM=
12
12
  data.tar.gz: !binary |-
13
- OTAzZjFkZTkxMTBiMGYxM2IwNzI3ZjA2Y2JlYmFlYzJiYTM2M2JkNGQzYzEy
14
- MDA3MmMyMjFkMDQyMzMzNDZjYjVjMWU0Mzk3YzU2MjhlOTYyNGIyODFiZGE2
15
- ZjRmNWNlYWMxZTcyOTIzMTZiZTQwMjIyNDY5Y2JmYTY1NTRmMTA=
13
+ ZDA3ZDdmZjdhMTU1NjYzNGYzN2UzMjJiNTZlZGY3ZTdjOTdjM2JiYzMzMTFi
14
+ N2ViNzI3YjVmNGY1YjcwYWM4Mjc3Mjk4MTViNmZiOTk2OWM2MmVmOTk5NDMx
15
+ MGJhMDQzNDkwZGEzMDU0MmNmZGUxODYwMGI4ZmZjMjQ5NDlmMjU=
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Ppjson
2
2
 
3
- TODO: Write a gem description
3
+ Pretty print your JSON on the command-line the easy way.
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,7 +18,51 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ Pretty printing your JSON on the command line has never been easier:
22
+
23
+ ```
24
+ ppjson '{"a":"b"}'
25
+ ```
26
+
27
+ If another command returns some JSON for you:
28
+
29
+ ```
30
+ curl http://mydomain.com/blah.json | ppjson
31
+ ```
32
+
33
+ Do you have some JSON in a file? See a pretty printed version of it:
34
+
35
+ ```
36
+ ppjson -f my_file.json
37
+ ```
38
+
39
+ Or maybe you want to pretty print the contents of the file and then update the file with the pretty printed version:
40
+
41
+ ```
42
+ ppjson -f -i my_file.json
43
+ ```
44
+
45
+ Perhaps you already have some pretty printed JSON in a file, but you want to pass it as an argument to some other command, so you need to un-pretty print it:
46
+
47
+ ```
48
+ ppjson -f -u my_file.json
49
+ ```
50
+
51
+ You can even store your un-pretty printed version back into the file for later:
52
+
53
+ ```
54
+ ppjson -f -u -i my_file.json
55
+ ```
56
+
57
+ Get some help with:
58
+
59
+ ```
60
+ ppjson -h
61
+ ```
62
+
63
+ It's easier to remember than `python -mjson.tool` and it won't annoyingly reorder your keys for you.
64
+
65
+ If you're using RVM just dump it into your global gemset to have it available everywhere.
22
66
 
23
67
  ## Contributing
24
68
 
data/TODO.txt CHANGED
@@ -1,9 +1,21 @@
1
- - write a decent readme
2
- - stop using an unreleased escort that points at filesystem
3
- - try it out when installing at the top level
1
+ - stop using an unreleased escort that points at filesystem DONE
2
+ - try it out when installing at the top level DONE
3
+ - add a summary and description DONE
4
+ - ppjson --file - to read json from a file, format it and spit it out to STDOUT DONE
5
+ - ppjson --file --inline - to read json from a file, format it and overwrite the file with it DONE
6
+ - ppjson --file --backwards - take json from file which is presumably pretty already and unprettify it DONE
7
+
8
+
9
+ - write a decent readme DONE
10
+ - do a bunch of specs
11
+ - do an integration test if can figure out a nice way of doing that
4
12
  - try it out with binstubs in another project
5
- - think about if it needs any arguments at all
13
+ - up the version
14
+ - release
15
+ - blog about the current design to handle the different options interacting in different ways nicely
6
16
 
7
- - ppjson --file - to read json from a file, format it and spit it out to STDOUT
8
- - ppjson --file --inline - to read json from a file, format it and overwrite the file with it
9
- - ppjson --file --backwards - take json from file which is presumably pretty already and unprettify it
17
+ - up the version of escort to one with depends support
18
+ - do the dependencies between options
19
+ - up the version
20
+ - update readme
21
+ - release
data/bin/ppjson CHANGED
@@ -6,8 +6,17 @@ require 'ppjson'
6
6
  Escort::App.create do |app|
7
7
  app.version Ppjson::VERSION
8
8
 
9
+ app.summary "Pretty print JSON on the command-line"
10
+ app.description "Pretty print JSON strings by piping or passing them as arguments. It will preserve the ordering of your keys."
11
+
9
12
  app.requires_arguments
10
13
 
14
+ app.options do |opts|
15
+ opts.opt :file, "Read the JSON from a file", :short => '-f', :long => '--file', :type => :boolean, :default => false
16
+ opts.opt :inline, "Overwrite the file with the pretty JSON (only make sense with the --file option)", :short => '-i', :long => '--inline', :type => :boolean, :default => false, :depends_on => [:file]
17
+ opts.opt :undo, "Take pretty printed JSON from file and unprettify it (only make sense with the --file option)", :short => '-u', :long => '--undo', :type => :boolean, :default => false, :depends_on => [:file]
18
+ end
19
+
11
20
  app.action do |options, arguments|
12
21
  Ppson::Commands::PrettyPrintJson.new(options, arguments).execute
13
22
  end
data/data/test1.json ADDED
@@ -0,0 +1 @@
1
+ {"a":"b","c":"d"}
data/data/test2.json ADDED
@@ -0,0 +1 @@
1
+ {"1":"2","4":"5"}
@@ -0,0 +1,22 @@
1
+ module Ppson
2
+ class Arguments
3
+ include Enumerable
4
+
5
+ attr_reader :arguments, :options
6
+
7
+ def initialize(arguments, options)
8
+ @arguments = arguments
9
+ @options = options
10
+ end
11
+
12
+ def each(&block)
13
+ arguments.each do |argument|
14
+ block.call(argument_instance(argument, options))
15
+ end
16
+ end
17
+
18
+ def argument_instance(argument_data, options)
19
+ raise "Must be implemented in child class"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ module Ppson
2
+ class ArgumentsDecorator
3
+ attr_reader :arguments, :options
4
+
5
+ def initialize(arguments, options)
6
+ @arguments = arguments
7
+ @options = options
8
+ end
9
+
10
+ def decorate
11
+ options[:file] ? file_arguments : json_arguments
12
+ end
13
+
14
+ private
15
+
16
+ def file_arguments
17
+ FileArguments.new(arguments, options)
18
+ end
19
+
20
+ def json_arguments
21
+ JsonArguments.new(arguments, options)
22
+ end
23
+ end
24
+ end
@@ -2,22 +2,10 @@ module Ppson
2
2
  module Commands
3
3
  class PrettyPrintJson < ::Escort::ActionCommand::Base
4
4
  def execute
5
- arguments.each do |argument|
6
- hash = from_json(argument.to_s)
7
- $stdout.puts MultiJson.dump(hash, :pretty => true)
5
+ ArgumentsDecorator.new(arguments, command_options).decorate.each do |argument|
6
+ argument.process
8
7
  end
9
8
  end
10
-
11
- private
12
-
13
- def from_json(json)
14
- MultiJson.load(json)
15
- rescue MultiJson::LoadError => e
16
- $stderr.puts "Invalid JSON string (don't forget to quote your strings e.g. '{\"abc\":\"def\"}'):"
17
- $stderr.puts e.message
18
- $stderr.puts "#{json}"
19
- exit(1)
20
- end
21
9
  end
22
10
  end
23
11
  end
@@ -0,0 +1,35 @@
1
+ module Ppson
2
+ class FileArgument
3
+ attr_reader :filepath, :options
4
+
5
+ def initialize(filepath, options)
6
+ @filepath = filepath
7
+ @options = options
8
+ end
9
+
10
+ def process
11
+ with_contents do |contents|
12
+ writer.write(contents, :pretty => prettify?)
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def with_contents(&block)
19
+ contents = reader.read
20
+ block.call(contents) if contents
21
+ end
22
+
23
+ def reader
24
+ @reader ||= FileJsonReader.new(filepath)
25
+ end
26
+
27
+ def writer
28
+ @writer ||= (options[:inline] ? FileJsonWriter.new(filepath) : StdoutJsonWriter.new)
29
+ end
30
+
31
+ def prettify?
32
+ !options[:undo]
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,7 @@
1
+ module Ppson
2
+ class FileArguments < Arguments
3
+ def argument_instance(argument_data, options)
4
+ FileArgument.new(argument_data, options)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ module Ppson
2
+ class FileJsonReader
3
+ attr_reader :filepath
4
+
5
+ def initialize(filepath)
6
+ @filepath = filepath
7
+ end
8
+
9
+ def read
10
+ contents = nil
11
+ begin
12
+ File.open(filepath, 'r') do |file|
13
+ contents = file.readlines.join("")
14
+ end
15
+ rescue => e
16
+ Escort::Logger.error.warn("Error reading file #{filepath}")
17
+ end
18
+ contents
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ module Ppson
2
+ class FileJsonWriter
3
+ attr_reader :filepath
4
+
5
+ def initialize(filepath)
6
+ @filepath = filepath
7
+ end
8
+
9
+ def write(contents, options = {:pretty => false})
10
+ begin
11
+ File.open(filepath, 'w') do |file|
12
+ Ppjson::StreamJsonWriter.new(file).write(contents, :pretty => options[:pretty])
13
+ end
14
+ rescue => e
15
+ Escort::Logger.error.warn("Error writing file #{filepath}")
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ module Ppson
2
+ class JsonArgument
3
+ attr_reader :data, :options
4
+
5
+ def initialize(data, options)
6
+ @data = data
7
+ @options = options
8
+ end
9
+
10
+ def process
11
+ Ppjson::StreamJsonWriter.new.write(data, :pretty => true)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ module Ppson
2
+ class JsonArguments < Arguments
3
+ def argument_instance(argument_data, options)
4
+ JsonArgument.new(argument_data, options)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Ppson
2
+ class StdoutJsonWriter
3
+ def write(contents, options = {:pretty => false})
4
+ Ppjson::StreamJsonWriter.new.write(contents, :pretty => options[:pretty])
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,25 @@
1
+ module Ppjson
2
+ class StreamJsonWriter
3
+ attr_reader :stream
4
+
5
+ def initialize(stream = $stdout)
6
+ @stream = stream
7
+ end
8
+
9
+ def write(json, options = {:pretty => true})
10
+ hash = from_json(json)
11
+ stream.puts MultiJson.dump(hash, options)
12
+ end
13
+
14
+ private
15
+
16
+ def from_json(json)
17
+ MultiJson.load(json)
18
+ rescue MultiJson::LoadError => e
19
+ Escort::Logger.error.error "Invalid JSON string (don't forget to quote your strings e.g. '{\"abc\":\"def\"}'):"
20
+ Escort::Logger.error.error e.message
21
+ Escort::Logger.error.warn "#{json}"
22
+ exit(1)
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module Ppjson
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/ppjson.rb CHANGED
@@ -4,4 +4,19 @@ require 'multi_json'
4
4
 
5
5
  require "ppjson/version"
6
6
 
7
+ require "ppjson/stream_json_writer"
8
+
9
+ require "ppjson/file_json_reader"
10
+ require "ppjson/stdout_json_writer"
11
+ require "ppjson/file_json_writer"
12
+
13
+ require "ppjson/file_argument"
14
+ require "ppjson/json_argument"
15
+
16
+ require "ppjson/arguments"
17
+ require "ppjson/file_arguments"
18
+ require "ppjson/json_arguments"
19
+
20
+ require "ppjson/arguments_decorator"
21
+
7
22
  require "ppjson/commands/pretty_print_json"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ppjson
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alan Skorkin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-02-28 00:00:00.000000000 Z
11
+ date: 2013-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: escort
@@ -97,8 +97,20 @@ files:
97
97
  - Rakefile
98
98
  - TODO.txt
99
99
  - bin/ppjson
100
+ - data/test1.json
101
+ - data/test2.json
100
102
  - lib/ppjson.rb
103
+ - lib/ppjson/arguments.rb
104
+ - lib/ppjson/arguments_decorator.rb
101
105
  - lib/ppjson/commands/pretty_print_json.rb
106
+ - lib/ppjson/file_argument.rb
107
+ - lib/ppjson/file_arguments.rb
108
+ - lib/ppjson/file_json_reader.rb
109
+ - lib/ppjson/file_json_writer.rb
110
+ - lib/ppjson/json_argument.rb
111
+ - lib/ppjson/json_arguments.rb
112
+ - lib/ppjson/stdout_json_writer.rb
113
+ - lib/ppjson/stream_json_writer.rb
102
114
  - lib/ppjson/version.rb
103
115
  - ppjson.gemspec
104
116
  homepage: https://github.com/skorks/ppjson