juliocesar-httparty 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- data/History +74 -0
- data/MIT-LICENSE +20 -0
- data/Manifest +32 -0
- data/README +36 -0
- data/Rakefile +43 -0
- data/bin/httparty +107 -0
- data/examples/aaws.rb +32 -0
- data/examples/basic.rb +6 -0
- data/examples/delicious.rb +36 -0
- data/examples/google.rb +16 -0
- data/examples/rubyurl.rb +14 -0
- data/examples/twitter.rb +31 -0
- data/examples/whoismyrep.rb +10 -0
- data/httparty.gemspec +40 -0
- data/lib/core_extensions.rb +349 -0
- data/lib/httparty.rb +120 -0
- data/lib/httparty/exceptions.rb +4 -0
- data/lib/httparty/request.rb +145 -0
- data/lib/httparty/version.rb +3 -0
- data/lib/module_level_inheritable_attributes.rb +25 -0
- data/setup.rb +1585 -0
- data/spec/fixtures/delicious.xml +23 -0
- data/spec/fixtures/google.html +3 -0
- data/spec/fixtures/twitter.json +1 -0
- data/spec/fixtures/twitter.xml +403 -0
- data/spec/httparty/request_spec.rb +202 -0
- data/spec/httparty_spec.rb +181 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +24 -0
- data/website/css/common.css +47 -0
- data/website/index.html +74 -0
- metadata +113 -0
data/History
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
== 0.2.6 2009-01-05
|
2
|
+
* 1 minor bug fix
|
3
|
+
* added explicit require of time as Time#parse failed outside of rails (willcodeforfoo)
|
4
|
+
|
5
|
+
== 0.2.5 2009-01-05
|
6
|
+
* 1 major enhancement
|
7
|
+
* Add command line interface to HTTParty (Alex Vollmer)
|
8
|
+
|
9
|
+
== 0.2.4 2008-12-23
|
10
|
+
* 1 bug fix
|
11
|
+
* Fixed that mimetype detection was failing if no mimetype was returned from service (skippy)
|
12
|
+
== 0.2.3 2008-12-23
|
13
|
+
* 1 bug fix
|
14
|
+
* Fixed typecasting class variable naming issue
|
15
|
+
|
16
|
+
== 0.2.2 2008-12-08
|
17
|
+
* 1 bug fix
|
18
|
+
* Added the missing core extension hash method to_xml_attributes
|
19
|
+
|
20
|
+
== 0.2.1 2008-12-08
|
21
|
+
* 1 bug fix
|
22
|
+
* Fixed that HTTParty was borking ActiveSupport and as such Rails (thanks to Rob Sanheim)
|
23
|
+
|
24
|
+
== 0.2.0 2008-12-07
|
25
|
+
* 1 major enhancement
|
26
|
+
* Removed ActiveSupport as a dependency. Now requires json gem for json deserialization and uses an included class to do the xml parsing.
|
27
|
+
|
28
|
+
== 0.1.8 2008-11-30
|
29
|
+
* 3 major enhancements
|
30
|
+
* Moved base_uri normalization into request class and out of httparty module, fixing
|
31
|
+
the problem where base_uri was not always being normalized.
|
32
|
+
* Stupid simple support for HTTParty.get/post/put/delete. (jqr)
|
33
|
+
* Switched gem management to Echoe from newgem.
|
34
|
+
|
35
|
+
== 0.1.7 2008-11-30
|
36
|
+
* 1 major enhancement
|
37
|
+
* fixed multiple class definitions overriding each others options
|
38
|
+
|
39
|
+
== 0.1.6 2008-11-26
|
40
|
+
* 1 major enhancement
|
41
|
+
* now passing :query to set_form_data if post request to avoid content length errors
|
42
|
+
|
43
|
+
== 0.1.5 2008-11-14
|
44
|
+
* 2 major enhancements
|
45
|
+
* Refactored send request method out into its own object.
|
46
|
+
* Added :html format if you just want to do that.
|
47
|
+
|
48
|
+
== 0.1.4 2008-11-08
|
49
|
+
* 3 major enhancements:
|
50
|
+
* Removed some cruft
|
51
|
+
* Added ability to follow redirects automatically and turn that off (Alex Vollmer)
|
52
|
+
|
53
|
+
== 0.1.3 2008-08-22
|
54
|
+
|
55
|
+
* 3 major enhancements:
|
56
|
+
* Added http_proxy key for setting proxy server and port (francxk@gmail.com)
|
57
|
+
* Now raises exception when http error occurs (francxk@gmail.com)
|
58
|
+
* Changed auto format detection from file extension to response content type (Jay Pignata)
|
59
|
+
|
60
|
+
== 0.1.2 2008-08-09
|
61
|
+
|
62
|
+
* 1 major enhancement:
|
63
|
+
* default_params were not being appended to query string if option[:query] was blank
|
64
|
+
|
65
|
+
== 0.1.1 2008-07-30
|
66
|
+
|
67
|
+
* 2 major enhancement:
|
68
|
+
* Added :basic_auth key for options when making a request
|
69
|
+
* :query and :body both now work with query string or hash
|
70
|
+
|
71
|
+
== 0.1.0 2008-07-27
|
72
|
+
|
73
|
+
* 1 major enhancement:
|
74
|
+
* Initial release
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 John Nunemaker
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
bin/httparty
|
2
|
+
examples/aaws.rb
|
3
|
+
examples/basic.rb
|
4
|
+
examples/delicious.rb
|
5
|
+
examples/google.rb
|
6
|
+
examples/rubyurl.rb
|
7
|
+
examples/twitter.rb
|
8
|
+
examples/whoismyrep.rb
|
9
|
+
History
|
10
|
+
httparty.gemspec
|
11
|
+
lib/core_extensions.rb
|
12
|
+
lib/httparty/exceptions.rb
|
13
|
+
lib/httparty/request.rb
|
14
|
+
lib/httparty/version.rb
|
15
|
+
lib/httparty.rb
|
16
|
+
lib/module_level_inheritable_attributes.rb
|
17
|
+
Manifest
|
18
|
+
MIT-LICENSE
|
19
|
+
Rakefile
|
20
|
+
README
|
21
|
+
setup.rb
|
22
|
+
spec/as_buggery_spec.rb
|
23
|
+
spec/fixtures/delicious.xml
|
24
|
+
spec/fixtures/google.html
|
25
|
+
spec/fixtures/twitter.json
|
26
|
+
spec/fixtures/twitter.xml
|
27
|
+
spec/httparty/request_spec.rb
|
28
|
+
spec/httparty_spec.rb
|
29
|
+
spec/spec.opts
|
30
|
+
spec/spec_helper.rb
|
31
|
+
website/css/common.css
|
32
|
+
website/index.html
|
data/README
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
= httparty
|
2
|
+
|
3
|
+
== DESCRIPTION:
|
4
|
+
|
5
|
+
Makes http fun again!
|
6
|
+
|
7
|
+
== FEATURES:
|
8
|
+
|
9
|
+
* Easy get, post requests
|
10
|
+
* Basic http authentication
|
11
|
+
* Default request query string parameters (ie: for api keys that are needed on each request)
|
12
|
+
* Automatic parsing of JSON and XML into ruby hashes based on response content-type
|
13
|
+
|
14
|
+
== EXAMPLES:
|
15
|
+
|
16
|
+
See http://github.com/jnunemaker/httparty/tree/master/examples
|
17
|
+
|
18
|
+
|
19
|
+
== COMMAND LINE INTERFACE
|
20
|
+
|
21
|
+
httparty also includes the executable <tt>httparty</tt> which can be
|
22
|
+
used to query web services and examine the resulting output. By default
|
23
|
+
it will output the response as a pretty-printed Ruby object (useful for
|
24
|
+
grokking the structure of output). This can also be overridden to output
|
25
|
+
formatted XML or JSON. Execute <tt>httparty --help</tt> for all the
|
26
|
+
options. Below is an example of how easy it is.
|
27
|
+
|
28
|
+
httparty "http://twitter.com/statuses/public_timeline.json" -f json
|
29
|
+
|
30
|
+
== REQUIREMENTS:
|
31
|
+
|
32
|
+
* JSON ~> 1.1
|
33
|
+
|
34
|
+
== INSTALL:
|
35
|
+
|
36
|
+
* sudo gem install httparty
|
data/Rakefile
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
ProjectName = 'httparty'
|
2
|
+
WebsitePath = "jnunemaker@rubyforge.org:/var/www/gforge-projects/#{ProjectName}"
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'rake'
|
6
|
+
require 'echoe'
|
7
|
+
require 'spec/rake/spectask'
|
8
|
+
require "lib/#{ProjectName}/version"
|
9
|
+
|
10
|
+
Echoe.new(ProjectName, HTTParty::Version) do |p|
|
11
|
+
p.description = "Makes http fun! Also, makes consuming restful web services dead easy."
|
12
|
+
p.install_message = "When you HTTParty, you must party hard!"
|
13
|
+
p.url = "http://#{ProjectName}.rubyforge.org"
|
14
|
+
p.author = "John Nunemaker"
|
15
|
+
p.email = "nunemaker@gmail.com"
|
16
|
+
p.extra_deps = [['json', '~> 1.1']]
|
17
|
+
p.need_tar_gz = false
|
18
|
+
p.docs_host = WebsitePath
|
19
|
+
end
|
20
|
+
|
21
|
+
desc 'Upload website files to rubyforge'
|
22
|
+
task :website do
|
23
|
+
sh %{rsync -av website/ #{WebsitePath}}
|
24
|
+
Rake::Task['website_docs'].invoke
|
25
|
+
end
|
26
|
+
|
27
|
+
task :website_docs do
|
28
|
+
Rake::Task['redocs'].invoke
|
29
|
+
sh %{rsync -av doc/ #{WebsitePath}/docs}
|
30
|
+
end
|
31
|
+
|
32
|
+
desc 'Preps the gem for a new release'
|
33
|
+
task :prepare do
|
34
|
+
%w[manifest build_gemspec].each do |task|
|
35
|
+
Rake::Task[task].invoke
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
Rake::Task[:default].prerequisites.clear
|
40
|
+
task :default => :spec
|
41
|
+
Spec::Rake::SpecTask.new do |t|
|
42
|
+
t.spec_files = FileList["spec/**/*_spec.rb"]
|
43
|
+
end
|
data/bin/httparty
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "optparse"
|
4
|
+
require "pp"
|
5
|
+
require "rexml/document"
|
6
|
+
|
7
|
+
$:.unshift(File.join(File.dirname(__FILE__), "/../lib"))
|
8
|
+
require "httparty"
|
9
|
+
|
10
|
+
opts = {
|
11
|
+
:action => :get,
|
12
|
+
:headers => {},
|
13
|
+
:verbose => false
|
14
|
+
}
|
15
|
+
|
16
|
+
def die(msg)
|
17
|
+
STDERR.puts(msg)
|
18
|
+
exit 1
|
19
|
+
end
|
20
|
+
|
21
|
+
OptionParser.new do |o|
|
22
|
+
o.banner = "USAGE: #{$0} [options] [url]"
|
23
|
+
o.on("-f",
|
24
|
+
"--format [FORMAT]",
|
25
|
+
"Output format to use instead of pretty-print ruby: " +
|
26
|
+
"plain, json or xml") do |f|
|
27
|
+
opts[:output_format] = f.downcase.to_sym
|
28
|
+
end
|
29
|
+
o.on("-a",
|
30
|
+
"--action [ACTION]",
|
31
|
+
"HTTP action: get (default), post, put or delete") do |a|
|
32
|
+
opts[:action] = a.downcase.to_sym
|
33
|
+
end
|
34
|
+
o.on("-d",
|
35
|
+
"--data [BODY]",
|
36
|
+
"Data to put in request body (prefix with '@' for file)") do |d|
|
37
|
+
if d =~ /^@/
|
38
|
+
opts[:data] = open(d).read
|
39
|
+
else
|
40
|
+
opts[:data] = d
|
41
|
+
end
|
42
|
+
end
|
43
|
+
o.on("-H", "--header [NAME=VALUE]", "Additional HTTP headers in NAME=VALUE form") do |h|
|
44
|
+
die "Invalid header specification, should be Name:Value" unless h =~ /.+:.+/
|
45
|
+
name, value = h.split(':')
|
46
|
+
opts[:headers][name.strip] = value.strip
|
47
|
+
end
|
48
|
+
o.on("-v", "--verbose", "If set, print verbose output") do |v|
|
49
|
+
opts[:verbose] = true
|
50
|
+
end
|
51
|
+
o.on("-u", "--user [CREDS]", "Use basic authentication. Value should be user:password") do |u|
|
52
|
+
die "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
|
+
o.on("-h", "--help", "Show help documentation") do |h|
|
57
|
+
puts o
|
58
|
+
exit
|
59
|
+
end
|
60
|
+
end.parse!
|
61
|
+
|
62
|
+
puts "Querying #{ARGV.first} with options: #{opts.inspect}" if opts[:verbose]
|
63
|
+
|
64
|
+
if ARGV.empty?
|
65
|
+
STDERR.puts "You need to provide a URL"
|
66
|
+
STDERR.puts "USAGE: #{$0} [options] [url]"
|
67
|
+
end
|
68
|
+
|
69
|
+
# 1.8.6 has mistyping of transitive in if statement
|
70
|
+
module REXML
|
71
|
+
class Document < Element
|
72
|
+
def write( output=$stdout, indent=-1, transitive=false, ie_hack=false )
|
73
|
+
if xml_decl.encoding != "UTF-8" && !output.kind_of?(Output)
|
74
|
+
output = Output.new( output, xml_decl.encoding )
|
75
|
+
end
|
76
|
+
formatter = if indent > -1
|
77
|
+
if transitive
|
78
|
+
REXML::Formatters::Transitive.new( indent, ie_hack )
|
79
|
+
else
|
80
|
+
REXML::Formatters::Pretty.new( indent, ie_hack )
|
81
|
+
end
|
82
|
+
else
|
83
|
+
REXML::Formatters::Default.new( ie_hack )
|
84
|
+
end
|
85
|
+
formatter.write( self, output )
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
if opts[:output_format].nil?
|
91
|
+
response = HTTParty.send(opts[:action], ARGV.first, opts)
|
92
|
+
puts "Status: #{response.status}"
|
93
|
+
pp response
|
94
|
+
else
|
95
|
+
print_format = opts[:output_format]
|
96
|
+
response = HTTParty.send(opts[:action], ARGV.first, opts)
|
97
|
+
puts "Status: #{response.status}"
|
98
|
+
case opts[:output_format]
|
99
|
+
when :json
|
100
|
+
puts JSON.pretty_generate(response)
|
101
|
+
when :xml
|
102
|
+
REXML::Document.new(response.original_body).write(STDOUT, 2)
|
103
|
+
puts
|
104
|
+
else
|
105
|
+
puts response
|
106
|
+
end
|
107
|
+
end
|
data/examples/aaws.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'activesupport'
|
3
|
+
|
4
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
5
|
+
require File.join(dir, 'httparty')
|
6
|
+
require 'pp'
|
7
|
+
config = YAML::load(File.read(File.join(ENV['HOME'], '.aaws')))
|
8
|
+
|
9
|
+
module AAWS
|
10
|
+
class Book
|
11
|
+
include HTTParty
|
12
|
+
base_uri 'http://ecs.amazonaws.com'
|
13
|
+
default_params :Service => 'AWSECommerceService', :Operation => 'ItemSearch', :SearchIndex => 'Books'
|
14
|
+
|
15
|
+
def initialize(key)
|
16
|
+
self.class.default_params :AWSAccessKeyId => key
|
17
|
+
end
|
18
|
+
|
19
|
+
def search(options={})
|
20
|
+
raise ArgumentError, 'You must search for something' if options[:query].blank?
|
21
|
+
|
22
|
+
# amazon uses nasty camelized query params
|
23
|
+
options[:query] = options[:query].inject({}) { |h, q| h[q[0].to_s.camelize] = q[1]; h }
|
24
|
+
|
25
|
+
# make a request and return the items (NOTE: this doesn't handle errors at this point)
|
26
|
+
self.class.get('/onca/xml', options)['ItemSearchResponse']['Items']
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
aaws = AAWS::Book.new(config[:access_key])
|
32
|
+
pp aaws.search(:query => {:title => 'Ruby On Rails'})
|
data/examples/basic.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require File.join(dir, 'httparty')
|
3
|
+
require 'pp'
|
4
|
+
config = YAML::load(File.read(File.join(ENV['HOME'], '.delicious')))
|
5
|
+
|
6
|
+
class Delicious
|
7
|
+
include HTTParty
|
8
|
+
base_uri 'https://api.del.icio.us/v1'
|
9
|
+
|
10
|
+
def initialize(u, p)
|
11
|
+
@auth = {:username => u, :password => p}
|
12
|
+
end
|
13
|
+
|
14
|
+
# query params that filter the posts are:
|
15
|
+
# tag (optional). Filter by this tag.
|
16
|
+
# dt (optional). Filter by this date (CCYY-MM-DDThh:mm:ssZ).
|
17
|
+
# url (optional). Filter by this url.
|
18
|
+
# ie: posts(:query => {:tag => 'ruby'})
|
19
|
+
def posts(options={})
|
20
|
+
options.merge!({:basic_auth => @auth})
|
21
|
+
self.class.get('/posts/get', options)
|
22
|
+
end
|
23
|
+
|
24
|
+
# query params that filter the posts are:
|
25
|
+
# tag (optional). Filter by this tag.
|
26
|
+
# count (optional). Number of items to retrieve (Default:15, Maximum:100).
|
27
|
+
def recent(options={})
|
28
|
+
options.merge!({:basic_auth => @auth})
|
29
|
+
self.class.get('/posts/recent', options)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
delicious = Delicious.new(config['username'], config['password'])
|
34
|
+
pp delicious.posts(:query => {:tag => 'ruby'})
|
35
|
+
pp delicious.recent
|
36
|
+
|
data/examples/google.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require File.join(dir, 'httparty')
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
class Google
|
6
|
+
include HTTParty
|
7
|
+
format :html
|
8
|
+
end
|
9
|
+
|
10
|
+
# google.com redirects to www.google.com so this is live test for redirection
|
11
|
+
pp Google.get('http://google.com')
|
12
|
+
|
13
|
+
puts '', '*'*70, ''
|
14
|
+
|
15
|
+
# check that ssl is requesting right
|
16
|
+
pp Google.get('https://www.google.com')
|
data/examples/rubyurl.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require File.join(dir, 'httparty')
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
class Rubyurl
|
6
|
+
include HTTParty
|
7
|
+
base_uri 'rubyurl.com'
|
8
|
+
|
9
|
+
def self.shorten( website_url )
|
10
|
+
post( '/api/links.json', :query => { :link => { :website_url => website_url } } )
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
pp Rubyurl.shorten( 'http://istwitterdown.com/' )
|
data/examples/twitter.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require File.join(dir, 'httparty')
|
3
|
+
require 'pp'
|
4
|
+
config = YAML::load(File.read(File.join(ENV['HOME'], '.twitter')))
|
5
|
+
|
6
|
+
class Twitter
|
7
|
+
include HTTParty
|
8
|
+
base_uri 'twitter.com'
|
9
|
+
|
10
|
+
def initialize(u, p)
|
11
|
+
@auth = {:username => u, :password => p}
|
12
|
+
end
|
13
|
+
|
14
|
+
# which can be :friends, :user or :public
|
15
|
+
# options[:query] can be things like since, since_id, count, etc.
|
16
|
+
def timeline(which=:friends, options={})
|
17
|
+
options.merge!({:basic_auth => @auth})
|
18
|
+
self.class.get("/statuses/#{which}_timeline.json", options)
|
19
|
+
end
|
20
|
+
|
21
|
+
def post(text)
|
22
|
+
options = { :query => {:status => text}, :basic_auth => @auth }
|
23
|
+
self.class.post('/statuses/update.json', options)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
twitter = Twitter.new(config['email'], config['password'])
|
28
|
+
pp twitter.timeline
|
29
|
+
# pp twitter.timeline(:friends, :query => {:since_id => 868482746})
|
30
|
+
# pp twitter.timeline(:friends, :query => 'since_id=868482746')
|
31
|
+
# pp twitter.post('this is a test of 0.2.0')
|