voxdolo-httparty 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History +108 -0
- data/MIT-LICENSE +20 -0
- data/Manifest +55 -0
- data/README +35 -0
- data/Rakefile +47 -0
- data/bin/httparty +98 -0
- data/cucumber.yml +1 -0
- data/examples/aaws.rb +32 -0
- data/examples/basic.rb +11 -0
- data/examples/delicious.rb +37 -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/features/basic_authentication.feature +20 -0
- data/features/command_line.feature +7 -0
- data/features/deals_with_http_error_codes.feature +26 -0
- data/features/handles_multiple_formats.feature +34 -0
- data/features/steps/env.rb +15 -0
- data/features/steps/httparty_response_steps.rb +26 -0
- data/features/steps/httparty_steps.rb +15 -0
- data/features/steps/mongrel_helper.rb +55 -0
- data/features/steps/remote_service_steps.rb +47 -0
- data/features/supports_redirection.feature +22 -0
- data/httparty.gemspec +37 -0
- data/lib/core_extensions.rb +175 -0
- data/lib/httparty/cookie_hash.rb +9 -0
- data/lib/httparty/exceptions.rb +7 -0
- data/lib/httparty/module_inheritable_attributes.rb +25 -0
- data/lib/httparty/parsers/json.rb +74 -0
- data/lib/httparty/parsers/xml.rb +209 -0
- data/lib/httparty/parsers.rb +4 -0
- data/lib/httparty/request.rb +139 -0
- data/lib/httparty/response.rb +17 -0
- data/lib/httparty/version.rb +3 -0
- data/lib/httparty.rb +201 -0
- data/setup.rb +1585 -0
- data/spec/fixtures/delicious.xml +23 -0
- data/spec/fixtures/empty.xml +0 -0
- data/spec/fixtures/google.html +3 -0
- data/spec/fixtures/twitter.json +1 -0
- data/spec/fixtures/twitter.xml +403 -0
- data/spec/fixtures/undefined_method_add_node_for_nil.xml +2 -0
- data/spec/hash_spec.rb +49 -0
- data/spec/httparty/cookie_hash_spec.rb +38 -0
- data/spec/httparty/parsers/json_spec.rb +42 -0
- data/spec/httparty/parsers/xml_spec.rb +445 -0
- data/spec/httparty/request_spec.rb +205 -0
- data/spec/httparty/response_spec.rb +53 -0
- data/spec/httparty_spec.rb +259 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +21 -0
- data/spec/string_spec.rb +27 -0
- data/website/css/common.css +47 -0
- data/website/index.html +74 -0
- metadata +133 -0
data/History
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
== 0.3.1 2009-02-10
|
2
|
+
* 1 minor fix, 1 minor enhancement
|
3
|
+
* Fixed unescaping umlauts (siebertm)
|
4
|
+
* Added yaml response parsing (Miha Filej)
|
5
|
+
|
6
|
+
== 0.3.0 2009-01-31
|
7
|
+
* 1 major enhancement, 1 bug fix
|
8
|
+
* JSON gem no longer a requirement. It was conflicting with rails json stuff so I just stole ActiveSupport's json decoding and bundled it with HTTParty.
|
9
|
+
* Fixed bug where query strings were being duplicated on redirects
|
10
|
+
* Added a bunch of specs and moved some code around.
|
11
|
+
|
12
|
+
== 0.2.10 2009-01-29
|
13
|
+
* 1 minor enhancement
|
14
|
+
* Made encoding on query parameters treat everything except URI::PATTERN::UNRESERVED as UNSAFE to force encoding of '+' character (Julian Russell)
|
15
|
+
|
16
|
+
== 0.2.9 2009-01-29
|
17
|
+
* 3 minor enhancements
|
18
|
+
* Added a 'headers' accessor to the response with a hash of any HTTP headers. (Don Peterson)
|
19
|
+
* Add support for a ":cookies" option to be used at the class level, or as an option on any individual call. It should be passed a hash, which will be converted to the proper format and added to the request headers when the call is made. (Don Peterson)
|
20
|
+
* Refactored several specs and added a full suite of cucumber features (Don Peterson)
|
21
|
+
|
22
|
+
== 0.2.8 2009-01-28
|
23
|
+
* 1 major fix
|
24
|
+
* fixed major bug with response where it wouldn't iterate or really work at all with parsed responses
|
25
|
+
|
26
|
+
== 0.2.7 2009-01-28
|
27
|
+
* 2 minor fixes, 2 minor enhancements, 2 major enhancements
|
28
|
+
* fixed undefined method add_node for nil class error that occasionally happened (juliocesar)
|
29
|
+
* Handle nil or unexpected values better when typecasting. (Brian Landau)
|
30
|
+
* More robust handling of mime types (Alex Vollmer)
|
31
|
+
* Fixed support for specifying headers and added support for basic auth to CLI. (Alex Vollmer)
|
32
|
+
* Added first class response object that includes original body and status code (Alex Vollmer)
|
33
|
+
* Now parsing all response types as some non-200 responses provide important information, this means no more exception raising (Alex Vollmer)
|
34
|
+
|
35
|
+
== 0.2.6 2009-01-05
|
36
|
+
* 1 minor bug fix
|
37
|
+
* added explicit require of time as Time#parse failed outside of rails (willcodeforfoo)
|
38
|
+
|
39
|
+
== 0.2.5 2009-01-05
|
40
|
+
* 1 major enhancement
|
41
|
+
* Add command line interface to HTTParty (Alex Vollmer)
|
42
|
+
|
43
|
+
== 0.2.4 2008-12-23
|
44
|
+
* 1 bug fix
|
45
|
+
* Fixed that mimetype detection was failing if no mimetype was returned from service (skippy)
|
46
|
+
== 0.2.3 2008-12-23
|
47
|
+
* 1 bug fix
|
48
|
+
* Fixed typecasting class variable naming issue
|
49
|
+
|
50
|
+
== 0.2.2 2008-12-08
|
51
|
+
* 1 bug fix
|
52
|
+
* Added the missing core extension hash method to_xml_attributes
|
53
|
+
|
54
|
+
== 0.2.1 2008-12-08
|
55
|
+
* 1 bug fix
|
56
|
+
* Fixed that HTTParty was borking ActiveSupport and as such Rails (thanks to Rob Sanheim)
|
57
|
+
|
58
|
+
== 0.2.0 2008-12-07
|
59
|
+
* 1 major enhancement
|
60
|
+
* Removed ActiveSupport as a dependency. Now requires json gem for json deserialization and uses an included class to do the xml parsing.
|
61
|
+
|
62
|
+
== 0.1.8 2008-11-30
|
63
|
+
* 3 major enhancements
|
64
|
+
* Moved base_uri normalization into request class and out of httparty module, fixing
|
65
|
+
the problem where base_uri was not always being normalized.
|
66
|
+
* Stupid simple support for HTTParty.get/post/put/delete. (jqr)
|
67
|
+
* Switched gem management to Echoe from newgem.
|
68
|
+
|
69
|
+
== 0.1.7 2008-11-30
|
70
|
+
* 1 major enhancement
|
71
|
+
* fixed multiple class definitions overriding each others options
|
72
|
+
|
73
|
+
== 0.1.6 2008-11-26
|
74
|
+
* 1 major enhancement
|
75
|
+
* now passing :query to set_form_data if post request to avoid content length errors
|
76
|
+
|
77
|
+
== 0.1.5 2008-11-14
|
78
|
+
* 2 major enhancements
|
79
|
+
* Refactored send request method out into its own object.
|
80
|
+
* Added :html format if you just want to do that.
|
81
|
+
|
82
|
+
== 0.1.4 2008-11-08
|
83
|
+
* 3 major enhancements:
|
84
|
+
* Removed some cruft
|
85
|
+
* Added ability to follow redirects automatically and turn that off (Alex Vollmer)
|
86
|
+
|
87
|
+
== 0.1.3 2008-08-22
|
88
|
+
|
89
|
+
* 3 major enhancements:
|
90
|
+
* Added http_proxy key for setting proxy server and port (francxk@gmail.com)
|
91
|
+
* Now raises exception when http error occurs (francxk@gmail.com)
|
92
|
+
* Changed auto format detection from file extension to response content type (Jay Pignata)
|
93
|
+
|
94
|
+
== 0.1.2 2008-08-09
|
95
|
+
|
96
|
+
* 1 major enhancement:
|
97
|
+
* default_params were not being appended to query string if option[:query] was blank
|
98
|
+
|
99
|
+
== 0.1.1 2008-07-30
|
100
|
+
|
101
|
+
* 2 major enhancement:
|
102
|
+
* Added :basic_auth key for options when making a request
|
103
|
+
* :query and :body both now work with query string or hash
|
104
|
+
|
105
|
+
== 0.1.0 2008-07-27
|
106
|
+
|
107
|
+
* 1 major enhancement:
|
108
|
+
* 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,55 @@
|
|
1
|
+
bin/httparty
|
2
|
+
cucumber.yml
|
3
|
+
examples/aaws.rb
|
4
|
+
examples/basic.rb
|
5
|
+
examples/delicious.rb
|
6
|
+
examples/google.rb
|
7
|
+
examples/rubyurl.rb
|
8
|
+
examples/twitter.rb
|
9
|
+
examples/whoismyrep.rb
|
10
|
+
features/basic_authentication.feature
|
11
|
+
features/command_line.feature
|
12
|
+
features/deals_with_http_error_codes.feature
|
13
|
+
features/handles_multiple_formats.feature
|
14
|
+
features/steps/env.rb
|
15
|
+
features/steps/httparty_response_steps.rb
|
16
|
+
features/steps/httparty_steps.rb
|
17
|
+
features/steps/mongrel_helper.rb
|
18
|
+
features/steps/remote_service_steps.rb
|
19
|
+
features/supports_redirection.feature
|
20
|
+
History
|
21
|
+
httparty.gemspec
|
22
|
+
lib/core_extensions.rb
|
23
|
+
lib/httparty/cookie_hash.rb
|
24
|
+
lib/httparty/exceptions.rb
|
25
|
+
lib/httparty/module_inheritable_attributes.rb
|
26
|
+
lib/httparty/parsers/json.rb
|
27
|
+
lib/httparty/parsers/xml.rb
|
28
|
+
lib/httparty/parsers.rb
|
29
|
+
lib/httparty/request.rb
|
30
|
+
lib/httparty/response.rb
|
31
|
+
lib/httparty/version.rb
|
32
|
+
lib/httparty.rb
|
33
|
+
Manifest
|
34
|
+
MIT-LICENSE
|
35
|
+
Rakefile
|
36
|
+
README
|
37
|
+
setup.rb
|
38
|
+
spec/fixtures/delicious.xml
|
39
|
+
spec/fixtures/empty.xml
|
40
|
+
spec/fixtures/google.html
|
41
|
+
spec/fixtures/twitter.json
|
42
|
+
spec/fixtures/twitter.xml
|
43
|
+
spec/fixtures/undefined_method_add_node_for_nil.xml
|
44
|
+
spec/hash_spec.rb
|
45
|
+
spec/httparty/cookie_hash_spec.rb
|
46
|
+
spec/httparty/parsers/json_spec.rb
|
47
|
+
spec/httparty/parsers/xml_spec.rb
|
48
|
+
spec/httparty/request_spec.rb
|
49
|
+
spec/httparty/response_spec.rb
|
50
|
+
spec/httparty_spec.rb
|
51
|
+
spec/spec.opts
|
52
|
+
spec/spec_helper.rb
|
53
|
+
spec/string_spec.rb
|
54
|
+
website/css/common.css
|
55
|
+
website/index.html
|
data/README
ADDED
@@ -0,0 +1,35 @@
|
|
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
|
+
== COMMAND LINE INTERFACE
|
19
|
+
|
20
|
+
httparty also includes the executable <tt>httparty</tt> which can be
|
21
|
+
used to query web services and examine the resulting output. By default
|
22
|
+
it will output the response as a pretty-printed Ruby object (useful for
|
23
|
+
grokking the structure of output). This can also be overridden to output
|
24
|
+
formatted XML or JSON. Execute <tt>httparty --help</tt> for all the
|
25
|
+
options. Below is an example of how easy it is.
|
26
|
+
|
27
|
+
httparty "http://twitter.com/statuses/public_timeline.json"
|
28
|
+
|
29
|
+
== REQUIREMENTS:
|
30
|
+
|
31
|
+
* You like to party!
|
32
|
+
|
33
|
+
== INSTALL:
|
34
|
+
|
35
|
+
* sudo gem install httparty
|
data/Rakefile
ADDED
@@ -0,0 +1,47 @@
|
|
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
|
+
require 'cucumber/rake/task'
|
10
|
+
|
11
|
+
Echoe.new(ProjectName, HTTParty::Version) do |p|
|
12
|
+
p.description = "Makes http fun! Also, makes consuming restful web services dead easy."
|
13
|
+
p.install_message = "When you HTTParty, you must party hard!"
|
14
|
+
p.url = "http://#{ProjectName}.rubyforge.org"
|
15
|
+
p.author = "John Nunemaker"
|
16
|
+
p.email = "nunemaker@gmail.com"
|
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
|
44
|
+
|
45
|
+
Cucumber::Rake::Task.new(:features) do |t|
|
46
|
+
t.cucumber_opts = "--format pretty"
|
47
|
+
end
|
data/bin/httparty
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "optparse"
|
4
|
+
require "pp"
|
5
|
+
|
6
|
+
$:.unshift(File.join(File.dirname(__FILE__), "/../lib"))
|
7
|
+
require "httparty"
|
8
|
+
|
9
|
+
opts = {
|
10
|
+
:action => :get,
|
11
|
+
:headers => {},
|
12
|
+
:verbose => false
|
13
|
+
}
|
14
|
+
|
15
|
+
def die(msg)
|
16
|
+
STDERR.puts(msg)
|
17
|
+
exit 1
|
18
|
+
end
|
19
|
+
|
20
|
+
OptionParser.new do |o|
|
21
|
+
o.banner = "USAGE: #{$0} [options] [url]"
|
22
|
+
|
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
|
+
|
30
|
+
o.on("-a",
|
31
|
+
"--action [ACTION]",
|
32
|
+
"HTTP action: get (default), post, put or delete") do |a|
|
33
|
+
opts[:action] = a.downcase.to_sym
|
34
|
+
end
|
35
|
+
|
36
|
+
o.on("-d",
|
37
|
+
"--data [BODY]",
|
38
|
+
"Data to put in request body (prefix with '@' for file)") do |d|
|
39
|
+
if d =~ /^@/
|
40
|
+
opts[:data] = open(d).read
|
41
|
+
else
|
42
|
+
opts[:data] = d
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
o.on("-H", "--header [NAME=VALUE]", "Additional HTTP headers in NAME=VALUE form") do |h|
|
47
|
+
die "Invalid header specification, should be Name:Value" unless h =~ /.+:.+/
|
48
|
+
name, value = h.split(':')
|
49
|
+
opts[:headers][name.strip] = value.strip
|
50
|
+
end
|
51
|
+
|
52
|
+
o.on("-v", "--verbose", "If set, print verbose output") do |v|
|
53
|
+
opts[:verbose] = true
|
54
|
+
end
|
55
|
+
|
56
|
+
o.on("-u", "--user [CREDS]", "Use basic authentication. Value should be user:password") do |u|
|
57
|
+
die "Invalid credentials format. Must be user:password" unless u =~ /.+:.+/
|
58
|
+
user, password = u.split(':')
|
59
|
+
opts[:basic_auth] = { :username => user, :password => password }
|
60
|
+
end
|
61
|
+
|
62
|
+
o.on("-h", "--help", "Show help documentation") do |h|
|
63
|
+
puts o
|
64
|
+
exit
|
65
|
+
end
|
66
|
+
end.parse!
|
67
|
+
|
68
|
+
puts "Querying #{ARGV.first} with options: #{opts.inspect}" if opts[:verbose]
|
69
|
+
|
70
|
+
if ARGV.empty?
|
71
|
+
STDERR.puts "You need to provide a URL"
|
72
|
+
STDERR.puts "USAGE: #{$0} [options] [url]"
|
73
|
+
end
|
74
|
+
|
75
|
+
if opts[:output_format].nil?
|
76
|
+
response = HTTParty.send(opts[:action], ARGV.first, opts)
|
77
|
+
puts "Status: #{response.code}"
|
78
|
+
pp response
|
79
|
+
else
|
80
|
+
print_format = opts[:output_format]
|
81
|
+
response = HTTParty.send(opts[:action], ARGV.first, opts)
|
82
|
+
puts "Status: #{response.code}"
|
83
|
+
case opts[:output_format]
|
84
|
+
when :json
|
85
|
+
begin
|
86
|
+
require 'rubygems'
|
87
|
+
require 'json'
|
88
|
+
puts JSON.pretty_generate(response.delegate)
|
89
|
+
rescue LoadError
|
90
|
+
puts YAML.dump(response.delegate)
|
91
|
+
end
|
92
|
+
when :xml
|
93
|
+
REXML::Document.new(response.body).write(STDOUT, 2)
|
94
|
+
puts
|
95
|
+
else
|
96
|
+
puts response
|
97
|
+
end
|
98
|
+
end
|
data/cucumber.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
default: features
|
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,11 @@
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require File.join(dir, 'httparty')
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
# You can also use post, put, delete in the same fashion
|
6
|
+
response = HTTParty.get('http://twitter.com/statuses/public_timeline.json')
|
7
|
+
puts response.body, response.code, response.headers.inspect
|
8
|
+
|
9
|
+
response.each do |item|
|
10
|
+
puts item['user']['screen_name']
|
11
|
+
end
|
@@ -0,0 +1,37 @@
|
|
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
|
+
|
37
|
+
delicious.recent['posts']['post'].each { |post| puts post['href'] }
|
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')
|
@@ -0,0 +1,10 @@
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require File.join(dir, 'httparty')
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
class Rep
|
6
|
+
include HTTParty
|
7
|
+
end
|
8
|
+
|
9
|
+
pp Rep.get('http://whoismyrepresentative.com/whoismyrep.php?zip=46544')
|
10
|
+
pp Rep.get('http://whoismyrepresentative.com/whoismyrep.php', :query => {:zip => 46544})
|
@@ -0,0 +1,20 @@
|
|
1
|
+
Feature: Basic Authentication
|
2
|
+
|
3
|
+
As a developer
|
4
|
+
I want to be able to use a service that requires Basic Authentication
|
5
|
+
Because that is not an uncommon requirement
|
6
|
+
|
7
|
+
Scenario: Passing no credentials to a page requiring Basic Authentication
|
8
|
+
Given a restricted page at '/protected.html'
|
9
|
+
When I call HTTParty#get with '/protected.html'
|
10
|
+
Then it should return a response with a 401 response code
|
11
|
+
|
12
|
+
Scenario: Passing proper credentials to a page requiring Basic Authentication
|
13
|
+
Given a remote service that returns 'Authenticated Page'
|
14
|
+
And that service is accessed at the path '/protected.html'
|
15
|
+
And that service is protected by Basic Authentication
|
16
|
+
And that service requires the username 'jcash' with the password 'maninblack'
|
17
|
+
When I call HTTParty#get with '/protected.html' and a basic_auth hash:
|
18
|
+
| username | password |
|
19
|
+
| jcash | maninblack |
|
20
|
+
Then the return value should match 'Authenticated Page'
|
@@ -0,0 +1,26 @@
|
|
1
|
+
Feature: Deals with HTTP error codes
|
2
|
+
|
3
|
+
As a developer
|
4
|
+
I want to be informed of non-successful responses
|
5
|
+
Because sometimes thing explode
|
6
|
+
And I should probably know what happened
|
7
|
+
|
8
|
+
Scenario: A response of '404 - Not Found'
|
9
|
+
Given a remote service that returns a 404 status code
|
10
|
+
And that service is accessed at the path '/service.html'
|
11
|
+
When I call HTTParty#get with '/service.html'
|
12
|
+
Then it should return a response with a 404 response code
|
13
|
+
|
14
|
+
Scenario: A response of '500 - Internal Server Error'
|
15
|
+
Given a remote service that returns a 500 status code
|
16
|
+
And that service is accessed at the path '/service.html'
|
17
|
+
When I call HTTParty#get with '/service.html'
|
18
|
+
Then it should return a response with a 500 response code
|
19
|
+
|
20
|
+
Scenario: A non-successful response where I need the body
|
21
|
+
Given a remote service that returns a 400 status code
|
22
|
+
And the response from the service has a body of 'Bad response'
|
23
|
+
And that service is accessed at the path '/service.html'
|
24
|
+
When I call HTTParty#get with '/service.html'
|
25
|
+
Then it should return a response with a 400 response code
|
26
|
+
And the return value should match 'Bad response'
|
@@ -0,0 +1,34 @@
|
|
1
|
+
Feature: Handles Multiple Formats
|
2
|
+
|
3
|
+
As a developer
|
4
|
+
I want to be able to consume remote services of many different formats
|
5
|
+
And I want those formats to be automatically detected and handled
|
6
|
+
Because web services take many forms
|
7
|
+
And I don't want to have to do any extra work
|
8
|
+
|
9
|
+
Scenario: An HTML service
|
10
|
+
Given a remote service that returns '<h1>Some HTML</h1>'
|
11
|
+
And that service is accessed at the path '/service.html'
|
12
|
+
And the response from the service has a Content-Type of 'text/html'
|
13
|
+
When I call HTTParty#get with '/service.html'
|
14
|
+
Then it should return a String
|
15
|
+
And the return value should match '<h1>Some HTML</h1>'
|
16
|
+
|
17
|
+
Scenario: A JSON service
|
18
|
+
Given a remote service that returns '{ "jennings": "waylon", "cash": "johnny" }'
|
19
|
+
And that service is accessed at the path '/service.json'
|
20
|
+
And the response from the service has a Content-Type of 'application/json'
|
21
|
+
When I call HTTParty#get with '/service.json'
|
22
|
+
Then it should return a Hash equaling:
|
23
|
+
| key | value |
|
24
|
+
| jennings | waylon |
|
25
|
+
| cash | johnny |
|
26
|
+
|
27
|
+
Scenario: An XML Service
|
28
|
+
Given a remote service that returns '<singer>waylon jennings</singer>'
|
29
|
+
And that service is accessed at the path '/service.xml'
|
30
|
+
And the response from the service has a Content-Type of 'text/xml'
|
31
|
+
When I call HTTParty#get with '/service.xml'
|
32
|
+
Then it should return a Hash equaling:
|
33
|
+
| key | value |
|
34
|
+
| singer | waylon jennings |
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'mongrel'
|
2
|
+
require 'activesupport'
|
3
|
+
require 'lib/httparty'
|
4
|
+
require 'spec/expectations'
|
5
|
+
|
6
|
+
Before do
|
7
|
+
port = ENV["HTTPARTY_PORT"] || 31981
|
8
|
+
@host_and_port = "0.0.0.0:#{port}"
|
9
|
+
@server = Mongrel::HttpServer.new("0.0.0.0", port)
|
10
|
+
@server.run
|
11
|
+
end
|
12
|
+
|
13
|
+
After do
|
14
|
+
@server.stop
|
15
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
Then /it should return an? (\w+)$/ do |class_string|
|
2
|
+
@response_from_httparty.should be_an_instance_of(class_string.constantize)
|
3
|
+
end
|
4
|
+
|
5
|
+
Then /the return value should match '(.*)'/ do |expected_text|
|
6
|
+
@response_from_httparty.should eql(expected_text)
|
7
|
+
end
|
8
|
+
|
9
|
+
Then /it should return a Hash equaling:/ do |hash_table|
|
10
|
+
@response_from_httparty.should be_an_instance_of(Hash)
|
11
|
+
@response_from_httparty.keys.length.should eql(hash_table.rows.length)
|
12
|
+
hash_table.hashes.each do |pair|
|
13
|
+
key, value = pair["key"], pair["value"]
|
14
|
+
@response_from_httparty.keys.should include(key)
|
15
|
+
@response_from_httparty[key].should eql(value)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Then /it should return a response with a (\d+) response code/ do |code|
|
20
|
+
@response_from_httparty.code.should eql(code)
|
21
|
+
end
|
22
|
+
|
23
|
+
Then /it should raise an HTTParty::RedirectionTooDeep exception/ do
|
24
|
+
@exception_from_httparty.should_not be_nil
|
25
|
+
@exception_from_httparty.class.should eql(HTTParty::RedirectionTooDeep)
|
26
|
+
end
|