File: jsonclient

package info (click to toggle)
ruby-httpclient 2.8.3%2Bgit20211122.4658227-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,908 kB
  • sloc: ruby: 9,963; makefile: 10; sh: 2
file content (85 lines) | stat: -rwxr-xr-x 1,690 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/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