jrq 0.1.4 → 0.1.5

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 (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +100 -10
  3. data/lib/jrq.rb +4 -1
  4. data/lib/jrq/version.rb +1 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c50da8bff7d688714e46391f660f01693c761cdf
4
- data.tar.gz: dc060d85212b1fcc81e9fce6cf33530334d6aec0
3
+ metadata.gz: 7e977293feceafe2cf96776d29cf80a25f705b88
4
+ data.tar.gz: f035ec0576334a3d30d4e7167217996981053137
5
5
  SHA512:
6
- metadata.gz: d8ea8fe85f67a70dfe125ebc0ed01b2040ef11b12ed26a1fbd42896ae5ea443e1de73c8081cdd85b20aef33aa9b4f988bef9bdfa3d6c06896038c95b4ce614a3
7
- data.tar.gz: 7103a8048974c7f3bb7e395dc7225d079edd1415354562a732f37e609adc51ce06d064109d0bf82b74aebf9526e1b16a3418ce3af771ffb496bcf853aad98d2f
6
+ metadata.gz: 45000c9249d529677a6af8f978d0da35c0bcc7e3198fb392a5b030426f70c9afebce8a3ef8ab9a5c40b7a3da9bb0aaa1d9eadf03542949d666d4d6a9df414f08
7
+ data.tar.gz: cbb19d2af1cce0501e2e8e4cd9ad01ed3d7d0a3a5ddfd22d730d0a6b33ef4eacf365a5bb50371369a20b4614bc25c1d9cfd0c3f6e6b782d7675ed69187c29d40
data/README.md CHANGED
@@ -1,4 +1,11 @@
1
- # Jrq
1
+ # jrq
2
+
3
+ jrq is a CLI JSON processor for Rubyists. jrq enable you to filter/map/reduce JSON without studying new syntax.
4
+
5
+ ```
6
+ $ echo '{"foo": [1, 2, 3], "bar": 100 }' \
7
+ | jrq '_.foo.reduce(&:+) * _.bar' # => 600
8
+ ```
2
9
 
3
10
  ## Installation
4
11
 
@@ -18,11 +25,90 @@ Or install it yourself as:
18
25
 
19
26
  ## Usage
20
27
 
21
- - sample JSON data
22
- - AWS ip-ranges.json
23
- - CloudFormation Template
24
- - ECS Task Definition
25
- - JSON Schema / Swagger
28
+ JSON data from STDIN will be parsed and passed as `_` variable. Because jrq just evaling given string in the context of Ruby, actually it's almost same as doing like `cat a.json | ruby -rjson -e "JSON.load(STDIN)['foo']"`. Here are some samples below:
29
+
30
+ `_` is a Hash, so `_.keys` would be nice place to start with.
31
+
32
+ ```
33
+ $ aws ec2 describe-instances \
34
+ | jrq '_.keys'
35
+ [
36
+ "Reservations"
37
+ ]
38
+ ```
39
+
40
+ jrq parses given JSON with [Hashie::Mash](https://github.com/intridea/hashie#mash), so you can access json fields with dot(`.`) like Ruby's method access.
41
+
42
+ ```
43
+ $ aws ec2 describe-instances \
44
+ | jrq '_.Reservations.class'
45
+ Array
46
+ ```
47
+
48
+ Then digging deeper...
49
+
50
+ ```
51
+ $ aws ec2 describe-instances \
52
+ | jrq -r '_.Reservations.map{|r| r.keys }.flatten.uniq'
53
+ [
54
+ "OwnerId",
55
+ "ReservationId",
56
+ "Groups",
57
+ "Instances",
58
+ "RequesterId"
59
+ ]
60
+ ```
61
+
62
+ ... to get information you need.
63
+
64
+ ```
65
+ $ aws ec2 describe-instances \
66
+ | jrq 'ins = _.Reservations.first.Instances.first;
67
+ [ins.InstanceId, ins.VpcId, ins.SubnetId, ins.PrivateIpAddress]'
68
+ [
69
+ "i-aaaaaaaa",
70
+ "vpc-5dxxxxx8",
71
+ "subnet-a1xxxxx6",
72
+ "172.31.25.92"
73
+ ]
74
+ ```
75
+
76
+ ```
77
+ $ aws ec2 describe-instances \
78
+ | jrq '_.Reservations.map{|r| r.Instances.map{|i| i.InstanceId } }.flatten'
79
+ [
80
+ "i-aaaaaaaa",
81
+ "i-bbbbbbbb",
82
+ "i-cccccccc",
83
+ "i-dddddddd",
84
+ ]
85
+ ```
86
+
87
+ With `-r` or `--raw` option, raw output would be displayed. It's suitable for passing to another program as input.
88
+
89
+ ```
90
+ $ aws ec2 describe-instances \
91
+ | jrq -r '_.Reservations.map{|r| r.Instances.map{|i| i.InstanceId } }.flatten'
92
+ i-aaaaaaaa
93
+ i-bbbbbbbb
94
+ i-cccccccc
95
+ i-dddddddd
96
+ ```
97
+
98
+ You can even require another Ruby library.
99
+
100
+ ```
101
+ $ curl -s https://ip-ranges.amazonaws.com/ip-ranges.json \
102
+ | jrq 'require "ipaddr"; _.prefixes.select{|p| p.region == "ap-northeast-1" }.map{|p| IPAddr.new(p.ip_prefix).to_i.to_s(2) }'
103
+
104
+ [
105
+ "11011000000000000000000000000",
106
+ "101110001100111110000000000000",
107
+ "110100010001000000000000000000",
108
+ "110100010111000011110000000000",
109
+ "110100010111000101000000000000",
110
+ ...
111
+ ```
26
112
 
27
113
  ## Development
28
114
 
@@ -42,17 +128,21 @@ or
42
128
 
43
129
  $ gem push jrq-x.y.z.gem
44
130
 
45
- ### Plan
131
+ ### Plan & TODOs
46
132
 
47
133
  - color
48
134
  - launch REPL when STDIN isn't given
49
135
  - jrq foo.json => also launch REPL
50
- - (maybe) _ -> j ?
51
136
 
52
- ## Contributing
137
+ - prepare sample JSON data
138
+ - AWS ip-ranges.json
139
+ - CloudFormation Template
140
+ - ECS Task Definition
141
+ - JSON Schema / Swagger
53
142
 
54
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jrq.
143
+ ## Contributing
55
144
 
145
+ Bug reports and pull requests are welcome on GitHub at https://github.com/memerelics/jrq/issues
56
146
 
57
147
  ## License
58
148
 
data/lib/jrq.rb CHANGED
@@ -9,7 +9,10 @@ module Jrq
9
9
 
10
10
  def run(args, opts)
11
11
  _ = JSON.load(stdin)
12
- _ = Hashie::Mash.new(_) # is_a Hash
12
+ _ = case _
13
+ when Hash then Hashie::Mash.new(_)
14
+ when Array then _.map{|obj| Hashie::Mash.new(obj) }
15
+ end
13
16
  if args.length.zero?
14
17
  display(_, opts)
15
18
  else
@@ -1,3 +1,3 @@
1
1
  module Jrq
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jrq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - memerelics
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-19 00:00:00.000000000 Z
11
+ date: 2016-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hashie