-
-
Save jmhodges/106756 to your computer and use it in GitHub Desktop.
print json response
This file contains hidden or 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
#!/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