httparty 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of httparty might be problematic. Click here for more details.

data/History.txt CHANGED
@@ -1,3 +1,9 @@
1
+ == 0.1.1 2008-07-30
2
+
3
+ * 2 major enhancement:
4
+ * Added :basic_auth key for options when making a request
5
+ * :query and :body both now work with query string or hash
6
+
1
7
  == 0.1.0 2008-07-27
2
8
 
3
9
  * 1 major enhancement:
data/Manifest.txt CHANGED
@@ -27,3 +27,5 @@ spec/spec_helper.rb
27
27
  tasks/deployment.rake
28
28
  tasks/environment.rake
29
29
  tasks/website.rake
30
+ website/css/common.css
31
+ website/index.html
@@ -8,8 +8,8 @@ class Delicious
8
8
  base_uri 'https://api.del.icio.us/v1'
9
9
  format :xml
10
10
 
11
- def initialize(user, pass)
12
- self.class.basic_auth(user, pass)
11
+ def initialize(u, p)
12
+ @auth = {:username => u, :password => p}
13
13
  end
14
14
 
15
15
  # query params that filter the posts are:
@@ -18,23 +18,21 @@ class Delicious
18
18
  # url (optional). Filter by this url.
19
19
  # ie: posts(:query => {:tag => 'ruby'})
20
20
  def posts(options={})
21
+ options.merge!({:basic_auth => @auth})
21
22
  # get posts and convert to structs so we can do .key instead of ['key'] with results
22
- self.class.get('/posts/get', options)['posts']['post'].map { |b| b.to_struct }
23
+ self.class.get('/posts/get', options)
23
24
  end
24
25
 
25
26
  # query params that filter the posts are:
26
27
  # tag (optional). Filter by this tag.
27
28
  # count (optional). Number of items to retrieve (Default:15, Maximum:100).
28
29
  def recent(options={})
29
- self.class.get('/posts/recent', options)['posts']['post'].map { |b| b.to_struct }
30
+ options.merge!({:basic_auth => @auth})
31
+ self.class.get('/posts/recent', options)
30
32
  end
31
33
  end
32
34
 
33
35
  delicious = Delicious.new(config['username'], config['password'])
34
-
35
36
  pp delicious.posts(:query => {:tag => 'ruby'})
36
-
37
- puts '', '*' * 50, ''
38
-
39
37
  pp delicious.recent
40
38
 
data/examples/twitter.rb CHANGED
@@ -7,30 +7,25 @@ class Twitter
7
7
  include HTTParty
8
8
  base_uri 'twitter.com'
9
9
 
10
- def initialize(user, pass)
11
- self.class.basic_auth user, pass
10
+ def initialize(u, p)
11
+ @auth = {:username => u, :password => p}
12
12
  end
13
13
 
14
14
  # which can be :friends, :user or :public
15
15
  # options[:query] can be things like since, since_id, count, etc.
16
16
  def timeline(which=:friends, options={})
17
- self.class.get("/statuses/#{which}_timeline.xml", options)['statuses'].map { |s| s.to_struct }
17
+ options.merge!({:basic_auth => @auth})
18
+ self.class.get("/statuses/#{which}_timeline.json", options)
18
19
  end
19
20
 
20
21
  def post(text)
21
- self.class.post('/statuses/update.xml', :query => {:status => text})['status'].to_struct
22
+ options = { :query => {:status => text}, :basic_auth => @auth }
23
+ self.class.post('/statuses/update.json', options)
22
24
  end
23
25
  end
24
26
 
25
-
26
27
  twitter = Twitter.new(config['email'], config['password'])
27
-
28
- twitter.timeline.each do |s|
29
- puts s.user.name, s.text, "#{s.created_at} #{s.id}", ''
30
- end
31
-
32
- # twitter.timeline(:friends, :query => {:since_id => 868482746}).each do |s|
33
- # puts s.user.name, s.text, "#{s.created_at} #{s.id}", ''
34
- # end
35
- #
28
+ pp twitter.timeline
29
+ # pp twitter.timeline(:friends, :query => {:since_id => 868482746})
30
+ # pp twitter.timeline(:friends, :query => 'since_id=868482746')
36
31
  # pp twitter.post('this is a test')
