Browse files

Added :basic_auth as an option key when making a request. [#1 state:r…

…esolved]
  • Loading branch information...
1 parent 9543593 commit 3a8ad1da073c5ae61a2687a250cb17bc04f9c7c9 @jnunemaker committed Jul 31, 2008
Showing with 40 additions and 27 deletions.
  1. +5 −0 History.txt
  2. +6 −8 examples/delicious.rb
  3. +8 −14 examples/twitter.rb
  4. +15 −5 lib/httparty.rb
  5. +6 −0 spec/httparty_spec.rb
View
5 History.txt
@@ -1,3 +1,8 @@
+== 0.1.1 2008-07-30
+
+* 1 major enhancement:
+ * Added :basic_auth key for options when making a request
+
== 0.1.0 2008-07-27
* 1 major enhancement:
View
14 examples/delicious.rb
@@ -8,8 +8,8 @@ class Delicious
base_uri 'https://api.del.icio.us/v1'
format :xml
- def initialize(user, pass)
- self.class.basic_auth(user, pass)
+ def initialize(u, p)
+ @auth = {:username => u, :password => p}
end
# query params that filter the posts are:
@@ -18,23 +18,21 @@ def initialize(user, pass)
# url (optional). Filter by this url.
# ie: posts(:query => {:tag => 'ruby'})
def posts(options={})
+ options.merge!({:basic_auth => @auth})
# get posts and convert to structs so we can do .key instead of ['key'] with results
- self.class.get('/posts/get', options)['posts']['post'].map { |b| b.to_struct }
+ self.class.get('/posts/get', options)
end
# query params that filter the posts are:
# tag (optional). Filter by this tag.
# count (optional). Number of items to retrieve (Default:15, Maximum:100).
def recent(options={})
- self.class.get('/posts/recent', options)['posts']['post'].map { |b| b.to_struct }
+ options.merge!({:basic_auth => @auth})
+ self.class.get('/posts/recent', options)
end
end
delicious = Delicious.new(config['username'], config['password'])
-
pp delicious.posts(:query => {:tag => 'ruby'})
-
-puts '', '*' * 50, ''
-
pp delicious.recent
View
22 examples/twitter.rb
@@ -7,30 +7,24 @@ class Twitter
include HTTParty
base_uri 'twitter.com'
- def initialize(user, pass)
- self.class.basic_auth user, pass
+ def initialize(u, p)
+ @auth = {:username => u, :password => p}
end
# which can be :friends, :user or :public
# options[:query] can be things like since, since_id, count, etc.
def timeline(which=:friends, options={})
- self.class.get("/statuses/#{which}_timeline.xml", options)['statuses'].map { |s| s.to_struct }
+ options.merge!({:basic_auth => @auth})
+ self.class.get("/statuses/#{which}_timeline.json", options)
end
def post(text)
- self.class.post('/statuses/update.xml', :query => {:status => text})['status'].to_struct
+ options = { :query => {:status => text}, :basic_auth => @auth }
+ self.class.post('/statuses/update.json', options)
end
end
-
twitter = Twitter.new(config['email'], config['password'])
-
-twitter.timeline.each do |s|
- puts s.user.name, s.text, "#{s.created_at} #{s.id}", ''
-end
-
-# twitter.timeline(:friends, :query => {:since_id => 868482746}).each do |s|
-# puts s.user.name, s.text, "#{s.created_at} #{s.id}", ''
-# end
-#
+pp twitter.timeline
+# pp twitter.timeline(:friends, :query => {:since_id => 868482746})
# pp twitter.post('this is a test')
View
20 lib/httparty.rb
@@ -28,10 +28,15 @@ def base_uri(base_uri=nil)
@base_uri = normalize_base_uri(base_uri)
end
+ # Warning: This is not thread safe most likely and
+ # only works if you use one set of credentials. I
+ # leave it because it is convenient on some occasions.
def basic_auth(u, p)
@auth = {:username => u, :password => p}
end
+ # Updates the default query string parameters
+ # that should be appended to each request.
def default_params(h={})
raise ArgumentError, 'Default params must be a hash' unless h.is_a?(Hash)
@default_params ||= {}
@@ -83,14 +88,17 @@ def http(uri)
@http
end
+ # FIXME: this method is doing way to much and needs to be split up
# options can be any or all of:
- # query => hash of keys/values to be converted to query string
- # body => string for raw post data
- # headers => hash of headers to send request with
+ # query => hash of keys/values to be converted to query string
+ # body => string for raw post data
+ # headers => hash of headers to send request with
+ # basic_auth => :username and :password to use as basic http authentication (overrides @auth class instance variable)
def send_request(method, path, options={})
raise ArgumentError, 'only get, post, put and delete methods are supported' unless %w[get post put delete].include?(method.to_s)
raise ArgumentError, ':query must be a hash' if options[:query] && !options[:query].is_a?(Hash)
raise ArgumentError, ':headers must be a hash' if options[:headers] && !options[:headers].is_a?(Hash)
+ raise ArgumentError, ':basic_auth must be a hash' if options[:basic_auth] && !options[:basic_auth].is_a?(Hash)
# we always want path that begins with /
path = path =~ /^(\/|https?:\/\/)/ ? path : "/#{path}"
@format ||= format_from_path(path)
@@ -100,9 +108,11 @@ def send_request(method, path, options={})
klass = Net::HTTP.const_get method.to_s.downcase.capitalize
request = klass.new(uri.request_uri)
request.body = options[:body] unless options[:body].blank?
+ basic_auth = options.delete(:basic_auth) || @auth
request.initialize_http_header headers.merge(options[:headers] || {})
- request.basic_auth(@auth[:username], @auth[:password]) if @auth
- response = http(uri).start() { |conn| conn.request(request) }
+ # note to self: self, do not put basic auth above headers because it removes basic auth
+ request.basic_auth(basic_auth[:username], basic_auth[:password]) if basic_auth
+ response = http(uri).request(request)
parse_response(response.body)
end
View
6 spec/httparty_spec.rb
@@ -141,5 +141,11 @@ class FooWithHttps
Foo.send(:send_request, 'get', '/foo', :headers => 'string')
end.should raise_error(ArgumentError)
end
+
+ it 'should require that :basic_auth is a hash if present' do
+ lambda do
+ Foo.send(:send_request, 'get', '/foo', :basic_auth => 'string')
+ end.should raise_error(ArgumentError)
+ end
end
end

0 comments on commit 3a8ad1d

Please sign in to comment.