bin/jsonclient in httpclient-2.5.3.3 vs bin/jsonclient in httpclient-2.6.0

- old
+ new

@@ -1,100 +1,54 @@ #!/usr/bin/env ruby -# httpclient shell command. +# jsonclient shell command. # -# Usage: 1) % httpclient get https://www.google.co.jp/ q=ruby -# Usage: 2) % httpclient +# Usage: 1) % jsonclient post https://www.example.com/ content.json +# Usage: 2) % jsonclient # # For 1) it issues a GET request to the given URI and shows the wiredump and # the parsed result. For 2) it invokes irb shell with the binding that has a -# HTTPClient as 'self'. You can call HTTPClient instance methods like; -# > get "https://www.google.co.jp/", :q => :ruby -require 'httpclient' -require 'json' +# JSONClient as 'self'. You can call JSONClient instance methods like; +# > post "https://www.example.com/resource", {'hello' => 'world'} +require 'jsonclient' -module HTTP - class Message - # Returns JSON object of message body - alias original_content content - def content - if JSONClient::CONTENT_TYPE_JSON_REGEX =~ content_type - JSON.parse(original_content) - else - original_content - end - end +method = ARGV.shift +url = ARGV.shift +body = [] +if ['post', 'put'].include?(method) + if ARGV.size == 1 && File.exist?(ARGV[0]) + body << File.read(ARGV[0]) + else + body << ARGF.read end end - - -# JSONClient provides JSON related methods in addition to HTTPClient. -class JSONClient < HTTPClient - CONTENT_TYPE_JSON_REGEX = /(application|text)\/(x-)?json/i - - attr_accessor :content_type_json - - class JSONRequestHeaderFilter - attr_accessor :replace - - def initialize(client) - @client = client - @replace = false +if method && url + require 'pp' + client = JSONClient.new + client.debug_dev = STDERR if $DEBUG + res = client.send(method, url, *body) + STDERR.puts('RESPONSE HEADER: ') + PP.pp(res.headers, STDERR) + if res.ok? + begin + puts JSON.pretty_generate(res.content) + rescue JSON::GeneratorError + puts res.content end - - def filter_request(req) - req.header['content-type'] = @client.content_type_json if @replace - end - - def filter_response(req, res) - @replace = false - end + exit 0 + else + STDERR.puts res.content + exit 1 end - - def initialize(*args) - super - @header_filter = JSONRequestHeaderFilter.new(self) - @request_filter << @header_filter - @content_type_json = 'application/json; charset=utf-8' - end - - def post(uri, *args, &block) - @header_filter.replace = true - request(:post, uri, jsonify(argument_to_hash(args, :body, :header, :follow_redirect)), &block) - end - - def put(uri, *args, &block) - @header_filter.replace = true - request(:put, uri, jsonify(argument_to_hash(args, :body, :header)), &block) - end - -private - - def jsonify(hash) - if hash[:body] && hash[:body].is_a?(Hash) - hash[:body] = JSON.generate(hash[:body]) - end - hash - end end -METHODS = ['head', 'get', 'post', 'put', 'delete', 'options', 'propfind', 'proppatch', 'trace'] -if ARGV.size >= 2 && METHODS.include?(ARGV[0]) - client = JSONClient.new - client.debug_dev = STDERR - $DEBUG = true - require 'pp' - pp client.send(*ARGV) - exit -end - require 'irb' require 'irb/completion' class Runner def initialize - @httpclient = HTTPClient.new + @httpclient = JSONClient.new end def method_missing(msg, *a, &b) debug, $DEBUG = $DEBUG, true begin @@ -122,10 +76,10 @@ IRB.irb_at_exit end end def to_s - 'HTTPClient' + 'JSONClient' end end Runner.new.run