juliocesar-httparty 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,181 @@
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 GRest
9
+ include HTTParty
10
+ base_uri "grest.com"
11
+ default_params :one => 'two'
12
+ end
13
+
14
+ class HRest
15
+ include HTTParty
16
+ base_uri "hrest.com"
17
+ default_params :two => 'three'
18
+ end
19
+
20
+ describe HTTParty do
21
+
22
+ describe "base uri" do
23
+ before do
24
+ Foo.base_uri('api.foo.com/v1')
25
+ end
26
+
27
+ it "should have reader" do
28
+ Foo.base_uri.should == 'http://api.foo.com/v1'
29
+ end
30
+
31
+ it 'should have writer' do
32
+ Foo.base_uri('http://api.foobar.com')
33
+ Foo.base_uri.should == 'http://api.foobar.com'
34
+ end
35
+ end
36
+
37
+ describe "#normalize_base_uri" do
38
+ it "should add http if not present for non ssl requests" do
39
+ uri = HTTParty.normalize_base_uri('api.foobar.com')
40
+ uri.should == 'http://api.foobar.com'
41
+ end
42
+
43
+ it "should add https if not present for ssl requests" do
44
+ uri = HTTParty.normalize_base_uri('api.foo.com/v1:443')
45
+ uri.should == 'https://api.foo.com/v1:443'
46
+ end
47
+
48
+ it "should not remove https for ssl requests" do
49
+ uri = HTTParty.normalize_base_uri('https://api.foo.com/v1:443')
50
+ uri.should == 'https://api.foo.com/v1:443'
51
+ end
52
+ end
53
+
54
+ describe "headers" do
55
+ it "should default to empty hash" do
56
+ Foo.headers.should == {}
57
+ end
58
+
59
+ it "should be able to be updated" do
60
+ init_headers = {:foo => 'bar', :baz => 'spax'}
61
+ Foo.headers init_headers
62
+ Foo.headers.should == init_headers
63
+ end
64
+ end
65
+
66
+ describe "default params" do
67
+ it "should default to empty hash" do
68
+ Foo.default_params.should == {}
69
+ end
70
+
71
+ it "should be able to be updated" do
72
+ new_defaults = {:foo => 'bar', :baz => 'spax'}
73
+ Foo.default_params new_defaults
74
+ Foo.default_params.should == new_defaults
75
+ end
76
+ end
77
+
78
+ describe "basic http authentication" do
79
+ it "should work" do
80
+ Foo.basic_auth 'foobar', 'secret'
81
+ Foo.default_options[:basic_auth].should == {:username => 'foobar', :password => 'secret'}
82
+ end
83
+ end
84
+
85
+ describe "format" do
86
+ it "should allow xml" do
87
+ Foo.format :xml
88
+ Foo.default_options[:format].should == :xml
89
+ end
90
+
91
+ it "should allow json" do
92
+ Foo.format :json
93
+ Foo.default_options[:format].should == :json
94
+ end
95
+
96
+ it 'should not allow funky format' do
97
+ lambda do
98
+ Foo.format :foobar
99
+ end.should raise_error(HTTParty::UnsupportedFormat)
100
+ end
101
+ end
102
+
103
+ describe "with explicit override of automatic redirect handling" do
104
+
105
+ it "should fail with redirected GET" do
106
+ lambda do
107
+ Foo.get('/foo', :no_follow => true)
108
+ end.should raise_error(HTTParty::RedirectionTooDeep)
109
+ end
110
+
111
+ it "should fail with redirected POST" do
112
+ lambda do
113
+ Foo.post('/foo', :no_follow => true)
114
+ end.should raise_error(HTTParty::RedirectionTooDeep)
115
+ end
116
+
117
+ it "should fail with redirected DELETE" do
118
+ lambda do
119
+ Foo.delete('/foo', :no_follow => true)
120
+ end.should raise_error(HTTParty::RedirectionTooDeep)
121
+ end
122
+
123
+ it "should fail with redirected PUT" do
124
+ lambda do
125
+ Foo.put('/foo', :no_follow => true)
126
+ end.should raise_error(HTTParty::RedirectionTooDeep)
127
+ end
128
+ end
129
+
130
+ describe "with multiple class definitions" do
131
+ it "should not run over each others options" do
132
+ HRest.default_options.should == {:base_uri => 'http://hrest.com', :default_params => {:two => 'three'}}
133
+ GRest.default_options.should == {:base_uri => 'http://grest.com', :default_params => {:one => 'two'}}
134
+ end
135
+ end
136
+
137
+ describe "#get" do
138
+ it "should be able to get html" do
139
+ stub_http_response_with('google.html')
140
+ resp = HTTParty.get('http://www.google.com')
141
+ resp.should == file_fixture('google.html')
142
+ resp.original_body.should == file_fixture('google.html')
143
+ end
144
+
145
+ it "should be able parse response type json automatically" do
146
+ stub_http_response_with('twitter.json')
147
+ tweets = HTTParty.get('http://twitter.com/statuses/public_timeline.json')
148
+ tweets.size.should == 20
149
+ tweets.first['user'].should == {
150
+ "name" => "Pyk",
151
+ "url" => nil,
152
+ "id" => "7694602",
153
+ "description" => nil,
154
+ "protected" => false,
155
+ "screen_name" => "Pyk",
156
+ "followers_count" => 1,
157
+ "location" => "Opera Plaza, California",
158
+ "profile_image_url" => "http://static.twitter.com/images/default_profile_normal.png"
159
+ }
160
+ tweets.original_body.should == file_fixture('twitter.json')
161
+ end
162
+
163
+ it "should be able parse response type xml automatically" do
164
+ stub_http_response_with('twitter.xml')
165
+ tweets = HTTParty.get('http://twitter.com/statuses/public_timeline.xml')
166
+ tweets['statuses'].size.should == 20
167
+ tweets['statuses'].first['user'].should == {
168
+ "name" => "Magic 8 Bot",
169
+ "url" => nil,
170
+ "id" => "17656026",
171
+ "description" => "ask me a question",
172
+ "protected" => "false",
173
+ "screen_name" => "magic8bot",
174
+ "followers_count" => "90",
175
+ "profile_image_url" => "http://s3.amazonaws.com/twitter_production/profile_images/65565851/8ball_large_normal.jpg",
176
+ "location" => nil
177
+ }
178
+ tweets.original_body.should == file_fixture('twitter.xml')
179
+ end
180
+ end
181
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,3 @@
1
+ --format
2
+ progress
3
+ --colour
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ gem 'rspec'
3
+ require 'spec'
4
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'httparty')
5
+
6
+ def file_fixture(filename)
7
+ open(File.join(File.dirname(__FILE__), 'fixtures', "#{filename.to_s}")).read
8
+ end
9
+
10
+ def stub_http_response_with(filename)
11
+ format = filename.split('.').last.intern
12
+ data = file_fixture(filename)
13
+ http = Net::HTTP.new('localhost', 80)
14
+
15
+ response = Net::HTTPOK.new("1.1", 200, "Content for you")
16
+ response.stub!(:body).and_return(data)
17
+ http.stub!(:request).and_return(response)
18
+
19
+ http_request = HTTParty::Request.new(Net::HTTP::Get, '')
20
+ http_request.stub!(:get_response).and_return(response)
21
+ http_request.stub!(:format).and_return(format)
22
+
23
+ HTTParty::Request.should_receive(:new).and_return(http_request)
24
+ end
@@ -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,74 @@
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> |
69
+ <a href="http://orderedlist.com/">Hire Me at Ordered List</a></p>
70
+ </div>
71
+ </div>
72
+
73
+ </body>
74
+ </html>
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: juliocesar-httparty
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.6
5
+ platform: ruby
6
+ authors:
7
+ - John Nunemaker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-05 00:00:00 -08:00
13
+ default_executable: httparty
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: json
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: "1.1"
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: echoe
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: "0"
32
+ version:
33
+ description: Makes http fun! Also, makes consuming restful web services dead easy.
34
+ email: nunemaker@gmail.com
35
+ executables:
36
+ - httparty
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - bin/httparty
41
+ - lib/core_extensions.rb
42
+ - lib/httparty/exceptions.rb
43
+ - lib/httparty/request.rb
44
+ - lib/httparty/version.rb
45
+ - lib/httparty.rb
46
+ - lib/module_level_inheritable_attributes.rb
47
+ - README
48
+ files:
49
+ - bin/httparty
50
+ - examples/aaws.rb
51
+ - examples/basic.rb
52
+ - examples/delicious.rb
53
+ - examples/google.rb
54
+ - examples/rubyurl.rb
55
+ - examples/twitter.rb
56
+ - examples/whoismyrep.rb
57
+ - History
58
+ - httparty.gemspec
59
+ - lib/core_extensions.rb
60
+ - lib/httparty/exceptions.rb
61
+ - lib/httparty/request.rb
62
+ - lib/httparty/version.rb
63
+ - lib/httparty.rb
64
+ - lib/module_level_inheritable_attributes.rb
65
+ - Manifest
66
+ - MIT-LICENSE
67
+ - Rakefile
68
+ - README
69
+ - setup.rb
70
+ - spec/as_buggery_spec.rb
71
+ - spec/fixtures/delicious.xml
72
+ - spec/fixtures/google.html
73
+ - spec/fixtures/twitter.json
74
+ - spec/fixtures/twitter.xml
75
+ - spec/httparty/request_spec.rb
76
+ - spec/httparty_spec.rb
77
+ - spec/spec.opts
78
+ - spec/spec_helper.rb
79
+ - website/css/common.css
80
+ - website/index.html
81
+ has_rdoc: true
82
+ homepage: http://httparty.rubyforge.org
83
+ post_install_message: When you HTTParty, you must party hard!
84
+ rdoc_options:
85
+ - --line-numbers
86
+ - --inline-source
87
+ - --title
88
+ - Httparty
89
+ - --main
90
+ - README
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: "0"
98
+ version:
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: "1.2"
104
+ version:
105
+ requirements: []
106
+
107
+ rubyforge_project: httparty
108
+ rubygems_version: 1.2.0
109
+ signing_key:
110
+ specification_version: 2
111
+ summary: Makes http fun! Also, makes consuming restful web services dead easy.
112
+ test_files: []
113
+