print_json_response 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/Manifest.txt +6 -0
- data/README.txt +42 -0
- data/Rakefile +12 -0
- data/bin/pjr +6 -0
- data/lib/print_json_response.rb +99 -0
- metadata +97 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
= pjr (print_json_response)
|
2
|
+
|
3
|
+
* http://github.com/jdunphy/print_json_response
|
4
|
+
|
5
|
+
== DESCRIPTION
|
6
|
+
|
7
|
+
A simple script to grab JSON from a URI and view it in a readable manner.
|
8
|
+
|
9
|
+
== SYNOPSIS:
|
10
|
+
|
11
|
+
$ pjr "http://someplace.com/someurl.json"
|
12
|
+
Retrieving http://someplace.com/someurl.json:
|
13
|
+
{
|
14
|
+
"what":"oh hey look some pretty-printed json"
|
15
|
+
}
|
16
|
+
|
17
|
+
You can also drop the hostname nonsense when hitting localhost
|
18
|
+
|
19
|
+
$ pjr "/someurl.json"
|
20
|
+
Retrieving http://127.0.0.1:3000/someurl.json:
|
21
|
+
{
|
22
|
+
"what":"oh hey look some pretty-printed json"
|
23
|
+
}
|
24
|
+
|
25
|
+
Override the base default host of localhost:3000 with a .pjr file.
|
26
|
+
|
27
|
+
$ cat /Users/jdunphy/.pjr
|
28
|
+
default_host:http://localhost:7000
|
29
|
+
|
30
|
+
pjr can jump into irb, instead of printing json
|
31
|
+
|
32
|
+
$ pjr -i someurl.json
|
33
|
+
Retrieving http://127.0.0.1:3000/someurl.json:
|
34
|
+
Loading IRB.
|
35
|
+
JSON response is in @response
|
36
|
+
irb(main):001:0> @response['what']
|
37
|
+
=> "oh hey look some pretty-printed json"
|
38
|
+
|
39
|
+
== INSTALL:
|
40
|
+
|
41
|
+
* gem install print_json_response
|
42
|
+
|
data/Rakefile
ADDED
data/bin/pjr
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'optparse'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
class PrintJsonResponse
|
8
|
+
|
9
|
+
VERSION = '1.0'
|
10
|
+
|
11
|
+
DEFAULT_HOST = 'http://127.0.0.1:3000'
|
12
|
+
|
13
|
+
CONFIG_PATH = File.expand_path '~/.pjr'
|
14
|
+
|
15
|
+
def self.process_args argv
|
16
|
+
options = {}
|
17
|
+
|
18
|
+
op = OptionParser.new do |opts|
|
19
|
+
opts.program_name = 'pjr'
|
20
|
+
opts.version = VERSION
|
21
|
+
opts.banner = <<-BANNER
|
22
|
+
Usage: pjr [options] URL [PATH ...]
|
23
|
+
|
24
|
+
PATH is a path of keys in the JSON document you wish to descend. Given a JSON
|
25
|
+
document like:
|
26
|
+
|
27
|
+
{ "a": { "b": ["c", "d"] } }
|
28
|
+
|
29
|
+
`pjr http://example/json a b` will print ["c", "d"]
|
30
|
+
|
31
|
+
BANNER
|
32
|
+
|
33
|
+
opts.on '--[no-]irb', 'Dump the results into @response in IRB' do |value|
|
34
|
+
options[:irb] = value
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
op.parse! argv
|
39
|
+
|
40
|
+
abort op.to_s if argv.empty?
|
41
|
+
|
42
|
+
url = argv.shift
|
43
|
+
|
44
|
+
options[:path] = argv
|
45
|
+
|
46
|
+
return url, options
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.run argv = ARGV
|
50
|
+
url, options = process_args argv
|
51
|
+
|
52
|
+
pjr = new url, options
|
53
|
+
|
54
|
+
pjr.run
|
55
|
+
end
|
56
|
+
|
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
|
62
|
+
|
63
|
+
@url = URI.parse url
|
64
|
+
|
65
|
+
@irb = options[:irb]
|
66
|
+
@path = options[:path]
|
67
|
+
end
|
68
|
+
|
69
|
+
def default_host
|
70
|
+
if File.exist? CONFIG_PATH and
|
71
|
+
File.read(CONFIG_PATH) =~ /default_host:(.+)/i then
|
72
|
+
$1.strip
|
73
|
+
else
|
74
|
+
DEFAULT_HOST
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def run
|
79
|
+
$stderr.puts "Retrieving #{@url}:" if $stdout.tty?
|
80
|
+
|
81
|
+
resp = Net::HTTP.get_response @url
|
82
|
+
json = JSON.parse resp.body
|
83
|
+
|
84
|
+
json = @path.inject json do |data, item| data[item] end
|
85
|
+
|
86
|
+
if @irb then
|
87
|
+
require 'irb'
|
88
|
+
@response = json
|
89
|
+
puts "Loading IRB."
|
90
|
+
puts "JSON response is in @response"
|
91
|
+
IRB.start
|
92
|
+
else
|
93
|
+
require 'pp'
|
94
|
+
puts json.pretty_inspect
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: print_json_response
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
version: "1.0"
|
9
|
+
platform: ruby
|
10
|
+
authors:
|
11
|
+
- Jacob Dunphy
|
12
|
+
autorequire:
|
13
|
+
bindir: bin
|
14
|
+
cert_chain: []
|
15
|
+
|
16
|
+
date: 2010-07-23 00:00:00 -07:00
|
17
|
+
default_executable:
|
18
|
+
dependencies:
|
19
|
+
- !ruby/object:Gem::Dependency
|
20
|
+
name: rubyforge
|
21
|
+
prerelease: false
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
segments:
|
27
|
+
- 2
|
28
|
+
- 0
|
29
|
+
- 4
|
30
|
+
version: 2.0.4
|
31
|
+
type: :development
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: hoe
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
segments:
|
41
|
+
- 2
|
42
|
+
- 6
|
43
|
+
- 1
|
44
|
+
version: 2.6.1
|
45
|
+
type: :development
|
46
|
+
version_requirements: *id002
|
47
|
+
description: A simple script to grab JSON from a URI and view it in a readable manner.
|
48
|
+
email:
|
49
|
+
- ""
|
50
|
+
executables:
|
51
|
+
- pjr
|
52
|
+
extensions: []
|
53
|
+
|
54
|
+
extra_rdoc_files:
|
55
|
+
- History.txt
|
56
|
+
- Manifest.txt
|
57
|
+
- README.txt
|
58
|
+
files:
|
59
|
+
- History.txt
|
60
|
+
- Manifest.txt
|
61
|
+
- README.txt
|
62
|
+
- Rakefile
|
63
|
+
- bin/pjr
|
64
|
+
- lib/print_json_response.rb
|
65
|
+
has_rdoc: true
|
66
|
+
homepage: http://github.com/jdunphy/print_json_response
|
67
|
+
licenses: []
|
68
|
+
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options:
|
71
|
+
- --main
|
72
|
+
- README.txt
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
version: "0"
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
requirements: []
|
90
|
+
|
91
|
+
rubyforge_project: print_json_response
|
92
|
+
rubygems_version: 1.3.6
|
93
|
+
signing_key:
|
94
|
+
specification_version: 3
|
95
|
+
summary: A simple script to grab JSON from a URI and view it in a readable manner.
|
96
|
+
test_files: []
|
97
|
+
|