Class: PrintJsonResponse
- Inherits:
-
Object
- Object
- PrintJsonResponse
- Defined in:
- lib/print_json_response.rb
Constant Summary collapse
- VERSION =
'2.0'- DEFAULT_HOST =
'http://127.0.0.1:3000'- CONFIG_PATH =
File. '~/.pjr'
Class Method Summary collapse
Instance Method Summary collapse
- #default_host ⇒ Object
- #diff(results) ⇒ Object
- #fetch(url) ⇒ Object
-
#initialize(urls, options) ⇒ PrintJsonResponse
constructor
A new instance of PrintJsonResponse.
- #irb(json) ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(urls, options) ⇒ PrintJsonResponse
Returns a new instance of PrintJsonResponse.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/print_json_response.rb', line 69 def initialize urls, @urls = urls.map do |url| unless url =~ /\Ahttp/i then url = '/' + url unless url.start_with? '/' url = default_host + url end URI.parse url end @diff_opts = [:diff_opts] @irb = [:irb] @path = [:path] end |
Class Method Details
.process_args(program_name, argv) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/print_json_response.rb', line 15 def self.process_args program_name, argv = {} [:diff_opts] = '-u' djr = program_name =~ /djr$/ op = OptionParser.new do |opts| opts.program_name = program_name opts.version = VERSION opts. = if djr then "Usage: #{program_name} [options] URL URL [PATH ...]" else "Usage: #{program_name} [options] URL [PATH ...]" end opts. << "\nPATH is a path of keys in the JSON document you wish to descend. Given a JSON\ndocument like:\n\n{ \"a\": { \"b\": [\"c\", \"d\"] } }\n\n`pjr http://example/json a b` will print [\"c\", \"d\"]\n\n BANNER\n\n opts.on '--diff-opts=DIFF_OPTS', 'Options for diff' do |value|\n options[:diff_opts] = value\n end if djr\n\n opts.on '--[no-]irb', 'Dump the results into @response in IRB' do |value|\n options[:irb] = value\n end\n end\n\n op.parse! argv\n\n abort op.to_s if argv.empty?\n\n urls = argv.shift(djr ? 2 : 1)\n\n options[:path] = argv\n\n return urls, options\nend\n" |
.run(program_name, argv = ARGV) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/print_json_response.rb', line 61 def self.run program_name, argv = ARGV urls, = process_args program_name, argv pjr = new urls, pjr.run end |
Instance Method Details
#default_host ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/print_json_response.rb', line 84 def default_host if File.exist? CONFIG_PATH and File.read(CONFIG_PATH) =~ /default_host:(.+)/i then $1.strip else DEFAULT_HOST end end |
#diff(results) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/print_json_response.rb', line 93 def diff results require 'pp' require 'tempfile' require 'enumerator' tempfiles = [] results.each_with_index do |result, i| io = Tempfile.open "url_#{i}_" io.puts result.pretty_inspect io.flush tempfiles << io end system "diff #{@diff_opts} #{tempfiles.map { |io| io.path}.join ' '}" end |
#fetch(url) ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/print_json_response.rb', line 111 def fetch url $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 end |
#irb(json) ⇒ Object
120 121 122 123 124 125 |
# File 'lib/print_json_response.rb', line 120 def irb json require 'irb' @response = json puts "JSON response is in @response" IRB.start end |
#run ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/print_json_response.rb', line 127 def run results = @urls.map do |url| fetch url end if results.length == 2 then diff results elsif @irb then irb results.first else require 'pp' puts results.first.pretty_inspect end end |