public
Rubygem
Description: Makes http fun! Also, makes consuming restful web services dead easy.
Homepage:
Clone URL: git://github.com/jnunemaker/httparty.git
Click here to lend your support to: httparty and make a donation at www.pledgie.com !
Added :basic_auth as an option key when making a request. [#1 state:resolved]
jnunemaker (author)
Wed Jul 30 21:05:36 -0700 2008
commit  3a8ad1da073c5ae61a2687a250cb17bc04f9c7c9
tree    98aa42c63b8a19176c87619ca49ac36b398e4bcb
parent  9543593d74193ae1ef7a1767a3007dc02c619066
...
 
 
 
 
 
1
2
3
...
1
2
3
4
5
6
7
8
0
@@ -1,3 +1,8 @@
0
+== 0.1.1 2008-07-30
0
+
0
+* 1 major enhancement:
0
+  * Added :basic_auth key for options when making a request
0
+
0
 == 0.1.0 2008-07-27
0
 
0
 * 1 major enhancement:
...
8
9
10
11
12
 
 
13
14
15
...
18
19
20
 
21
22
 
23
24
25
26
27
28
29
 
 
30
31
32
33
34
35
36
37
38
39
40
...
8
9
10
 
 
11
12
13
14
15
...
18
19
20
21
22
 
23
24
25
26
27
28
29
 
30
31
32
33
34
35
 
36
 
 
 
37
38
0
@@ -8,8 +8,8 @@ class Delicious
0
   base_uri 'https://api.del.icio.us/v1'
0
   format :xml
0
   
0
-  def initialize(user, pass)
0
-    self.class.basic_auth(user, pass)
0
+  def initialize(u, p)
0
+    @auth = {:username => u, :password => p}
0
   end
0
   
0
   # query params that filter the posts are:
0
@@ -18,23 +18,21 @@ class Delicious
0
   #   url (optional). Filter by this url.
0
   #   ie: posts(:query => {:tag => 'ruby'})
0
   def posts(options={})
0
+    options.merge!({:basic_auth => @auth})
0
     # get posts and convert to structs so we can do .key instead of ['key'] with results
0
-    self.class.get('/posts/get', options)['posts']['post'].map { |b| b.to_struct }
0
+    self.class.get('/posts/get', options)
0
   end
0
   
0
   # query params that filter the posts are:
0
   #   tag (optional). Filter by this tag.
0
   #   count (optional). Number of items to retrieve (Default:15, Maximum:100).
0
   def recent(options={})
0
-    self.class.get('/posts/recent', options)['posts']['post'].map { |b| b.to_struct }
0
+    options.merge!({:basic_auth => @auth})
0
+    self.class.get('/posts/recent', options)
0
   end
0
 end
0
 
0
 delicious = Delicious.new(config['username'], config['password'])
0
-
0
 pp delicious.posts(:query => {:tag => 'ruby'})
0
-
0
-puts '', '*' * 50, ''
0
-
0
 pp delicious.recent
0
 
...
7
8
9
10
11
 
 
12
13
14
15
16
17
 
 
18
19
20
21
 
 
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 
 
36
37
...
7
8
9
 
 
10
11
12
13
14
15
16
 
17
18
19
20
21
 
22
23
24
25
26
 
27
 
 
 
 
 
 
 
 
 
28
29
30
31
0
@@ -7,30 +7,24 @@ class Twitter
0
   include HTTParty
0
   base_uri 'twitter.com'
0
   
0
-  def initialize(user, pass)
0
-    self.class.basic_auth user, pass
0
+  def initialize(u, p)
0
+    @auth = {:username => u, :password => p}
0
   end
0
   
0
   # which can be :friends, :user or :public
0
   # options[:query] can be things like since, since_id, count, etc.
0
   def timeline(which=:friends, options={})
0
-    self.class.get("/statuses/#{which}_timeline.xml", options)['statuses'].map { |s| s.to_struct }
0
+    options.merge!({:basic_auth => @auth})
0
+    self.class.get("/statuses/#{which}_timeline.json", options)
0
   end
0
   
0
   def post(text)
0
-    self.class.post('/statuses/update.xml', :query => {:status => text})['status'].to_struct
0
+    options = { :query => {:status => text}, :basic_auth => @auth }
0
+    self.class.post('/statuses/update.json', options)
0
   end
0
 end
0
 
0
-
0
 twitter = Twitter.new(config['email'], config['password'])
0
-
0
-twitter.timeline.each do |s|
0
-  puts s.user.name, s.text, "#{s.created_at} #{s.id}", ''
0
-end
0
-
0
-# twitter.timeline(:friends, :query => {:since_id => 868482746}).each do |s|
0
-#   puts s.user.name, s.text, "#{s.created_at} #{s.id}", ''
0
-# end
0
-# 
0
+pp twitter.timeline
0
+# pp twitter.timeline(:friends, :query => {:since_id => 868482746})
0
 # pp twitter.post('this is a test')
0
\ No newline at end of file
...
28
29
30
 
 
 
31
32
33
34
 
 
35
36
37
...
83
84
85
 
86
87
88
89
 
 
 
 
90
91
92
93
 
94
95
96
...
100
101
102
 
103
104
105
 
 
 
106
107
108
...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
...
88
89
90
91
92
 
 
 
93
94
95
96
97
98
99
100
101
102
103
104
...
108
109
110
111
112
 
 
113
114
115
116
117
118
0
@@ -28,10 +28,15 @@ module HTTParty
0
       @base_uri = normalize_base_uri(base_uri)
0
     end
0
     
0
+    # Warning: This is not thread safe most likely and
0
+    # only works if you use one set of credentials. I
0
+    # leave it because it is convenient on some occasions.
0
     def basic_auth(u, p)
0
       @auth = {:username => u, :password => p}
0
     end
0
     
0
+    # Updates the default query string parameters
0
+    # that should be appended to each request.
0
     def default_params(h={})
0
       raise ArgumentError, 'Default params must be a hash' unless h.is_a?(Hash)
0
       @default_params ||= {}
0
@@ -83,14 +88,17 @@ module HTTParty
0
         @http
0
       end
0
       
0
+      # FIXME: this method is doing way to much and needs to be split up
0
       # options can be any or all of:
0
-      #   query   => hash of keys/values to be converted to query string
0
-      #   body    => string for raw post data
0
-      #   headers => hash of headers to send request with
0
+      #   query       => hash of keys/values to be converted to query string
0
+      #   body        => string for raw post data
0
+      #   headers     => hash of headers to send request with
0
+      #   basic_auth  => :username and :password to use as basic http authentication (overrides @auth class instance variable)
0
       def send_request(method, path, options={})
0
         raise ArgumentError, 'only get, post, put and delete methods are supported' unless %w[get post put delete].include?(method.to_s)
0
         raise ArgumentError, ':query must be a hash' if options[:query] && !options[:query].is_a?(Hash)
0
         raise ArgumentError, ':headers must be a hash' if options[:headers] && !options[:headers].is_a?(Hash)
0
+        raise ArgumentError, ':basic_auth must be a hash' if options[:basic_auth] && !options[:basic_auth].is_a?(Hash)
0
         # we always want path that begins with /
0
         path         = path =~ /^(\/|https?:\/\/)/ ? path : "/#{path}"
0
         @format    ||= format_from_path(path)
0
@@ -100,9 +108,11 @@ module HTTParty
0
         klass        = Net::HTTP.const_get method.to_s.downcase.capitalize
0
         request      = klass.new(uri.request_uri)
0
         request.body = options[:body] unless options[:body].blank?
0
+        basic_auth   = options.delete(:basic_auth) || @auth
0
         request.initialize_http_header headers.merge(options[:headers] || {})
0
-        request.basic_auth(@auth[:username], @auth[:password]) if @auth
0
-        response     = http(uri).start() { |conn| conn.request(request) }
0
+        # note to self: self, do not put basic auth above headers because it removes basic auth
0
+        request.basic_auth(basic_auth[:username], basic_auth[:password]) if basic_auth
0
+        response     = http(uri).request(request)
0
         parse_response(response.body)
0
       end
0
       
...
141
142
143
 
 
 
 
 
 
144
145
146
...
141
142
143
144
145
146
147
148
149
150
151
152
0
@@ -141,5 +141,11 @@ describe HTTParty do
0
         Foo.send(:send_request, 'get', '/foo', :headers => 'string')
0
       end.should raise_error(ArgumentError)
0
     end
0
+    
0
+    it 'should require that :basic_auth is a hash if present' do
0
+      lambda do
0
+        Foo.send(:send_request, 'get', '/foo', :basic_auth => 'string')
0
+      end.should raise_error(ArgumentError)
0
+    end
0
   end
0
 end
0
\ No newline at end of file

Comments