data/httparty.gemspec CHANGED
@@ -1,14 +1,14 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = %q{httparty}
3
- s.version = "0.1.0"
3
+ s.version = "0.1.1"
4
4
 
5
5
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
6
  s.authors = ["John Nunemaker"]
7
- s.date = %q{2008-07-28}
7
+ s.date = %q{2008-07-31}
8
8
  s.description = %q{Makes http fun! Also, makes consuming restful web services dead easy.}
9
9
  s.email = ["nunemaker@gmail.com"]
10
10
  s.extra_rdoc_files = ["History.txt", "License.txt", "Manifest.txt", "PostInstall.txt", "README.txt"]
11
- s.files = ["History.txt", "License.txt", "Manifest.txt", "PostInstall.txt", "README.txt", "Rakefile", "config/hoe.rb", "config/requirements.rb", "examples/aaws.rb", "examples/delicious.rb", "examples/twitter.rb", "examples/whoismyrep.rb", "httparty.gemspec", "lib/httparty.rb", "lib/httparty/core_ext.rb", "lib/httparty/core_ext/hash.rb", "lib/httparty/version.rb", "script/console", "script/destroy", "script/generate", "script/txt2html", "setup.rb", "spec/hash_spec.rb", "spec/httparty_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "tasks/deployment.rake", "tasks/environment.rake", "tasks/website.rake"]
11
+ s.files = ["History.txt", "License.txt", "Manifest.txt", "PostInstall.txt", "README.txt", "Rakefile", "config/hoe.rb", "config/requirements.rb", "examples/aaws.rb", "examples/delicious.rb", "examples/twitter.rb", "examples/whoismyrep.rb", "httparty.gemspec", "lib/httparty.rb", "lib/httparty/core_ext.rb", "lib/httparty/core_ext/hash.rb", "lib/httparty/version.rb", "script/console", "script/destroy", "script/generate", "script/txt2html", "setup.rb", "spec/hash_spec.rb", "spec/httparty_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "tasks/deployment.rake", "tasks/environment.rake", "tasks/website.rake", "website/css/common.css", "website/index.html"]
12
12
  s.has_rdoc = true
13
13
  s.homepage = %q{http://httparty.rubyforge.org}
14
14
  s.post_install_message = %q{When you HTTParty, you must party hard!}
data/lib/httparty.rb CHANGED
@@ -28,10 +28,15 @@ module HTTParty
28
28
  @base_uri = normalize_base_uri(base_uri)
29
29
  end
30
30
 
31
+ # Warning: This is not thread safe most likely and
32
+ # only works if you use one set of credentials. I
33
+ # leave it because it is convenient on some occasions.
31
34
  def basic_auth(u, p)
32
35
  @auth = {:username => u, :password => p}
33
36
  end
34
37
 
38
+ # Updates the default query string parameters
39
+ # that should be appended to each request.
35
40
  def default_params(h={})
36
41
  raise ArgumentError, 'Default params must be a hash' unless h.is_a?(Hash)
37
42
  @default_params ||= {}
@@ -83,26 +88,34 @@ module HTTParty
83
88
  @http
84
89
  end
85
90
 
91
+ # FIXME: this method is doing way to much and needs to be split up
86
92
  # options can be any or all of:
87
- # query => hash of keys/values to be converted to query string
88
- # body => string for raw post data
89
- # headers => hash of headers to send request with
93
+ # query => hash of keys/values or a query string (foo=bar&baz=poo)
94
+ # body => hash of keys/values or a query string (foo=bar&baz=poo)
95
+ # headers => hash of headers to send request with
96
+ # basic_auth => :username and :password to use as basic http authentication (overrides @auth class instance variable)
90
97
  def send_request(method, path, options={})
91
98
  raise ArgumentError, 'only get, post, put and delete methods are supported' unless %w[get post put delete].include?(method.to_s)
92
- raise ArgumentError, ':query must be a hash' if options[:query] && !options[:query].is_a?(Hash)
93
99
  raise ArgumentError, ':headers must be a hash' if options[:headers] && !options[:headers].is_a?(Hash)
100
+ raise ArgumentError, ':basic_auth must be a hash' if options[:basic_auth] && !options[:basic_auth].is_a?(Hash)
94
101
  # we always want path that begins with /
95
- path = path =~ /^(\/|https?:\/\/)/ ? path : "/#{path}"
96
- @format ||= format_from_path(path)
97
- uri = URI.parse("#{base_uri}#{path}")
98
- current_qs = uri.query ? "#{uri.query}&" : ''
99
- uri.query = current_qs + default_params.merge(options[:query] || {}).to_query
100
- klass = Net::HTTP.const_get method.to_s.downcase.capitalize
101
- request = klass.new(uri.request_uri)
102
- request.body = options[:body] unless options[:body].blank?
102
+ path = path =~ /^(\/|https?:\/\/)/ ? path : "/#{path}"
103
+ @format ||= format_from_path(path)
104
+ uri = URI.parse("#{base_uri}#{path}")
105
+ existing_query = uri.query ? "#{uri.query}&" : ''
106
+ uri.query = if options[:query].blank?
107
+ existing_query
108
+ else
109
+ existing_query + (options[:query].is_a?(Hash) ? default_params.merge(options[:query]).to_query : options[:query])
110
+ end
111
+ klass = Net::HTTP.const_get method.to_s.downcase.capitalize
112
+ request = klass.new(uri.request_uri)
113
+ request.body = options[:body].is_a?(Hash) ? options[:body].to_query : options[:body] unless options[:body].blank?
114
+ basic_auth = options.delete(:basic_auth) || @auth
103
115
  request.initialize_http_header headers.merge(options[:headers] || {})
104
- request.basic_auth(@auth[:username], @auth[:password]) if @auth
105
- response = http(uri).start() { |conn| conn.request(request) }
116
+ # note to self: self, do not put basic auth above headers because it removes basic auth
117
+ request.basic_auth(basic_auth[:username], basic_auth[:password]) if basic_auth
118
+ response = http(uri).request(request)
106
119
  parse_response(response.body)
107
120
  end
108
121
 
@@ -2,7 +2,7 @@ module HTTParty
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 0
5
+ TINY = 1
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -130,15 +130,15 @@ describe HTTParty do
130
130
  end.should raise_error(ArgumentError)
131
131
  end
132
132
 
133
- it 'should require that :query is a hash if present' do
133
+ it 'should require that :headers is a hash if present' do
134
134
  lambda do
135
- Foo.send(:send_request, 'get', '/foo', :query => 'string')
135
+ Foo.send(:send_request, 'get', '/foo', :headers => 'string')
136
136
  end.should raise_error(ArgumentError)
137
137
  end
138
138
 
139
- it 'should require that :headers is a hash if present' do
139
+ it 'should require that :basic_auth is a hash if present' do
140
140
  lambda do
141
- Foo.send(:send_request, 'get', '/foo', :headers => 'string')
141
+ Foo.send(:send_request, 'get', '/foo', :basic_auth => 'string')
142
142
  end.should raise_error(ArgumentError)
143
143
  end
144
144
  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,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 CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: httparty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-07-29 00:00:00 -04:00
12
+ date: 2008-07-31 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -65,6 +65,8 @@ files:
65
65
  - tasks/deployment.rake
66
66
  - tasks/environment.rake
67
67
  - tasks/website.rake
68
+ - website/css/common.css
69
+ - website/index.html
68
70
  has_rdoc: true
69
71
  homepage: http://httparty.rubyforge.org
70
72
  post_install_message: When you HTTParty, you must party hard!