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