Skip to content

Instantly share code, notes, and snippets.

@jmhodges
Created May 5, 2009 00:42
Show Gist options
  • Save jmhodges/106756 to your computer and use it in GitHub Desktop.
Save jmhodges/106756 to your computer and use it in GitHub Desktop.
print json response
#!/usr/bin/env ruby
require 'rubygems'
require 'net/http'
require 'json'
DEFAULT_HOST = 'http://127.0.0.1:7000'
def read_from_stdin
while url = gets
process_url url
end
end
def read_from_args
ARGV.each { |url| process_url url }
end
def process_url(url)
if url !~ /\Ahttp/
p = url.split('/').compact
p.unshift(DEFAULT_HOST)
url = p.join('/')
end
puts "Retrieving #{url}:"
resp = Net::HTTP.get_response(URI.parse(url))
raw = resp.body
data = JSON.parse(raw)
puts JSON.pretty_unparse(data)
end
if ARGV.empty?
read_from_stdin
else
read_from_args
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment