alexvollmer-httparty 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,142 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ describe HTTParty::Request do
4
+ before do
5
+ @request = HTTParty::Request.new(Net::HTTP::Get, 'http://api.foo.com/v1', :format => :xml)
6
+ end
7
+
8
+ describe 'http' do
9
+ it "should use ssl for port 443" do
10
+ @request.send(:http, URI.parse('https://api.foo.com/v1:443')).use_ssl?.should == true
11
+ end
12
+
13
+ it 'should not use ssl for port 80' do
14
+ @request.send(:http, URI.parse('http://foobar.com')).use_ssl?.should == false
15
+ end
16
+ end
17
+
18
+ describe 'parsing responses' do
19
+ it 'should handle xml automatically' do
20
+ xml = %q[<books><book><id>1234</id><name>Foo Bar!</name></book></books>]
21
+ @request.options[:format] = :xml
22
+ @request.send(:parse_response, xml).should == {'books' => {'book' => {'id' => '1234', 'name' => 'Foo Bar!'}}}
23
+ end
24
+
25
+ it 'should handle json automatically' do
26
+ json = %q[{"books": {"book": {"name": "Foo Bar!", "id": "1234"}}}]
27
+ @request.options[:format] = :json
28
+ @request.send(:parse_response, json).should == {'books' => {'book' => {'id' => '1234', 'name' => 'Foo Bar!'}}}
29
+ end
30
+ end
31
+
32
+ it "should not attempt to parse empty responses" do
33
+ http = Net::HTTP.new('localhost', 80)
34
+ @request.stub!(:http).and_return(http)
35
+ response = Net::HTTPNoContent.new("1.1", 204, "No content for you")
36
+ response.stub!(:body).and_return(nil)
37
+ http.stub!(:request).and_return(response)
38
+
39
+ @request.options[:format] = :xml
40
+ @request.perform.should be_nil
41
+
42
+ response.stub!(:body).and_return("")
43
+ @request.perform.should be_nil
44
+ end
45
+
46
+ describe "that respond with redirects" do
47
+ def setup_redirect
48
+ @http = Net::HTTP.new('localhost', 80)
49
+ @request.stub!(:http).and_return(@http)
50
+ @request.stub!(:uri).and_return(URI.parse("http://foo.com/foobar"))
51
+ @redirect = Net::HTTPFound.new("1.1", 302, "")
52
+ @redirect['location'] = '/foo'
53
+ end
54
+
55
+ def setup_ok_response
56
+ @ok = Net::HTTPOK.new("1.1", 200, "Content for you")
57
+ @ok.stub!(:body).and_return({"foo" => "bar"}.to_xml)
58
+ @http.should_receive(:request).and_return(@redirect, @ok)
59
+ @request.options[:format] = :xml
60
+ end
61
+
62
+ def setup_redirect_response
63
+ @http.stub!(:request).and_return(@redirect)
64
+ end
65
+
66
+ def setup_successful_redirect
67
+ setup_redirect
68
+ setup_ok_response
69
+ end
70
+
71
+ def setup_infinite_redirect
72
+ setup_redirect
73
+ setup_redirect_response
74
+ end
75
+
76
+ it "should handle redirects for GET transparently" do
77
+ setup_successful_redirect
78
+ @request.perform.should == {"hash" => {"foo" => "bar"}}
79
+ end
80
+
81
+ it "should handle redirects for POST transparently" do
82
+ setup_successful_redirect
83
+ @request.http_method = Net::HTTP::Post
84
+ @request.perform.should == {"hash" => {"foo" => "bar"}}
85
+ end
86
+
87
+ it "should handle redirects for DELETE transparently" do
88
+ setup_successful_redirect
89
+ @request.http_method = Net::HTTP::Delete
90
+ @request.perform.should == {"hash" => {"foo" => "bar"}}
91
+ end
92
+
93
+ it "should handle redirects for PUT transparently" do
94
+ setup_successful_redirect
95
+ @request.http_method = Net::HTTP::Put
96
+ @request.perform.should == {"hash" => {"foo" => "bar"}}
97
+ end
98
+
99
+ it "should prevent infinite loops" do
100
+ setup_infinite_redirect
101
+
102
+ lambda do
103
+ @request.perform
104
+ end.should raise_error(HTTParty::RedirectionTooDeep)
105
+ end
106
+
107
+ describe "with explicit override of automatic redirect handling" do
108
+ before(:each) do
109
+ @request.options = { :no_follow => true }
110
+ setup_redirect_response
111
+ end
112
+
113
+ it "should fail with redirected GET" do
114
+ lambda do
115
+ @request.http_method = Net::HTTP::Get
116
+ @request.perform
117
+ end.should raise_error(HTTParty::RedirectionTooDeep)
118
+ end
119
+
120
+ it "should fail with redirected POST" do
121
+ lambda do
122
+ @request.http_method = Net::HTTP::Post
123
+ @request.perform
124
+ end.should raise_error(HTTParty::RedirectionTooDeep)
125
+ end
126
+
127
+ it "should fail with redirected DELETE" do
128
+ lambda do
129
+ @request.http_method = Net::HTTP::Delete
130
+ @request.perform
131
+ end
132
+ end
133
+
134
+ it "should fail with redirected PUT" do
135
+ lambda do
136
+ @request.http_method = Net::HTTP::Put
137
+ @request.perform
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,149 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ class Foo
4
+ include HTTParty
5
+ base_uri 'api.foo.com/v1'
6
+ end
7
+
8
+ class FooWithHttps
9
+ include HTTParty
10
+ base_uri 'api.foo.com/v1:443'
11
+ end
12
+
13
+ describe HTTParty do
14
+
15
+ describe "base uri" do
16
+ before do
17
+ Foo.base_uri('api.foo.com/v1')
18
+ end
19
+
20
+ it "should have reader" do
21
+ Foo.base_uri.should == 'http://api.foo.com/v1'
22
+ end
23
+
24
+ it 'should have writer' do
25
+ Foo.base_uri('http://api.foobar.com')
26
+ Foo.base_uri.should == 'http://api.foobar.com'
27
+ end
28
+
29
+ it "should add http if not present for non ssl requests" do
30
+ Foo.base_uri('api.foobar.com')
31
+ Foo.base_uri.should == 'http://api.foobar.com'
32
+ end
33
+
34
+ it "should add https if not present for ssl requests" do
35
+ Foo.base_uri('api.foo.com/v1:443')
36
+ Foo.base_uri.should == 'https://api.foo.com/v1:443'
37
+ end
38
+
39
+ it "should not remove https for ssl requests" do
40
+ Foo.base_uri('https://api.foo.com/v1:443')
41
+ Foo.base_uri.should == 'https://api.foo.com/v1:443'
42
+ end
43
+ end
44
+
45
+ describe "headers" do
46
+ it "should default to empty hash" do
47
+ Foo.headers.should == {}
48
+ end
49
+
50
+ it "should be able to be updated" do
51
+ init_headers = {:foo => 'bar', :baz => 'spax'}
52
+ Foo.headers init_headers
53
+ Foo.headers.should == init_headers
54
+ end
55
+ end
56
+
57
+ describe "default params" do
58
+ it "should default to empty hash" do
59
+ Foo.default_params.should == {}
60
+ end
61
+
62
+ it "should be able to be updated" do
63
+ new_defaults = {:foo => 'bar', :baz => 'spax'}
64
+ Foo.default_params new_defaults
65
+ Foo.default_params.should == new_defaults
66
+ end
67
+ end
68
+
69
+ describe "basic http authentication" do
70
+ it "should work" do
71
+ Foo.basic_auth 'foobar', 'secret'
72
+ Foo.default_options[:basic_auth].should == {:username => 'foobar', :password => 'secret'}
73
+ end
74
+ end
75
+
76
+ describe "format" do
77
+ it "should allow xml" do
78
+ Foo.format :xml
79
+ Foo.default_options[:format].should == :xml
80
+ end
81
+
82
+ it "should allow json" do
83
+ Foo.format :json
84
+ Foo.default_options[:format].should == :json
85
+ end
86
+
87
+ it 'should not allow funky format' do
88
+ lambda do
89
+ Foo.format :foobar
90
+ end.should raise_error(HTTParty::UnsupportedFormat)
91
+ end
92
+ end
93
+
94
+ describe "with explicit override of automatic redirect handling" do
95
+
96
+ it "should fail with redirected GET" do
97
+ lambda do
98
+ Foo.get('/foo', :no_follow => true)
99
+ end.should raise_error(HTTParty::RedirectionTooDeep)
100
+ end
101
+
102
+ it "should fail with redirected POST" do
103
+ lambda do
104
+ Foo.post('/foo', :no_follow => true)
105
+ end.should raise_error(HTTParty::RedirectionTooDeep)
106
+ end
107
+
108
+ it "should fail with redirected DELETE" do
109
+ lambda do
110
+ Foo.delete('/foo', :no_follow => true)
111
+ end.should raise_error(HTTParty::RedirectionTooDeep)
112
+ end
113
+
114
+ it "should fail with redirected PUT" do
115
+ lambda do
116
+ Foo.put('/foo', :no_follow => true)
117
+ end.should raise_error(HTTParty::RedirectionTooDeep)
118
+ end
119
+
120
+ describe "that respond with redirects" do
121
+ describe "with explicit override of automatic redirect handling" do
122
+
123
+ it "should fail with redirected GET" do
124
+ lambda do
125
+ Foo.get('/foo', :no_follow => true)
126
+ end.should raise_error(HTTParty::RedirectionTooDeep)
127
+ end
128
+
129
+ it "should fail with redirected POST" do
130
+ lambda do
131
+ Foo.post('/foo', :no_follow => true)
132
+ end.should raise_error(HTTParty::RedirectionTooDeep)
133
+ end
134
+
135
+ it "should fail with redirected DELETE" do
136
+ lambda do
137
+ Foo.delete('/foo', :no_follow => true)
138
+ end
139
+ end
140
+
141
+ it "should fail with redirected PUT" do
142
+ lambda do
143
+ Foo.put('/foo', :no_follow => true)
144
+ end
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,3 @@
1
+ --format
2
+ progress
3
+ --colour
@@ -0,0 +1,9 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'httparty')
@@ -0,0 +1,43 @@
1
+ desc 'Preps the gem for a new release'
2
+ task :prep_for_release do
3
+ require 'rio'
4
+ Rake::Task['manifest:refresh'].invoke
5
+ gemspec = %x[rake debug_gem]
6
+ lines = gemspec.split("\n")
7
+ rio('httparty.gemspec') < lines[1, lines.length-1].join("\n")
8
+ end
9
+
10
+ desc 'Release the website and new gem version'
11
+ task :deploy => [:check_version, :website, :release] do
12
+ puts "Remember to create SVN tag:"
13
+ puts "svn copy svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/trunk " +
14
+ "svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/tags/REL-#{VERS} "
15
+ puts "Suggested comment:"
16
+ puts "Tagging release #{CHANGES}"
17
+ end
18
+
19
+ desc 'Runs tasks website_generate and install_gem as a local deployment of the gem'
20
+ task :local_deploy => [:website_generate, :install_gem]
21
+
22
+ task :check_version do
23
+ unless ENV['VERSION']
24
+ puts 'Must pass a VERSION=x.y.z release version'
25
+ exit
26
+ end
27
+ unless ENV['VERSION'] == VERS
28
+ puts "Please update your version.rb to match the release version, currently #{VERS}"
29
+ exit
30
+ end
31
+ end
32
+
33
+ desc 'Install the package as a gem, without generating documentation(ri/rdoc)'
34
+ task :install_gem_no_doc => [:clean, :package] do
35
+ sh "#{'sudo ' unless Hoe::WINDOZE }gem install pkg/*.gem --no-rdoc --no-ri"
36
+ end
37
+
38
+ namespace :manifest do
39
+ desc 'Recreate Manifest.txt to include ALL files'
40
+ task :refresh do
41
+ `rake check_manifest | patch -p0 > Manifest.txt`
42
+ end
43
+ end
@@ -0,0 +1,7 @@
1
+ task :ruby_env do
2
+ RUBY_APP = if RUBY_PLATFORM =~ /java/
3
+ "jruby"
4
+ else
5
+ "ruby"
6
+ end unless defined? RUBY_APP
7
+ end
@@ -0,0 +1,17 @@
1
+ desc 'Generate website files'
2
+ task :website_generate => :ruby_env do
3
+ (Dir['website/**/*.txt'] - Dir['website/version*.txt']).each do |txt|
4
+ sh %{ #{RUBY_APP} script/txt2html #{txt} > #{txt.gsub(/txt$/,'html')} }
5
+ end
6
+ end
7
+
8
+ desc 'Upload website files to rubyforge'
9
+ task :website_upload do
10
+ host = "#{rubyforge_username}@rubyforge.org"
11
+ remote_dir = "/var/www/gforge-projects/#{PATH}/"
12
+ local_dir = 'website'
13
+ sh %{rsync -aCv #{local_dir}/ #{host}:#{remote_dir}}
14
+ end
15
+
16
+ desc 'Generate and upload website files'
17
+ task :website => [:website_upload, :publish_docs]
@@ -0,0 +1,47 @@
1
+ @media screen, projection {
2
+ /*
3
+ Copyright (c) 2007, Yahoo! Inc. All rights reserved.
4
+ Code licensed under the BSD License:
5
+ http://developer.yahoo.net/yui/license.txt
6
+ version: 2.2.0
7
+ */
8
+ body {font:13px arial,helvetica,clean,sans-serif;*font-size:small;*font:x-small;}table {font-size:inherit;font:100%;}select, input, textarea {font:99% arial,helvetica,clean,sans-serif;}pre, code {font:115% monospace;*font-size:100%;}body * {line-height:1.22em;}
9
+ body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th,td{margin:0;padding:0;}table{border-collapse:collapse;border-spacing:0;}fieldset,img{border:0;}address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;font-weight:normal;}/*ol,ul {list-style:none;}*/caption,th {text-align:left;}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal;}q:before,q:after{content:'';}abbr,acronym {border:0;}
10
+ /* end of yahoo reset and fonts */
11
+
12
+ body {color:#333; background:#4b1a1a; line-height:1.3;}
13
+ p {margin:0 0 20px;}
14
+ a {color:#4b1a1a;}
15
+ a:hover {text-decoration:none;}
16
+ strong {font-weight:bold;}
17
+ em {font-style:italics;}
18
+ h1,h2,h3,h4,h5,h6 {font-weight:bold;}
19
+ h1 {font-size:197%; margin:30px 0; color:#4b1a1a;}
20
+ h2 {font-size:174%; margin:20px 0; color:#b8111a;}
21
+ h3 {font-size:152%; margin:10px 0;}
22
+ h4 {font-size:129%; margin:10px 0;}
23
+ pre {background:#eee; margin:0 0 20px; padding:20px; border:1px solid #ccc; font-size:100%; overflow:auto;}
24
+ code {font-size:100%; margin:0; padding:0;}
25
+ ul, ol {margin:10px 0 10px 25px;}
26
+ ol li {margin:0 0 10px;}
27
+
28
+
29
+
30
+
31
+
32
+ div#wrapper {background:#fff; width:560px; margin:0 auto; padding:20px; border:10px solid #bc8c46; border-width:0 10px;}
33
+ div#header {position:relative; border-bottom:1px dotted; margin:0 0 10px; padding:0 0 10px;}
34
+ div#header p {margin:0; padding:0;}
35
+ div#header h1 {margin:0; padding:0;}
36
+ ul#nav {position:absolute; top:0; right:0; list-style:none; margin:0; padding:0;}
37
+ ul#nav li {display:inline; padding:0 0 0 5px;}
38
+ ul#nav li a {}
39
+ div#content {}
40
+ div#footer {margin:40px 0 0; border-top:1px dotted; padding:10px 0 0;}
41
+
42
+
43
+
44
+
45
+
46
+
47
+ }
@@ -0,0 +1,83 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
5
+ <title>HTTParty by John Nunemaker</title>
6
+ <link rel="stylesheet" href="css/common.css" type="text/css" />
7
+ </head>
8
+ <body>
9
+
10
+ <div id="wrapper">
11
+ <div id="header">
12
+ <h1>HTTParty</h1>
13
+ <p>Tonight we're gonna HTTParty like it's 1999!</p>
14
+
15
+ <ul id="nav">
16
+ <li><a href="rdoc/">Docs</a></li>
17
+ <li><a href="http://github.com/jnunemaker/httparty">Github</a></li>
18
+ <li><a href="http://jnunemaker.lighthouseapp.com/projects/14842-httparty/tickets">Lighthouse</a></li>
19
+ <li><a href="http://rubyforge.org/projects/httparty/">Rubyforge</a></li>
20
+ </ul>
21
+ </div>
22
+
23
+ <div id="content">
24
+ <h2>Install</h2>
25
+ <pre><code>$ sudo gem install httparty</code></pre>
26
+
27
+ <h2>Some Quick Examples</h2>
28
+
29
+ <p>The following is a simple example of wrapping Twitter's API for posting updates.</p>
30
+
31
+ <pre><code>class Twitter
32
+ include HTTParty
33
+ base_uri 'twitter.com'
34
+ basic_auth 'username', 'password'
35
+ end
36
+
37
+ Twitter.post('/statuses/update.json', :query => {:status => "It's an HTTParty and everyone is invited!"})</code></pre>
38
+
39
+ <p>That is really it! The object returned is a ruby hash that is decoded from Twitter's json response. JSON parsing is used because of the .json extension in the path of the request. You can also explicitly set a format (see the examples). </p>
40
+
41
+ <p>That works and all but what if you don't want to embed your username and password in the class? Below is an example to fix that:</p>
42
+
43
+ <pre><code>class Twitter
44
+ include HTTParty
45
+ base_uri 'twitter.com'
46
+
47
+ def initialize(u, p)
48
+ @auth = {:username => u, :password => p}
49
+ end
50
+
51
+ def post(text)
52
+ options = { :query => {:status => text}, :basic_auth => @auth }
53
+ self.class.post('/statuses/update.json', options)
54
+ end
55
+ end
56
+
57
+ Twitter.new('username', 'password').post("It's an HTTParty and everyone is invited!")</code></pre>
58
+
59
+ <p><strong>More Examples:</strong> There are <a href="http://github.com/jnunemaker/httparty/tree/master/examples/">several examples in the gem itself</a>.</p>
60
+
61
+ <h2>Support</h2>
62
+ <p>Conversations welcome in the <a href="http://groups.google.com/group/httparty-gem">google group</a> and bugs/features over at <a href="http://jnunemaker.lighthouseapp.com/projects/14842-httparty/overview">Lightouse</a>.</p>
63
+
64
+
65
+ </div>
66
+
67
+ <div id="footer">
68
+ <p>Created by <a href="http://addictedtonew.com/about/">John Nunemaker</a></p>
69
+ </div>
70
+ </div>
71
+
72
+ <script type="text/javascript">
73
+ var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
74
+ document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
75
+ </script>
76
+ <script type="text/javascript">
77
+ var pageTracker = _gat._getTracker("UA-85301-19");
78
+ pageTracker._initData();
79
+ pageTracker._trackPageview();
80
+ </script>
81
+
82
+ </body>
83
+ </html>
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alexvollmer-httparty
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.5
5
+ platform: ruby
6
+ authors:
7
+ - John Nunemaker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-14 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "2.1"
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: hoe
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 1.8.0
32
+ version:
33
+ description: Makes http fun! Also, makes consuming restful web services dead easy.
34
+ email:
35
+ - nunemaker@gmail.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - History.txt
42
+ - License.txt
43
+ - Manifest.txt
44
+ - PostInstall.txt
45
+ - README.txt
46
+ files:
47
+ - History.txt
48
+ - License.txt
49
+ - Manifest.txt
50
+ - PostInstall.txt
51
+ - README.txt
52
+ - Rakefile
53
+ - config/hoe.rb
54
+ - config/requirements.rb
55
+ - examples/aaws.rb
56
+ - examples/delicious.rb
57
+ - examples/google.rb
58
+ - examples/twitter.rb
59
+ - examples/whoismyrep.rb
60
+ - httparty.gemspec
61
+ - lib/httparty.rb
62
+ - lib/httparty/request.rb
63
+ - lib/httparty/version.rb
64
+ - script/console
65
+ - script/destroy
66
+ - script/generate
67
+ - script/txt2html
68
+ - setup.rb
69
+ - spec/httparty/request_spec.rb
70
+ - spec/httparty_spec.rb
71
+ - spec/spec.opts
72
+ - spec/spec_helper.rb
73
+ - tasks/deployment.rake
74
+ - tasks/environment.rake
75
+ - tasks/website.rake
76
+ - website/css/common.css
77
+ - website/index.html
78
+ has_rdoc: true
79
+ homepage: http://httparty.rubyforge.org
80
+ post_install_message: When you HTTParty, you must party hard!
81
+ rdoc_options:
82
+ - --main
83
+ - README.txt
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: "0"
91
+ version:
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: "0"
97
+ version:
98
+ requirements: []
99
+
100
+ rubyforge_project: httparty
101
+ rubygems_version: 1.2.0
102
+ signing_key:
103
+ specification_version: 2
104
+ summary: Makes http fun! Also, makes consuming restful web services dead easy.
105
+ test_files: []
106
+