jqr 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ef733d9483c93f4c2e4ffa718de0827659a3e7d3
4
- data.tar.gz: 1ab9f37a666e24e020911a5f2fa8d98d426cffa4
3
+ metadata.gz: b7d1912da9a297eaf332ae30edd2b869c1ff0410
4
+ data.tar.gz: 79572aa7489541bda6cc46a535ffcb1b79ffce7a
5
5
  SHA512:
6
- metadata.gz: abfca28681c214afaa4e0c3fdc108eb3d70e40383f06dbd28152aad844701e5bea22295991649559039a8264fd878c14a38096227b8b722419e8fe7a0832f61d
7
- data.tar.gz: 7d15cba92348dc26019baf874d1b5c1e3e6224364edeec37df3e6596ae84ae96a1e730b3e0e606a0463faa268aa56bc65e414582ad5d738b9864782c40a6e4c5
6
+ metadata.gz: 231e9edb0e886f7ea644bddec002bc90a0248009a7fa4b7a583367a1ba0182e906e6c2a0267a0ac157c792b237065ce587466d9c20ee3792515ccf248453e75e
7
+ data.tar.gz: c8761c78221a02b1830cee8c18b01a56ed218369eed7785e9b7332afc508af88080d163e8985cc666198cf07f22f7feae0c2a4c41aa94c432c4176fc96ab0646
data/bin/jqr CHANGED
@@ -1,30 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "active_support/core_ext/hash/indifferent_access"
4
- require "color_echo/get"
5
- require "json"
6
- require "pp"
3
+ $LOAD_PATH.unshift("../../lib", __FILE__)
4
+ require "jqr"
7
5
 
8
- raw_json = JSON.parse(STDIN.read).with_indifferent_access
9
- op = ARGV[0] || ""
10
-
11
- result = eval("raw_json#{op}")
12
-
13
- processed =
14
- case result
15
- when Hash
16
- pretty = JSON.pretty_generate(result)
17
- if STDOUT.tty?
18
- pretty
19
- .gsub(/(?<key>".+"):/) { "#{CE.fg(:h_blue).get(Regexp.last_match[:key])}:" }
20
- .gsub(/: (?<val>".+")/) { ": #{CE.fg(:green).get(Regexp.last_match[:val])}" }
21
- else
22
- pretty
23
- end
24
- when String, Numeric, TrueClass, FalseClass
25
- result
26
- else
27
- PP.pp(result, "")
28
- end
29
-
30
- puts processed
6
+ Jqr::Runner.new(ARGV).run
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.add_dependency "activesupport"
21
21
  spec.add_dependency "color_echo"
22
+ spec.add_dependency "slop"
22
23
 
23
24
  spec.add_development_dependency "bundler", "~> 1.9"
24
25
  spec.add_development_dependency "rake", "~> 10.0"
data/lib/jqr.rb CHANGED
@@ -1,5 +1,14 @@
1
- require "jqr/version"
1
+ require "active_support/core_ext/hash/indifferent_access"
2
+ require "color_echo/get"
3
+ require "json"
4
+ require "pp"
5
+ require "slop"
2
6
 
3
7
  module Jqr
4
8
  # Your code goes here...
5
9
  end
10
+
11
+ require "jqr/commands"
12
+ require "jqr/processor"
13
+ require "jqr/runner"
14
+ require "jqr/version"
@@ -0,0 +1,8 @@
1
+ module Commands
2
+
3
+ end
4
+
5
+ require "jqr/commands/base"
6
+ require "jqr/commands/help"
7
+ require "jqr/commands/runner"
8
+ require "jqr/commands/version"
@@ -0,0 +1,11 @@
1
+ module Jqr
2
+ module Commands
3
+ class Base
4
+ attr_reader :options
5
+
6
+ def initialize(options)
7
+ @options = options
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ module Jqr
2
+ module Commands
3
+ class Help < Base
4
+ def run
5
+ puts options
6
+ exit
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ module Jqr
2
+ module Commands
3
+ class Runner < Base
4
+ def run
5
+ Processor.new(options).run
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ module Jqr
2
+ module Commands
3
+ class Version < Base
4
+ def run
5
+ puts Jqr::VERSION
6
+ exit
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,61 @@
1
+ module Jqr
2
+ class Processor
3
+ attr_reader :options
4
+
5
+ def initialize(options = {})
6
+ @options = options
7
+ end
8
+
9
+ def run
10
+ puts output
11
+ end
12
+
13
+ private
14
+
15
+ def colorful?
16
+ !options[:"no-color"]
17
+ end
18
+
19
+ def colorize(txt)
20
+ txt
21
+ .gsub(/(?<key>".+"):/) { "#{CE.fg(:h_blue).get(Regexp.last_match[:key])}:" }
22
+ .gsub(/: (?<val>".+")/) { ": #{CE.fg(:green).get(Regexp.last_match[:val])}" }
23
+ end
24
+
25
+ def input
26
+ STDIN.read
27
+ end
28
+
29
+ def input_json
30
+ JSON.parse(input).with_indifferent_access
31
+ end
32
+
33
+ def output
34
+ case processed_data
35
+ when Hash
36
+ tty? && colorful? ? colorize(pretty_json) : pretty_json
37
+ when String, Numeric, TrueClass, FalseClass
38
+ processed_data
39
+ else
40
+ PP.pp(processed_data, "")
41
+ end
42
+ end
43
+
44
+ def pretty_json
45
+ JSON.pretty_generate(processed_data)
46
+ end
47
+
48
+ def processed_data
49
+ @oprocessed_data ||=
50
+ queries.inject(input_json) { |json, query| eval("json#{query}") }
51
+ end
52
+
53
+ def queries
54
+ @queries ||= options.arguments
55
+ end
56
+
57
+ def tty?
58
+ STDOUT.tty?
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,36 @@
1
+ module Jqr
2
+ class Runner
3
+ attr_reader :arguments
4
+
5
+ def initialize(arguments = ARGV)
6
+ @arguments = arguments
7
+ end
8
+
9
+ def command_class
10
+ case
11
+ when options.help?
12
+ Commands::Help
13
+ when options.version?
14
+ Commands::Version
15
+ else
16
+ Commands::Runner
17
+ end
18
+ end
19
+
20
+ def run
21
+ command_class.new(options).run
22
+ end
23
+
24
+ private
25
+
26
+ def options
27
+ @options ||= Slop.parse(arguments) do |o|
28
+ o.banner = "Usage: cat JSON | jqr [options] QUERY"
29
+
30
+ o.bool "-v", "--version", "Print the version"
31
+ o.bool "-h", "--help", "Show this message"
32
+ o.bool "--no-color", "Disable colorful output"
33
+ end
34
+ end
35
+ end
36
+ end
@@ -1,3 +1,3 @@
1
1
  module Jqr
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jqr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - nownabe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-10 00:00:00.000000000 Z
11
+ date: 2015-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: slop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -85,6 +99,13 @@ files:
85
99
  - bin/jqr
86
100
  - jqr.gemspec
87
101
  - lib/jqr.rb
102
+ - lib/jqr/commands.rb
103
+ - lib/jqr/commands/base.rb
104
+ - lib/jqr/commands/help.rb
105
+ - lib/jqr/commands/runner.rb
106
+ - lib/jqr/commands/version.rb
107
+ - lib/jqr/processor.rb
108
+ - lib/jqr/runner.rb
88
109
  - lib/jqr/version.rb
89
110
  homepage: https://github.com/nownabe/jqr
90
111
  licenses: []