jgrep 1.0.0 → 1.0.1

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.
Files changed (4) hide show
  1. data/bin/jgrep +46 -4
  2. data/jgrep.gemspec +1 -1
  3. data/lib/jgrep.rb +1 -1
  4. metadata +2 -2
data/bin/jgrep CHANGED
@@ -1,16 +1,58 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'jgrep'
4
+ require 'optparse'
5
+
6
+ options = {:flat => false}
7
+
8
+ OptionParser.new do |opts|
9
+ opts.banner = "Usage: jgrep [options] \"expression\""
10
+ opts.on("-s", "--simple FIELD", "Display a single field from each of the resulting json documents") do |field|
11
+ options[:field] = field
12
+ end
13
+ opts.on("-f", "--flat", "Display non pretty json") do
14
+ options[:flat] = true
15
+ end
16
+
17
+ end.parse!
18
+
4
19
 
5
20
  begin
6
- raise "Please pass a valid arugment to jgrep" if ARGV == []
21
+ raise "Please pass a valid arugment to jgrep" if ARGV == [] || ARGV ==["-s"]
7
22
  rescue Exception => e
8
23
  STDERR.puts e
9
24
  exit 1
10
25
  end
11
26
 
12
- json = STDIN.read
27
+ begin
28
+ json = STDIN.read
13
29
 
30
+ unless options[:field]
31
+ result = JGrep::jgrep((json), ARGV.last)
32
+ unless result == "[]"
33
+ (options[:flat] == false) ? puts(JSON.pretty_generate(result)) : puts(result.to_json)
34
+ end
35
+ else
36
+ if options[:field] =~ /=|<|>/
37
+ raise "'-s' flag requires a field to display"
38
+ end
14
39
 
15
- result = JGrep::jgrep((json), ARGV[0])
16
- puts result unless result == "[]"
40
+ result = JGrep::jgrep((json), ARGV.last)
41
+
42
+ result.each do |r|
43
+ options[:field].split(".").each do |node|
44
+ unless r.is_a? Array
45
+ r = r[node]
46
+ else
47
+ break
48
+ end
49
+ end
50
+ unless r.nil?
51
+ (!r.is_a?(Array) || !r.is_a?(Hash)) ? puts(r) : puts(JSON.pretty_generate(r))
52
+ end
53
+ end
54
+ end
55
+ rescue Exception => e
56
+ STDERR.puts "Error - #{e}"
57
+ exit 1
58
+ end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "jgrep"
3
- s.version = "1.0.0"
3
+ s.version = "1.0.1"
4
4
 
5
5
  s.authors = ["P Loubser"]
6
6
  s.date = %q{2011-07-19}
@@ -18,7 +18,7 @@ module JGrep
18
18
  result << document
19
19
  end
20
20
  end
21
- return result.to_json
21
+ return result
22
22
 
23
23
  rescue NameError => e
24
24
  var = e.to_s
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 1
7
7
  - 0
8
- - 0
9
- version: 1.0.0
8
+ - 1
9
+ version: 1.0.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - P Loubser