mirror of
https://github.com/jnunemaker/httparty
synced 2023-03-27 23:23:07 -04:00
Added the bin/httparty executable.
This uses HTTParty to query services and format the responses in several different ways. It's a handy way to explore web services or simply query them. Conflicts: httparty.gemspec lib/httparty/version.rb
This commit is contained in:
parent
3c54cf2caa
commit
5615a4ba05
4 changed files with 78 additions and 3 deletions
1
Manifest
1
Manifest
|
@ -1,3 +1,4 @@
|
|||
bin/httparty
|
||||
examples/aaws.rb
|
||||
examples/basic.rb
|
||||
examples/delicious.rb
|
||||
|
|
76
bin/httparty
Executable file
76
bin/httparty
Executable file
|
@ -0,0 +1,76 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
require "optparse"
|
||||
require "pp"
|
||||
require "rexml/document"
|
||||
|
||||
$:.unshift(File.join(File.dirname(__FILE__), "/../lib"))
|
||||
require "httparty"
|
||||
|
||||
opts = {
|
||||
:action => :get,
|
||||
:headers => {},
|
||||
:keep_body => true,
|
||||
:verbose => false,
|
||||
:pretty_print => false
|
||||
}
|
||||
|
||||
OptionParser.new do |o|
|
||||
o.banner = "USAGE: #{$0} [options] [url]"
|
||||
o.on("-f",
|
||||
"--format [FORMAT]",
|
||||
"Body format: plain, json or xml") do |f|
|
||||
opts[:format] = f.downcase.to_sym
|
||||
end
|
||||
o.on("-r", "--ruby", "Dump output in Ruby pretty-print format") do |r|
|
||||
opts[:pretty_print] = true
|
||||
end
|
||||
o.on("-a",
|
||||
"--action [ACTION]",
|
||||
"HTTP action: get (default), post, put or delete") do |a|
|
||||
opts[:action] = a.downcase.to_sym
|
||||
end
|
||||
o.on("-d",
|
||||
"--data [BODY]",
|
||||
"Data to put in request body (prefix with '@' for file)") do |d|
|
||||
if d =~ /^@/
|
||||
opts[:data] = open(d).read
|
||||
else
|
||||
opts[:data] = d
|
||||
end
|
||||
end
|
||||
o.on("-H", "--header [NAME=VALUE]", "Additional HTTP headers in NAME=VALUE form") do |h|
|
||||
name, value = h.split('=')
|
||||
opts[:headers][name] = value
|
||||
end
|
||||
o.on("-v", "--verbose", "If set, print verbose output") do |v|
|
||||
opts[:verbose] = true
|
||||
end
|
||||
o.on("-h", "--help", "Show help documentation") do |h|
|
||||
puts o
|
||||
exit
|
||||
end
|
||||
end.parse!
|
||||
|
||||
puts "Querying #{ARGV.first} with options: #{opts.inspect}" if opts[:verbose]
|
||||
|
||||
if ARGV.empty?
|
||||
STDERR.puts "You need to provide a URL"
|
||||
STDERR.puts "USAGE: #{$0} [options] [url]"
|
||||
end
|
||||
|
||||
response = HTTParty.send(opts[:action], ARGV.first, opts)
|
||||
|
||||
if opts[:pretty_print]
|
||||
response.delete('_body')
|
||||
pp response
|
||||
else
|
||||
case opts[:format]
|
||||
when :json
|
||||
puts JSON.pretty_generate(response['_body'])
|
||||
when :xml
|
||||
REXML::Document.new(response['_body']).write(STDOUT, 2)
|
||||
else
|
||||
puts response
|
||||
end
|
||||
end
|
|
@ -1,5 +1,3 @@
|
|||
# -*- encoding: utf-8 -*-
|
||||
|
||||
Gem::Specification.new do |s|
|
||||
s.name = %q{httparty}
|
||||
s.version = "0.2.4"
|
||||
|
|
Loading…
Reference in a new issue