Class: PrintJsonResponse
- Inherits:
-
Object
- Object
- PrintJsonResponse
- Defined in:
- lib/print_json_response.rb
Constant Summary collapse
- VERSION =
'2.0.1'
- 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.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/print_json_response.rb', line 91 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
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/print_json_response.rb', line 35 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. << <<-BANNER 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 '--diff-opts=DIFF_OPTS', 'Options for diff' do |value| [:diff_opts] = value end if djr opts.on '--[no-]irb', 'Dump the results into $response in IRB' do |value| [:irb] = value end end op.parse! argv abort op.to_s if argv.empty? urls = argv.shift(djr ? 2 : 1) [:path] = argv.dup argv.clear return urls, end |
.run(program_name, argv = ARGV) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/print_json_response.rb', line 83 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
106 107 108 109 110 111 112 113 |
# File 'lib/print_json_response.rb', line 106 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
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/print_json_response.rb', line 115 def diff results 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
132 133 134 135 136 137 138 139 |
# File 'lib/print_json_response.rb', line 132 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
141 142 143 144 145 146 |
# File 'lib/print_json_response.rb', line 141 def irb json require 'irb' $response = json puts "JSON response is in $response" IRB.start end |
#run ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/print_json_response.rb', line 148 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 |