patron 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,6 +27,7 @@
27
27
 
28
28
  static VALUE mPatron = Qnil;
29
29
  static VALUE cSession = Qnil;
30
+ static VALUE cRequest = Qnil;
30
31
  static VALUE ePatronError = Qnil;
31
32
  static VALUE eUnsupportedProtocol = Qnil;
32
33
  static VALUE eURLFormatError = Qnil;
@@ -270,7 +271,7 @@ static void set_options_from_request(VALUE self, VALUE request) {
270
271
 
271
272
  VALUE credentials = rb_funcall(request, rb_intern("credentials"), 0);
272
273
  if (!NIL_P(credentials)) {
273
- curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
274
+ curl_easy_setopt(curl, CURLOPT_HTTPAUTH, rb_iv_get(request, "@auth_type"));
274
275
  curl_easy_setopt(curl, CURLOPT_USERPWD, StringValuePtr(credentials));
275
276
  }
276
277
  }
@@ -398,14 +399,21 @@ void Init_session_ext() {
398
399
  ePartialFileError = rb_const_get(mPatron, rb_intern("PartialFileError"));
399
400
  eTimeoutError = rb_const_get(mPatron, rb_intern("TimeoutError"));
400
401
  eTooManyRedirects = rb_const_get(mPatron, rb_intern("TooManyRedirects"));
402
+
401
403
 
402
404
  rb_define_module_function(mPatron, "libcurl_version", libcurl_version, 0);
403
405
 
404
406
  cSession = rb_define_class_under(mPatron, "Session", rb_cObject);
407
+ cRequest = rb_define_class_under(mPatron, "Request", rb_cObject);
405
408
  rb_define_alloc_func(cSession, session_alloc);
406
409
 
407
410
  rb_define_method(cSession, "ext_initialize", session_ext_initialize, 0);
408
411
  rb_define_method(cSession, "escape", session_escape, 1);
409
412
  rb_define_method(cSession, "unescape", session_unescape, 1);
410
413
  rb_define_method(cSession, "handle_request", session_handle_request, 1);
414
+
415
+ rb_define_const(cRequest, "AuthBasic", CURLAUTH_BASIC);
416
+ rb_define_const(cRequest, "AuthDigest", CURLAUTH_DIGEST);
417
+ rb_define_const(cRequest, "AuthAny", CURLAUTH_ANY);
418
+
411
419
  }
@@ -40,8 +40,44 @@ module Patron
40
40
  @max_redirects = -1
41
41
  end
42
42
 
43
- attr_accessor :url, :username, :password, :upload_data, :file_name, :proxy
43
+ attr_accessor :url, :username, :password, :file_name, :proxy, :auth_type
44
44
  attr_reader :action, :timeout, :connect_timeout, :max_redirects, :headers
45
+ attr_reader :auth_type
46
+
47
+ # Set the type of authentication to use for this request.
48
+ #
49
+ # @param [String, Symbol] type - The type of authentication to use for this request, can be one of
50
+ # :basic, :digest, or :any
51
+ #
52
+ # @example
53
+ # sess.username = "foo"
54
+ # sess.password = "sekrit"
55
+ # sess.auth_type = :digest
56
+ def auth_type=(type=:basic)
57
+ @auth_type = case type
58
+ when :basic, "basic"
59
+ Request::AuthBasic
60
+ when :digest, "digest"
61
+ Request::AuthDigest
62
+ when :any, "any"
63
+ Request::AuthAny
64
+ else
65
+ raise "#{type.inspect} is an unknown authentication type"
66
+ end
67
+ end
68
+
69
+ def upload_data=(data)
70
+ @upload_data = case data
71
+ when Hash:
72
+ hash_to_string(data)
73
+ else
74
+ data
75
+ end
76
+ end
77
+
78
+ def upload_data
79
+ @upload_data
80
+ end
45
81
 
46
82
  def action=(new_action)
47
83
  if !VALID_ACTIONS.include?(new_action)
@@ -92,5 +128,20 @@ module Patron
92
128
  "#{username}:#{password}"
93
129
  end
94
130
 
131
+ private
132
+
133
+ # serialize hash for Rails-style params
134
+ def hash_to_string(hash)
135
+ pairs = []
136
+ recursive = Proc.new do |h, prefix|
137
+ h.each_pair do |k,v|
138
+ key = prefix == '' ? k : "#{prefix}[#{k}]"
139
+ v.is_a?(Hash) ? recursive.call(v, key) : pairs << "#{key}=#{v}"
140
+ end
141
+ end
142
+ recursive.call(hash, '')
143
+ return pairs.join('&')
144
+ end
145
+
95
146
  end
96
147
  end
@@ -55,6 +55,10 @@ module Patron
55
55
 
56
56
  # Standard set of headers that are used in all requests.
57
57
  attr_reader :headers
58
+
59
+ # Set the authentication type for the request.
60
+ # @see Patron::Request#auth_type
61
+ attr_accessor :auth_type
58
62
 
59
63
  private :ext_initialize, :handle_request
60
64
 
@@ -65,6 +69,7 @@ module Patron
65
69
  @timeout = 5
66
70
  @connect_timeout = 1000
67
71
  @max_redirects = -1
72
+ @auth_type = :basic
68
73
  end
69
74
 
70
75
  ###################################################################
@@ -116,7 +121,7 @@ module Patron
116
121
  # Uploads the contents of a file to the specified +url+ using HTTP POST.
117
122
  def post_file(url, filename, headers = {})
118
123
  request(:post, url, headers, :file => filename)
119
- end
124
+ end
120
125
 
121
126
  ###################################################################
122
127
  ### WebDAV methods
@@ -145,6 +150,7 @@ module Patron
145
150
  req.upload_data = options[:data]
146
151
  req.file_name = options[:file]
147
152
  req.proxy = proxy
153
+ req.auth_type = auth_type
148
154
 
149
155
  req.url = self.base_url.to_s + url.to_s
150
156
  raise ArgumentError, "Empty URL" if req.url.empty?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: patron
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Phillip Toland
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-20 00:00:00 -05:00
12
+ date: 2009-08-10 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -71,7 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
71
  requirements: []
72
72
 
73
73
  rubyforge_project: patron
74
- rubygems_version: 1.3.2
74
+ rubygems_version: 1.3.5
75
75
  signing_key:
76
76
  specification_version: 3
77
77
  summary: Patron HTTP client