print_json_response 1.0.1 → 2.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.
data/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ === 2.0
2
+
3
+ * Major enhancement:
4
+ * Added djr "diff json response", diff the output of two json results
5
+
1
6
  === 1.0 / ??
2
7
 
3
8
  * Major Enhancement
data/Manifest.txt CHANGED
@@ -2,5 +2,6 @@ History.txt
2
2
  Manifest.txt
3
3
  README.txt
4
4
  Rakefile
5
+ bin/djr
5
6
  bin/pjr
6
7
  lib/print_json_response.rb
data/README.txt CHANGED
@@ -5,6 +5,7 @@
5
5
  == DESCRIPTION
6
6
 
7
7
  A simple script to grab JSON from a URI and view it in a readable manner.
8
+ Also, diff the JSON result of two URIs.
8
9
 
9
10
  == SYNOPSIS:
10
11
 
@@ -36,6 +37,15 @@ pjr can jump into irb, instead of printing json
36
37
  irb(main):001:0> @response['what']
37
38
  => "oh hey look some pretty-printed json"
38
39
 
40
+ djr can diff two JSON results:
41
+
42
+ $ djr http://one.example/some/path http://two.example/some/path
43
+
44
+ pjr and djr can walk the JSON tree for more specific results:
45
+
46
+ $ pjr http://one.example/some/path a b c
47
+ $ djr http://one.example/some/path http://two.example/some/path a b c
48
+
39
49
  == INSTALL:
40
50
 
41
51
  * gem install print_json_response
data/bin/djr ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'print_json_response'
4
+
5
+ PrintJsonResponse.run $0, ARGV
6
+
data/bin/pjr CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  require 'print_json_response'
4
4
 
5
- PrintJsonResponse.run
5
+ PrintJsonResponse.run $0, ARGV
6
6
 
@@ -6,20 +6,28 @@ require 'json'
6
6
 
7
7
  class PrintJsonResponse
8
8
 
9
- VERSION = '1.0.1'
9
+ VERSION = '2.0'
10
10
 
11
11
  DEFAULT_HOST = 'http://127.0.0.1:3000'
12
12
 
13
13
  CONFIG_PATH = File.expand_path '~/.pjr'
14
14
 
15
- def self.process_args argv
15
+ def self.process_args program_name, argv
16
16
  options = {}
17
+ options[:diff_opts] = '-u'
18
+
19
+ djr = program_name =~ /djr$/
17
20
 
18
21
  op = OptionParser.new do |opts|
19
- opts.program_name = 'pjr'
22
+ opts.program_name = program_name
20
23
  opts.version = VERSION
21
- opts.banner = <<-BANNER
22
- Usage: pjr [options] URL [PATH ...]
24
+ opts.banner = if djr then
25
+ "Usage: #{program_name} [options] URL URL [PATH ...]"
26
+ else
27
+ "Usage: #{program_name} [options] URL [PATH ...]"
28
+ end
29
+
30
+ opts.banner << <<-BANNER
23
31
 
24
32
  PATH is a path of keys in the JSON document you wish to descend. Given a JSON
25
33
  document like:
@@ -30,6 +38,10 @@ document like:
30
38
 
31
39
  BANNER
32
40
 
41
+ opts.on '--diff-opts=DIFF_OPTS', 'Options for diff' do |value|
42
+ options[:diff_opts] = value
43
+ end if djr
44
+
33
45
  opts.on '--[no-]irb', 'Dump the results into @response in IRB' do |value|
34
46
  options[:irb] = value
35
47
  end
@@ -39,31 +51,34 @@ document like:
39
51
 
40
52
  abort op.to_s if argv.empty?
41
53
 
42
- url = argv.shift
54
+ urls = argv.shift(djr ? 2 : 1)
43
55
 
44
56
  options[:path] = argv
45
57
 
46
- return url, options
58
+ return urls, options
47
59
  end
48
60
 
49
- def self.run argv = ARGV
50
- url, options = process_args argv
61
+ def self.run program_name, argv = ARGV
62
+ urls, options = process_args program_name, argv
51
63
 
52
- pjr = new url, options
64
+ pjr = new urls, options
53
65
 
54
66
  pjr.run
55
67
  end
56
68
 
57
- def initialize url, options
58
- unless url =~ /\Ahttp/i then
59
- url = '/' + url unless url.start_with? '/'
60
- url = default_host + url
61
- end
69
+ def initialize urls, options
70
+ @urls = urls.map do |url|
71
+ unless url =~ /\Ahttp/i then
72
+ url = '/' + url unless url.start_with? '/'
73
+ url = default_host + url
74
+ end
62
75
 
63
- @url = URI.parse url
76
+ URI.parse url
77
+ end
64
78
 
65
- @irb = options[:irb]
66
- @path = options[:path]
79
+ @diff_opts = options[:diff_opts]
80
+ @irb = options[:irb]
81
+ @path = options[:path]
67
82
  end
