console_utils 0.3.2 → 0.4.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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4c3fe1a107afccfae81595c74595bdb4dfd18352
4
- data.tar.gz: d3fb4f250a454750a58edb0d34eaa4b2bdac1d67
3
+ metadata.gz: 22a211280b416bdaf805b0230552de96331ce71e
4
+ data.tar.gz: 83669052246d74df2a926a254f08104e7fca8da6
5
5
  SHA512:
6
- metadata.gz: f053b200ef2551b711a80aa1259810e80c4394dc364c00a4e6310ac7a090ea3e5d91241898c7062783f052b51c584212b1f9e5e094cad932eb2ca82ecc591f00
7
- data.tar.gz: 00f07b42ef8d7007d56c094166ea0d1816d05e037927b8954acb0211d767bf1f9c5fe349afa77ef25977b3680a821afec99c30ce1687d38d98119ff40e40323e
6
+ metadata.gz: 893930c9027842760bc57f49a85078cee0ed60cc7aa6818379afa3d4026c2e11d61be17df10e308216bf07d24cc9b4f0d96803d1f61ca0300ee48689fc49b195
7
+ data.tar.gz: 0e20c16c9eee9d4608ec3b6a9f829d195d32abf85a9455a29910de9b152d5648288ed30ebeb6bb6ffbba634c3617151cf98d5bacbe17e5ceebc165ab839ec4b6
@@ -0,0 +1,30 @@
1
+ module ConsoleUtils
2
+ module JSONOutput
3
+ # The abstract singleton class for a prettified JSON formatting.
4
+ class BaseFormatter
5
+ include Singleton
6
+
7
+ # Prints formatted JSON to stdout.
8
+ def call(body) # :yields:
9
+ formatted = format_with_fallback(body)
10
+ if block_given?
11
+ yield(formatted)
12
+ else
13
+ puts formatted
14
+ end
15
+ end
16
+
17
+ # Formats a given JSON string
18
+ def format(body)
19
+ raise NotImplementedError
20
+ end
21
+
22
+ def format_with_fallback(body)
23
+ format(body)
24
+ rescue ParseError => error
25
+ warn error
26
+ return body.to_s
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,13 @@
1
+ module ConsoleUtils
2
+ module JSONOutput
3
+ # The default formatter uses standart JSON library to output prettified JSON
4
+ class DefaultFormatter < BaseFormatter
5
+ def format(body) #:nodoc:
6
+ JSON.pretty_generate JSON(body), :allow_nan => true, :max_nesting => false
7
+ rescue JSON::ParseError, JSON::GeneratorError
8
+ error = $!
9
+ raise ParseError, "#{error.class.to_s}: #{error.message}"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ require "open3"
2
+
3
+ module ConsoleUtils
4
+ module JSONOutput
5
+ # The jq formatter uses the external {jq}[http://stedolan.github.com/jq] utility.
6
+ class JqFormatter < BaseFormatter
7
+ delegate :jq_command, :to => :ConsoleUtils
8
+
9
+ def format(body) #:nodoc:
10
+ output, err, s = Open3.capture3(jq_command, stdin_data: body)
11
+
12
+ raise ParseError, err.squish unless s.success?
13
+
14
+ output
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,38 @@
1
+ module ConsoleUtils
2
+ module JSONOutput
3
+ extend ActiveSupport::Autoload
4
+
5
+ class ParseError < StandardError
6
+ def initialize(original_message = nil)
7
+ super("Failed to format a json. (#{original_message})")
8
+ end
9
+ end
10
+
11
+ eager_autoload do
12
+ autoload :BaseFormatter
13
+ end
14
+
15
+ autoload :DefaultFormatter
16
+ autoload :JqFormatter
17
+
18
+ FORMATTER_CLASSES = {}
19
+
20
+ # Get current formatter object
21
+ def self.formatter
22
+ const_get(formatter_class_name).instance
23
+ end
24
+
25
+ def self.json_formatter # :nodoc:
26
+ ConsoleUtils.json_formatter
27
+ end
28
+
29
+ def self.formatter_class_name # :nodoc:
30
+ FORMATTER_CLASSES[json_formatter] ||= "#{json_formatter}_formatter".classify.freeze
31
+ end
32
+
33
+ private_constant :FORMATTER_CLASSES
34
+ private_class_method :json_formatter, :formatter_class_name
35
+ end
36
+
37
+ JSONOutput.eager_load!
38
+ end
@@ -13,13 +13,22 @@ module ConsoleUtils::RequestUtils #:nodoc:
13
13
  end
14
14
 
15
15
  def inspect
16
- format INSPECT_FORMAT, request.path, response.status
16
+ format INSPECT_FORMAT, request.try(:path), response.try(:status)
17
17
  end
18
18
 
19
19
  private
20
20
 
21
- delegate :controller, to: :app, prefix: true, allow_nil: true
22
- delegate :request, :response, to: :app_controller, allow_nil: true
21
+ def request
22
+ app.controller.try(:request)
23
+ end
24
+
25
+ def response
26
+ app.controller.try(:response)
27
+ end
28
+
29
+ def response_body
30
+ app.controller.try(:response_body) || response.try(:body)
31
+ end
23
32
 
