Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make pjr releaseable as a gem
- Loading branch information
Showing
8 changed files
with
169 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
=== 1.0 / ?? | ||
|
||
* Major Enhancement | ||
* Birthday! | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
History.txt | ||
Manifest.txt | ||
README.txt | ||
Rakefile | ||
bin/pjr |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
= pjr (print_json_response) | ||
|
||
* http://github.com/jdunphy/print_json_response | ||
|
||
== DESCRIPTION | ||
|
||
A simple script to grab JSON from a URI and view it in a readable manner. | ||
|
||
== SYNOPSIS: | ||
|
||
$ pjr "http://someplace.com/someurl.json" | ||
Retrieving http://someplace.com/someurl.json: | ||
{ | ||
"what":"oh hey look some pretty-printed json" | ||
} | ||
|
||
You can also drop the hostname nonsense when hitting localhost | ||
|
||
$ pjr "/someurl.json" | ||
Retrieving http://127.0.0.1:3000/someurl.json: | ||
{ | ||
"what":"oh hey look some pretty-printed json" | ||
} | ||
|
||
Override the base default host of localhost:3000 with a .pjr file. | ||
|
||
$ cat /Users/jdunphy/.pjr | ||
default_host:http://localhost:7000 | ||
|
||
pjr can jump into irb, instead of printing json | ||
|
||
$ pjr -i someurl.json | ||
Retrieving http://127.0.0.1:3000/someurl.json: | ||
Loading IRB. | ||
JSON response is in @response | ||
irb(main):001:0> @response['what'] | ||
=> "oh hey look some pretty-printed json" | ||
|
||
== INSTALL: | ||
|
||
* gem install print_json_response | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# -*- ruby -*- | ||
|
||
require 'rubygems' | ||
require 'hoe' | ||
|
||
Hoe.plugin :git | ||
|
||
Hoe.spec 'print_json_response' do | ||
developer 'Jacob Dunphy', '' | ||
end | ||
|
||
# vim: syntax=Ruby |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require 'print_json_response' | ||
|
||
PrintJsonResponse.run | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
require 'net/http' | ||
require 'optparse' | ||
require 'uri' | ||
|
||
require 'json' | ||
|
||
class PrintJsonResponse | ||
|
||
VERSION = '1.0' | ||
|
||
DEFAULT_HOST = 'http://127.0.0.1:3000' | ||
|
||
CONFIG_PATH = File.expand_path '~/.pjr' | ||
|
||
def self.process_args argv | ||
options = {} | ||
|
||
op = OptionParser.new do |opts| | ||
opts.program_name = 'pjr' | ||
opts.version = VERSION | ||
opts.banner = <<-BANNER | ||
Usage: pjr [options] URL [PATH ...] | ||
PATH is a path of keys in the JSON document you wish to descend. Given a JSON | ||
document like: | ||
{ "a": { "b": ["c", "d"] } } | ||
`pjr http://example/json a b` will print ["c", "d"] | ||
BANNER | ||
|
||
opts.on '--[no-]irb', 'Dump the results into @response in IRB' do |value| | ||
options[:irb] = value | ||
end | ||
end | ||
|
||
op.parse! argv | ||
|
||
abort op.to_s if argv.empty? | ||
|
||
url = argv.shift | ||
|
||
options[:path] = argv | ||
|
||
return url, options | ||
end | ||
|
||
def self.run argv = ARGV | ||
url, options = process_args argv | ||
|
||
pjr = new url, options | ||
|
||
pjr.run | ||
end | ||
|
||
def initialize url, options | ||
unless url =~ /\Ahttp/i then | ||
url = '/' + url unless url.start_with? '/' | ||
url = default_host + url | ||
end | ||
|
||
@url = URI.parse url | ||
|
||
@irb = options[:irb] | ||
@path = options[:path] | ||
end | ||
|
||
def default_host | ||
if File.exist? CONFIG_PATH and | ||
File.read(CONFIG_PATH) =~ /default_host:(.+)/i then | ||
$1.strip | ||
else | ||
DEFAULT_HOST | ||
end | ||
end | ||
|
||
def run | ||
$stderr.puts "Retrieving #{@url}:" if $stdout.tty? | ||
|
||
resp = Net::HTTP.get_response @url | ||
json = JSON.parse resp.body | ||
|
||
json = @path.inject json do |data, item| data[item] end | ||
|
||
if @irb then | ||
require 'irb' | ||
@response = json | ||
puts "Loading IRB." | ||
puts "JSON response is in @response" | ||
IRB.start | ||
else | ||
require 'pp' | ||
puts json.pretty_inspect | ||
end | ||
end | ||
|
||
end | ||
|