PageRenderTime 10ms CodeModel.GetById 6ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 1ms

/bin/httparty

http://github.com/jnunemaker/httparty
Ruby | 121 lines | 102 code | 18 blank | 1 comment | 10 complexity | ac4005465ebb76a0a9064d30e7b095ba MD5 | raw file
  1#!/usr/bin/env ruby
  2
  3require "optparse"
  4require "pp"
  5
  6$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "/../lib"))
  7require "httparty"
  8
  9opts = {
 10  action: :get,
 11  headers: {},
 12  verbose: false
 13}
 14
 15OptionParser.new do |o|
 16  o.banner = "USAGE: #{$PROGRAM_NAME} [options] [url]"
 17
 18  o.on("-f",
 19       "--format [FORMAT]",
 20       "Output format to use instead of pretty-print ruby: " \
 21       "plain, csv, json or xml") do |f|
 22    opts[:output_format] = f.downcase.to_sym
 23  end
 24
 25  o.on("-a",
 26       "--action [ACTION]",
 27       "HTTP action: get (default), post, put, delete, head, or options") do |a|
 28    opts[:action] = a.downcase.to_sym
 29  end
 30
 31  o.on("-d",
 32       "--data [BODY]",
 33       "Data to put in request body (prefix with '@' for file)") do |d|
 34    if d =~ /^@/
 35      opts[:body] = open(d[1..-1]).read
 36    else
 37      opts[:body] = d
 38    end
 39  end
 40
 41  o.on("-H", "--header [NAME:VALUE]", "Additional HTTP headers in NAME:VALUE form") do |h|
 42    abort "Invalid header specification, should be Name:Value" unless h =~ /.+:.+/
 43    name, value = h.split(':')
 44    opts[:headers][name.strip] = value.strip
 45  end
 46
 47  o.on("-v", "--verbose", "If set, print verbose output") do |v|
 48    opts[:verbose] = true
 49  end
 50
 51  o.on("-u", "--user [CREDS]", "Use basic authentication. Value should be user:password") do |u|
 52    abort "Invalid credentials format. Must be user:password" unless u =~ /.*:.+/
 53    user, password = u.split(':')
 54    opts[:basic_auth] = { username: user, password: password }
 55  end
 56
 57  o.on("-r", "--response-code", "Command fails if response code >= 400") do
 58    opts[:response_code] = true
 59  end
 60
 61  o.on("-h", "--help", "Show help documentation") do |h|
 62    puts o
 63    exit
 64  end
 65
 66  o.on("--version", "Show HTTParty version") do |ver|
 67    puts "Version: #{HTTParty::VERSION}"
 68    exit
 69  end
 70end.parse!
 71
 72if ARGV.empty?
 73  STDERR.puts "You need to provide a URL"
 74  STDERR.puts "USAGE: #{$PROGRAM_NAME} [options] [url]"
 75end
 76
 77def dump_headers(response)
 78  resp_type = Net::HTTPResponse::CODE_TO_OBJ[response.code.to_s]
 79  puts "#{response.code} #{resp_type.to_s.sub(/^Net::HTTP/, '')}"
 80  response.headers.each do |n, v|
 81    puts "#{n}: #{v}"
 82  end
 83  puts
 84end
 85
 86if opts[:verbose]
 87  puts "#{opts[:action].to_s.upcase} #{ARGV.first}"
 88  opts[:headers].each do |n, v|
 89    puts "#{n}: #{v}"
 90  end
 91  puts
 92end
 93
 94response = HTTParty.send(opts[:action], ARGV.first, opts)
 95if opts[:output_format].nil?
 96  dump_headers(response) if opts[:verbose]
 97  pp response
 98else
 99  print_format = opts[:output_format]
100  dump_headers(response) if opts[:verbose]
101
102  case opts[:output_format]
103    when :json
104      begin
105        require 'json'
106        puts JSON.pretty_generate(response)
107      rescue LoadError
108        puts YAML.dump(response)
109      end
110    when :xml
111      require 'rexml/document'
112      REXML::Document.new(response.body).write(STDOUT, 2)
113      puts
114    when :csv
115      require 'csv'
116      puts CSV.parse(response.body).map(&:to_s)
117    else
118      puts response
119  end
120end
121exit false if opts[:response_code] && response.code >= 400