24
33
  def resp_wrap(meth, url, *args)
25
34
  @url, @_args = url, args
@@ -16,7 +16,7 @@ module ConsoleUtils::RequestUtils #:nodoc:
16
16
 
17
17
  def preview(mth = nil)
18
18
  if output = to_s.presence
19
- JSONOutput.formatter.(output)
19
+ ConsoleUtils::JSONOutput.formatter.(output)
20
20
  show_complete_in!
21
21
  show_transfered!
22
22
  yield(self) if block_given?
@@ -27,7 +27,7 @@ module ConsoleUtils::RequestUtils #:nodoc:
27
27
 
28
28
  # Copies to pasteboard
29
29
  def pbcopy(content = nil)
30
- content ||= JSONOutput::Default.instance.format(to_s)
30
+ content ||= ConsoleUtils::JSONOutput::Default.instance.format_with_fallback(to_s)
31
31
  IO.popen('pbcopy', 'w') { |io| io << content.to_s }
32
32
  puts PBCOPY_MESSAGE
33
33
  end
@@ -13,11 +13,6 @@ module ConsoleUtils #:nodoc:
13
13
  autoload :Exap
14
14
  autoload :Remo
15
15
 
16
- autoload :JSONOutput do
17
- autoload :Default
18
- autoload :Jq
19
- end
20
-
21
16
  autoload :DefaultAuthAutomator, "console_utils/request_utils/auth_automators"
22
17
  autoload :SimpleTokenAutomator, "console_utils/request_utils/auth_automators"
23
18
 
@@ -1,3 +1,3 @@
1
1
  module ConsoleUtils
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/console_utils.rb CHANGED
@@ -50,6 +50,7 @@ module ConsoleUtils
50
50
 
51
51
  MODULES = [
52
52
  :ActiveRecordUtils,
53
+ :JSONOutput,
53
54
  :RequestUtils,
54
55
  :BenchUtils,
55
56
  :OtherUtils
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-29 00:00:00.000000000 Z
11
+ date: 2015-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -140,6 +140,10 @@ files:
140
140
  - lib/console_utils/bench_utils/chips.rb
141
141
  - lib/console_utils/core_ext/array_to_proc.rb
142
142
  - lib/console_utils/core_ext/local_values.rb
143
+ - lib/console_utils/json_output.rb
144
+ - lib/console_utils/json_output/base_formatter.rb
145
+ - lib/console_utils/json_output/default_formatter.rb
146
+ - lib/console_utils/json_output/jq_formatter.rb
143
147
  - lib/console_utils/other_utils.rb
144
148
  - lib/console_utils/pry.rb
145
149
  - lib/console_utils/railtie.rb
@@ -147,7 +151,6 @@ files:
147
151
  - lib/console_utils/request_utils.rb
148
152
  - lib/console_utils/request_utils/auth_automators.rb
149
153
  - lib/console_utils/request_utils/exap.rb
150
- - lib/console_utils/request_utils/json_output.rb
151
154
  - lib/console_utils/request_utils/remo.rb
152
155
  - lib/console_utils/request_utils/request_params.rb
153
156
  - lib/console_utils/request_utils/requester.rb
@@ -1,62 +0,0 @@
1
- module ConsoleUtils::RequestUtils
2
- module JSONOutput
3
- FORMATTERS = {}
4
-
5
- # The abstract singleton class for a prettified JSON formatting.
6
- class Formatter
7
- include Singleton
8
-
9
- # Prints formatted JSON to stdout.
10
- def call(body)
11
- formatted = format(body)
12
- if block_given?
13
- yield(formatted)
14
- else
15
- puts formatted
16
- end
17
- end
18
-
19
- # Formats a given JSON string
20
- def format(body)
21
- raise NotImplementedError
22
- end
23
-
24
- def self.inherited(sub) #:nodoc:
25
- super
26
- key = sub.name.demodulize.underscore.to_sym
27
- FORMATTERS[key] = sub
28
- end
29
- end
30
-
31
- # The default formatter uses standart JSON library to output prettified JSON
32
- class Default < Formatter
33
- def format(body) #:nodoc:
34
- JSON.pretty_generate JSON(body), :allow_nan => true, :max_nesting => false
35
- rescue JSON::GeneratorError => e
36
- warn "Warning: Failed to format a json.", e.message, body
37
- body.to_s
38
- end
39
- end
40
-
41
- # The jq formatter uses the external {jq}[http://stedolan.github.com/jq] utility.
42
- class Jq < Formatter
43
- delegate :jq_command, :to => :ConsoleUtils
44
-
45
- def format(body) #:nodoc:
46
- IO.popen(jq_command, 'r+') { |io| (io << body).tap(&:close_write).read }
47
- end
48
- end
49
-
50
- class << self
51
- delegate :json_formatter, :to => :ConsoleUtils
52
-
53
- # Get current formatter object
54
- def formatter
55
- FORMATTERS[json_formatter].instance
56
- end
57
- end
58
-
59
- private_constant :Formatter
60
- private_class_method :json_formatter
61
- end
62
- end