Skip to content

Commit

Permalink
Add json-path-following feature
Browse files Browse the repository at this point in the history
Make pjr releaseable as a gem
  • Loading branch information
drbrain committed Jul 13, 2010
1 parent 3aa1edb commit b0083fe
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 96 deletions.
5 changes: 5 additions & 0 deletions History.txt
@@ -0,0 +1,5 @@
=== 1.0 / ??

* Major Enhancement
* Birthday!

5 changes: 5 additions & 0 deletions Manifest.txt
@@ -0,0 +1,5 @@
History.txt
Manifest.txt
README.txt
Rakefile
bin/pjr
40 changes: 0 additions & 40 deletions README.markdown

This file was deleted.

42 changes: 42 additions & 0 deletions README.txt
@@ -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

12 changes: 12 additions & 0 deletions Rakefile
@@ -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
6 changes: 6 additions & 0 deletions bin/pjr
@@ -0,0 +1,6 @@
#!/usr/bin/env ruby

require 'print_json_response'

PrintJsonResponse.run

99 changes: 99 additions & 0 deletions lib/print_json_response.rb
@@ -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

56 changes: 0 additions & 56 deletions pjr

This file was deleted.

0 comments on commit b0083fe

Please sign in to comment.