jnunemaker-httparty 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/spec/hash_spec.rb ADDED
@@ -0,0 +1,11 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe HTTParty::CoreExt::HashConversions do
4
+ it "should convert hash to struct" do
5
+ {'foo' => 'bar'}.to_struct.should == OpenStruct.new(:foo => 'bar')
6
+ end
7
+
8
+ it 'should convert nested hash to struct' do
9
+ {'foo' => {'bar' => 'baz'}}.to_struct.should == OpenStruct.new(:foo => OpenStruct.new(:bar => 'baz'))
10
+ end
11
+ end
@@ -0,0 +1,145 @@
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
+ it "should be gettable" do
17
+ Foo.base_uri.should == 'http://api.foo.com/v1'
18
+ end
19
+
20
+ it 'should be setable' do
21
+ Foo.base_uri('http://api.foobar.com')
22
+ Foo.base_uri.should == 'http://api.foobar.com'
23
+ end
24
+
25
+ it "should add http if not present for non ssl requests" do
26
+ Foo.base_uri('api.foobar.com')
27
+ Foo.base_uri.should == 'http://api.foobar.com'
28
+ end
29
+
30
+ it "should add https if not present for ssl requests" do
31
+ FooWithHttps.base_uri.should == 'https://api.foo.com/v1:443'
32
+ end
33
+ end
34
+
35
+ describe "headers" do
36
+ it "should default to empty hash" do
37
+ Foo.headers.should == {}
38
+ end
39
+
40
+ it "should be able to be updated" do
41
+ init_headers = {:foo => 'bar', :baz => 'spax'}
42
+ Foo.headers init_headers
43
+ Foo.headers.should == init_headers
44
+ end
45
+ end
46
+
47
+ describe "default params" do
48
+ it "should default to empty hash" do
49
+ Foo.default_params.should == {}
50
+ end
51
+
52
+ it "should be able to be updated" do
53
+ new_defaults = {:foo => 'bar', :baz => 'spax'}
54
+ Foo.default_params new_defaults
55
+ Foo.default_params.should == new_defaults
56
+ end
57
+ end
58
+
59
+ describe "basic http authentication" do
60
+ it "should work" do
61
+ Foo.basic_auth 'foobar', 'secret'
62
+ Foo.instance_variable_get("@auth").should == {:username => 'foobar', :password => 'secret'}
63
+ end
64
+ end
65
+
66
+ describe "format" do
67
+ it "should allow xml" do
68
+ Foo.format :xml
69
+ Foo.instance_variable_get("@format").should == 'xml'
70
+ end
71
+
72
+ it "should allow json" do
73
+ Foo.format :json
74
+ Foo.instance_variable_get("@format").should == 'json'
75
+ end
76
+
77
+ it 'should not allow funky format' do
78
+ lambda do
79
+ Foo.format :foobar
80
+ end.should raise_error(HTTParty::UnsupportedFormat)
81
+ end
82
+ end
83
+
84
+ describe 'http' do
85
+ it "should use ssl for port 443" do
86
+ FooWithHttps.send(:http, URI.parse('https://api.foo.com/v1:443')).use_ssl?.should == true
87
+ end
88
+
89
+ it 'should not use ssl for port 80' do
90
+ Foo.send(:http, URI.parse('http://foobar.com')).use_ssl?.should == false
91
+ end
92
+ end
93
+
94
+ describe "deriving format from path" do
95
+ it "should work if there is extension and extension is an allowed format" do
96
+ %w[xml json].each do |ext|
97
+ Foo.send(:format_from_path, "/foo/bar.#{ext}").should == ext
98
+ end
99
+ end
100
+
101
+ it "should NOT work if there is extension but extention is not allow format" do
102
+ Foo.send(:format_from_path, '/foo/bar.php').should == nil
103
+ end
104
+
105
+ it 'should NOT work if there is no extension' do
106
+ ['', '.'].each do |ext|
107
+ Foo.send(:format_from_path, "/foo/bar#{ext}").should == nil
108
+ end
109
+ end
110
+ end
111
+
112
+ describe 'parsing responses' do
113
+ it 'should handle xml automatically' do
114
+ xml = %q[<books><book><id>1234</id><name>Foo Bar!</name></book></books>]
115
+ Foo.format :xml
116
+ Foo.send(:parse_response, xml).should == {'books' => {'book' => {'id' => '1234', 'name' => 'Foo Bar!'}}}
117
+ end
118
+
119
+ it 'should handle json automatically' do
120
+ json = %q[{"books": {"book": {"name": "Foo Bar!", "id": "1234"}}}]
121
+ Foo.format :json
122
+ Foo.send(:parse_response, json).should == {'books' => {'book' => {'id' => '1234', 'name' => 'Foo Bar!'}}}
123
+ end
124
+ end
125
+
126
+ describe "sending requests" do
127
+ it "should not work with request method other than get, post, put, delete" do
128
+ lambda do
129
+ Foo.send(:send_request, 'foo', '/foo')
130
+ end.should raise_error(ArgumentError)
131
+ end
132
+
133
+ it 'should require that :headers is a hash if present' do
134
+ lambda do
135
+ Foo.send(:send_request, 'get', '/foo', :headers => 'string')
136
+ end.should raise_error(ArgumentError)
137
+ end
138
+
139
+ it 'should require that :basic_auth is a hash if present' do
140
+ lambda do
141
+ Foo.send(:send_request, 'get', '/foo', :basic_auth => 'string')
142
+ end.should raise_error(ArgumentError)
143
+ end
144
+ end
145
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --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,34 @@
1
+ desc 'Release the website and new gem version'
2
+ task :deploy => [:check_version, :website, :release] do
3
+ puts "Remember to create SVN tag:"
4
+ puts "svn copy svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/trunk " +
5
+ "svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/tags/REL-#{VERS} "
6
+ puts "Suggested comment:"
7
+ puts "Tagging release #{CHANGES}"
8
+ end
9
+
10
+ desc 'Runs tasks website_generate and install_gem as a local deployment of the gem'
11
+ task :local_deploy => [:website_generate, :install_gem]
12
+
13
+ task :check_version do
14
+ unless ENV['VERSION']
15
+ puts 'Must pass a VERSION=x.y.z release version'
16
+ exit
17
+ end
18
+ unless ENV['VERSION'] == VERS
19
+ puts "Please update your version.rb to match the release version, currently #{VERS}"
20
+ exit
21
+ end
22
+ end
23
+
24
+ desc 'Install the package as a gem, without generating documentation(ri/rdoc)'
25
+ task :install_gem_no_doc => [:clean, :package] do
26
+ sh "#{'sudo ' unless Hoe::WINDOZE }gem install pkg/*.gem --no-rdoc --no-ri"
27
+ end
28
+
29
+ namespace :manifest do
30
+ desc 'Recreate Manifest.txt to include ALL files'
31
+ task :refresh do
32
+ `rake check_manifest | patch -p0 > Manifest.txt`
33
+ end
34
+ 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_generate, :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,77 @@
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://rubyforge.org/projects/httparty/">Rubyforge</a></li>
19
+ </ul>
20
+ </div>
21
+
22
+ <div id="content">
23
+ <h2>Install</h2>
24
+ <pre><code>$ sudo gem install httparty</code></pre>
25
+
26
+ <h2>Some Quick Examples</h2>
27
+
28
+ <p>The following is a simple example of wrapping Twitter's API for posting updates.</p>
29
+
30
+ <pre><code>class Twitter
31
+ include HTTParty
32
+ base_uri 'twitter.com'
33
+ basic_auth 'username', 'password'
34
+ end
35
+
36
+ Twitter.post('/statuses/update.json', :query => {:status => "It's an HTTParty and everyone is invited!"})</code></pre>
37
+
38
+ <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>
39
+
40
+ <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>
41
+
42
+ <pre><code>class Twitter
43
+ include HTTParty
44
+ base_uri 'twitter.com'
45
+
46
+ def initialize(user, pass)
47
+ self.class.basic_auth user, pass
48
+ end
49
+
50
+ def post(text)
51
+ self.class.post('/statuses/update.json', :query => {:status => text})
52
+ end
53
+ end
54
+
55
+ Twitter.new('username', 'password').post("It's an HTTParty and everyone is invited!")</code></pre>
56
+
57
+ <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>
58
+
59
+ <h2>Support</h2>
60
+ <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>
61
+
62
+
63
+ </div>
64
+
65
+ <div id="footer">
66
+ <p>Created by <a href="http://addictedtonew.com/about/">John Nunemaker</a></p>
67
+ </div>
68
+ </div>
69
+
70
+ <script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script>
71
+ <script type="text/javascript">_uacct = "UA-85301-9"; urchinTracker();</script>
72
+
73
+ <!-- 103bees.com 'bee' code v1.11 - please do not make any changes! -->
74
+ <script type="text/javascript" src="http://103bees.com/bees/?bee=3672&amp;fid=5643"></script>
75
+ <!-- 103bees.com 'bee' code -->
76
+ </body>
77
+ </html>
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jnunemaker-httparty
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - John Nunemaker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-07-31 00:00:00 -07: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
+ description: Makes http fun! Also, makes consuming restful web services dead easy.
25
+ email:
26
+ - nunemaker@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - History.txt
33
+ - License.txt
34
+ - Manifest.txt
35
+ - PostInstall.txt
36
+ - README.txt
37
+ files:
38
+ - History.txt
39
+ - License.txt
40
+ - Manifest.txt
41
+ - PostInstall.txt
42
+ - README.txt
43
+ - Rakefile
44
+ - config/hoe.rb
45
+ - config/requirements.rb
46
+ - examples/aaws.rb
47
+ - examples/delicious.rb
48
+ - examples/twitter.rb
49
+ - examples/whoismyrep.rb
50
+ - httparty.gemspec
51
+ - lib/httparty.rb
52
+ - lib/httparty/core_ext.rb
53
+ - lib/httparty/core_ext/hash.rb
54
+ - lib/httparty/version.rb
55
+ - script/console
56
+ - script/destroy
57
+ - script/generate
58
+ - script/txt2html
59
+ - setup.rb
60
+ - spec/hash_spec.rb
61
+ - spec/httparty_spec.rb
62
+ - spec/spec.opts
63
+ - spec/spec_helper.rb
64
+ - tasks/deployment.rake
65
+ - tasks/environment.rake
66
+ - tasks/website.rake
67
+ - website/css/common.css
68
+ - website/index.html
69
+ has_rdoc: true
70
+ homepage: http://httparty.rubyforge.org
71
+ post_install_message: When you HTTParty, you must party hard!
72
+ rdoc_options:
73
+ - --main
74
+ - README.txt
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
+ version:
89
+ requirements: []
90
+
91
+ rubyforge_project: httparty
92
+ rubygems_version: 1.2.0
93
+ signing_key:
94
+ specification_version: 2
95
+ summary: Makes http fun! Also, makes consuming restful web services dead easy.
96
+ test_files: []
97
+