68
83
 
69
84
  def default_host
@@ -75,23 +90,52 @@ document like:
75
90
  end
76
91
  end
77
92
 
78
- def run
79
- $stderr.puts "Retrieving #{@url}:" if $stdout.tty?
93
+ def diff results
94
+ require 'pp'
95
+ require 'tempfile'
96
+ require 'enumerator'
97
+
98
+ tempfiles = []
99
+
100
+ results.each_with_index do |result, i|
101
+ io = Tempfile.open "url_#{i}_"
102
+ io.puts result.pretty_inspect
103
+ io.flush
104
+
105
+ tempfiles << io
106
+ end
107
+
108
+ system "diff #{@diff_opts} #{tempfiles.map { |io| io.path}.join ' '}"
109
+ end
80
110
 
81
- resp = Net::HTTP.get_response @url
111
+ def fetch url
112
+ $stderr.puts "Retrieving #{url}:" if $stdout.tty?
113
+
114
+ resp = Net::HTTP.get_response url
82
115
  json = JSON.parse resp.body
83
116
 
84
117
  json = @path.inject json do |data, item| data[item] end
118
+ end
119
+
120
+ def irb json
121
+ require 'irb'
122
+ @response = json
123
+ puts "JSON response is in @response"
124
+ IRB.start
125
+ end
126
+
127
+ def run
128
+ results = @urls.map do |url|
129
+ fetch url
130
+ end
85
131
 
86
- if @irb then
87
- require 'irb'
88
- @response = json
89
- puts "Loading IRB."
90
- puts "JSON response is in @response"
91
- IRB.start
132
+ if results.length == 2 then
133
+ diff results
134
+ elsif @irb then
135
+ irb results.first
92
136
  else
93
137
  require 'pp'
94
- puts json.pretty_inspect
138
+ puts results.first.pretty_inspect
95
139
  end
96
140
  end
97
141
 
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: print_json_response
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 3
4
5
  prerelease: false
5
6
  segments:
6
- - 1
7
+ - 2
7
8
  - 0
8
- - 1
9
- version: 1.0.1
9
+ version: "2.0"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jacob Dunphy
@@ -14,16 +14,18 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-05 00:00:00 -07:00
17
+ date: 2010-10-08 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: json
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
24
25
  requirements:
25
26
  - - ">="
26
27
  - !ruby/object:Gem::Version
28
+ hash: 3
27
29
  segments:
28
30
  - 0
29
31
  version: "0"
@@ -33,9 +35,11 @@ dependencies:
33
35
  name: rubyforge
34
36
  prerelease: false
35
37
  requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
36
39
  requirements:
37
40
  - - ">="
38
41
  - !ruby/object:Gem::Version
42
+ hash: 7
39
43
  segments:
40
44
  - 2
41
45
  - 0
@@ -47,20 +51,25 @@ dependencies:
47
51
  name: hoe
48
52
  prerelease: false
49
53
  requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
50
55
  requirements:
51
56
  - - ">="
52
57
  - !ruby/object:Gem::Version
58
+ hash: 19
53
59
  segments:
54
60
  - 2
55
61
  - 6
56
- - 1
57
- version: 2.6.1
62
+ - 2
63
+ version: 2.6.2
58
64
  type: :development
59
65
  version_requirements: *id003
60
- description: A simple script to grab JSON from a URI and view it in a readable manner.
66
+ description: |-
67
+ A simple script to grab JSON from a URI and view it in a readable manner.
68
+ Also, diff the JSON result of two URIs.
61
69
  email:
62
70
  - ""
63
71
  executables:
72
+ - djr
64
73
  - pjr
65
74
  extensions: []
66
75
 
@@ -73,6 +82,7 @@ files:
73
82
  - Manifest.txt
74
83
  - README.txt
75
84
  - Rakefile
85
+ - bin/djr
76
86
  - bin/pjr
77
87
  - lib/print_json_response.rb
78
88
  has_rdoc: true
@@ -86,25 +96,29 @@ rdoc_options:
86
96
  require_paths:
87
97
  - lib
88
98
  required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
89
100
  requirements:
90
101
  - - ">="
91
102
  - !ruby/object:Gem::Version
103
+ hash: 3
92
104
  segments:
93
105
  - 0
94
106
  version: "0"
95
107
  required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
96
109
  requirements:
97
110
  - - ">="
98
111
  - !ruby/object:Gem::Version
112
+ hash: 3
99
113
  segments:
100
114
  - 0
101
115
  version: "0"
102
116
  requirements: []
103
117
 
104
118
  rubyforge_project: print_json_response
105
- rubygems_version: 1.3.6
119
+ rubygems_version: 1.3.7
106
120
  signing_key:
107
121
  specification_version: 3
108
- summary: A simple script to grab JSON from a URI and view it in a readable manner.
122
+ summary: A simple script to grab JSON from a URI and view it in a readable manner
109
123
  test_files: []
110
124