Sha256: 64256f0d323a856e753e65e30f73c999a073eace314a1a077624ffd684bab671

Contents?: true

Size: 1.65 KB

Versions: 72

Compression:

Stored size: 1.65 KB

Contents

#!/usr/bin/env ruby

# jsonclient shell command.
#
# 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
# JSONClient as 'self'.  You can call JSONClient instance methods like;
# > post "https://www.example.com/resource", {'hello' => 'world'}
require 'jsonclient'
 
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
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
    exit 0
  else
    STDERR.puts res.content
    exit 1
  end
end

require 'irb'
require 'irb/completion'

class Runner
  def initialize
    @httpclient = JSONClient.new
  end

  def method_missing(msg, *a, &b)
    debug, $DEBUG = $DEBUG, true
    begin
      @httpclient.send(msg, *a, &b)
    ensure
      $DEBUG = debug
    end
  end

  def run
    IRB.setup(nil)
    ws = IRB::WorkSpace.new(binding)
    irb = IRB::Irb.new(ws)
    IRB.conf[:MAIN_CONTEXT] = irb.context

    trap("SIGINT") do
      irb.signal_handle
    end

    begin
      catch(:IRB_EXIT) do
        irb.eval_input
      end
    ensure
      IRB.irb_at_exit
    end
  end

  def to_s
    'JSONClient'
  end
end

Runner.new.run

Version data entries

72 entries across 64 versions & 8 rubygems

Version Path
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/httpclient-2.8.3/bin/jsonclient
httpclient-fixcerts-2.8.5 bin/jsonclient
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/httpclient-2.8.3/bin/jsonclient
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/gems/httpclient-2.8.3/bin/jsonclient
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/3.0.0/gems/httpclient-2.8.3/bin/jsonclient
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/bin/jsonclient
video_chat_get-0.1.9 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/bin/jsonclient
vagrant-unbundled-2.2.14.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/bin/jsonclient
video_chat_get-0.1.6 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/bin/jsonclient
vagrant-unbundled-2.2.10.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/bin/jsonclient
video_chat_get-0.1.5 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/bin/jsonclient
video_chat_get-0.1.4 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/bin/jsonclient
video_chat_get-0.1.3 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/bin/jsonclient
video_chat_get-0.1.2 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/bin/jsonclient
video_chat_get-0.1.1 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/bin/jsonclient
video_chat_get-0.1.0 vendor/bundle/ruby/2.5.0/gems/httpclient-2.8.3/bin/jsonclient
vagrant-unbundled-2.2.9.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/bin/jsonclient
vagrant-unbundled-2.2.8.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/bin/jsonclient
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.6.0/gems/httpclient-2.8.3/bin/jsonclient
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.7.0/gems/httpclient-2.8.3/bin/jsonclient