patron 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,42 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Patron HTTP Client: Bootstrap script
4
+ ## Copyright (c) 2008 The Hive http://www.thehive.com/
5
+ ##
6
+ ## Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ ## of this software and associated documentation files (the "Software"), to deal
8
+ ## in the Software without restriction, including without limitation the rights
9
+ ## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ ## copies of the Software, and to permit persons to whom the Software is
11
+ ## furnished to do so, subject to the following conditions:
12
+ ##
13
+ ## The above copyright notice and this permission notice shall be included in
14
+ ## all copies or substantial portions of the Software.
15
+ ##
16
+ ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ ## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ ## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ ## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ ## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ ## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ ## THE SOFTWARE.
23
+ ##
24
+ ## -------------------------------------------------------------------
25
+ require 'yaml'
26
+ require 'pathname'
27
+
28
+ cwd = Pathname(__FILE__).dirname
29
+ $:.unshift(cwd) unless $:.include?(cwd) || $:.include?(cwd.expand_path)
30
+
31
+ require 'patron/session'
32
+
33
+ module Patron #:nodoc:
34
+ # Returns the version number of the Patron library as a string
35
+ def self.version
36
+ cwd = Pathname(__FILE__).dirname
37
+ yaml = YAML.load_file(cwd.expand_path / '../VERSION.yml')
38
+ major = (yaml['major'] || yaml[:major]).to_i
39
+ minor = (yaml['minor'] || yaml[:minor]).to_i
40
+ "#{major}.#{minor}"
41
+ end
42
+ end
@@ -0,0 +1,55 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Patron HTTP Client: Error definitions
4
+ ## Copyright (c) 2008 The Hive http://www.thehive.com/
5
+ ##
6
+ ## Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ ## of this software and associated documentation files (the "Software"), to deal
8
+ ## in the Software without restriction, including without limitation the rights
9
+ ## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ ## copies of the Software, and to permit persons to whom the Software is
11
+ ## furnished to do so, subject to the following conditions:
12
+ ##
13
+ ## The above copyright notice and this permission notice shall be included in
14
+ ## all copies or substantial portions of the Software.
15
+ ##
16
+ ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ ## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ ## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ ## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ ## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ ## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ ## THE SOFTWARE.
23
+ ##
24
+ ## -------------------------------------------------------------------
25
+
26
+ module Patron
27
+
28
+ # Base class for Patron exceptions.
29
+ class Error < StandardError; end
30
+
31
+ # The URL you passed to Patron used a protocol that it does not support.
32
+ # This most likely the result of a misspelled protocol string.
33
+ class UnsupportedProtocol < Error; end
34
+
35
+ # The URL was not properly formatted.
36
+ class URLFormatError < Error; end
37
+
38
+ # Could not resolve the remote host name.
39
+ class HostResolutionError < Error; end
40
+
41
+ # Failed to connect to the remote host.
42
+ class ConnectionFailed < Error; end
43
+
44
+ # A file transfer was shorter or larger than expected.
45
+ # This happens when the server first reports an expected transfer size,
46
+ # and then delivers data that doesn't match the previously given size.
47
+ class PartialFileError < Error; end
48
+
49
+ # Operation timeout. The specified time-out period was reached.
50
+ class TimeoutError < Error; end
51
+
52
+ # Too many redirects. When following redirects, Patron hit the maximum amount.
53
+ class TooManyRedirects < Error; end
54
+
55
+ end
@@ -0,0 +1,90 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Patron HTTP Client: Request class
4
+ ## Copyright (c) 2008 The Hive http://www.thehive.com/
5
+ ##
6
+ ## Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ ## of this software and associated documentation files (the "Software"), to deal
8
+ ## in the Software without restriction, including without limitation the rights
9
+ ## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ ## copies of the Software, and to permit persons to whom the Software is
11
+ ## furnished to do so, subject to the following conditions:
12
+ ##
13
+ ## The above copyright notice and this permission notice shall be included in
14
+ ## all copies or substantial portions of the Software.
15
+ ##
16
+ ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ ## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ ## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ ## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ ## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ ## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ ## THE SOFTWARE.
23
+ ##
24
+ ## -------------------------------------------------------------------
25
+
26
+ module Patron
27
+
28
+ # Represents the information necessary for an HTTP request.
29
+ # This is basically a data object with validation. Not all fields will be
30
+ # used in every request.
31
+ class Request
32
+
33
+ def initialize
34
+ @action = :get
35
+ @headers = {}
36
+ @timeout = 0
37
+ @connect_timeout = 0
38
+ @max_redirects = -1
39
+ end
40
+
41
+ attr_accessor :url, :username, :password, :upload_data
42
+ attr_reader :action, :timeout, :connect_timeout, :max_redirects, :headers
43
+
44
+ def action=(new_action)
45
+ if ![:get, :put, :post, :delete, :head].include?(new_action)
46
+ raise ArgumentError, "Action must be one of :get, :put, :post, :delete or :head"
47
+ end
48
+
49
+ @action = new_action
50
+ end
51
+
52
+ def timeout=(new_timeout)
53
+ if new_timeout.to_i < 1
54
+ raise ArgumentError, "Timeout must be a positive integer greater than 0"
55
+ end
56
+
57
+ @timeout = new_timeout.to_i
58
+ end
59
+
60
+ def connect_timeout=(new_timeout)
61
+ if new_timeout.to_i < 1
62
+ raise ArgumentError, "Timeout must be a positive integer greater than 0"
63
+ end
64
+
65
+ @connect_timeout = new_timeout.to_i
66
+ end
67
+
68
+ def max_redirects=(new_max_redirects)
69
+ if new_max_redirects.to_i < -1
70
+ raise ArgumentError, "Max redirects must be a positive integer, 0 or -1"
71
+ end
72
+
73
+ @max_redirects = new_max_redirects.to_i
74
+ end
75
+
76
+ def headers=(new_headers)
77
+ if !new_headers.kind_of?(Hash)
78
+ raise ArgumentError, "Headers must be a hash"
79
+ end
80
+
81
+ @headers = new_headers
82
+ end
83
+
84
+ def credentials
85
+ return nil if username.nil? || password.nil?
86
+ "#{username}:#{password}"
87
+ end
88
+
89
+ end
90
+ end
@@ -0,0 +1,57 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Patron HTTP Client: Response class
4
+ ## Copyright (c) 2008 The Hive http://www.thehive.com/
5
+ ##
6
+ ## Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ ## of this software and associated documentation files (the "Software"), to deal
8
+ ## in the Software without restriction, including without limitation the rights
9
+ ## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ ## copies of the Software, and to permit persons to whom the Software is
11
+ ## furnished to do so, subject to the following conditions:
12
+ ##
13
+ ## The above copyright notice and this permission notice shall be included in
14
+ ## all copies or substantial portions of the Software.
15
+ ##
16
+ ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ ## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ ## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ ## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ ## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ ## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ ## THE SOFTWARE.
23
+ ##
24
+ ## -------------------------------------------------------------------
25
+
26
+ module Patron
27
+
28
+ # Represents the response from the HTTP server.
29
+ class Response
30
+
31
+ def initialize
32
+ @headers = {}
33
+ end
34
+
35
+ attr_reader :url, :status, :status_line, :redirect_count, :body, :headers
36
+
37
+ def inspect
38
+ # Avoid spamming the console with the header and body data
39
+ "#<Patron::Response @status_line='#{@status_line}'>"
40
+ end
41
+
42
+ private
43
+
44
+ # Called by the C code to parse and set the headers
45
+ def parse_headers(header_data)
46
+ header_data.split(/\r\n/).each do |header|
47
+ if header =~ %r|^HTTP/1.[01]|
48
+ @status_line = header.strip
49
+ else
50
+ parts = header.split(':', 2)
51
+ @headers[parts[0]] = parts[1]
52
+ end
53
+ end
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,114 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Patron HTTP Client: Session class
4
+ ## Copyright (c) 2008 The Hive http://www.thehive.com/
5
+ ##
6
+ ## Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ ## of this software and associated documentation files (the "Software"), to deal
8
+ ## in the Software without restriction, including without limitation the rights
9
+ ## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ ## copies of the Software, and to permit persons to whom the Software is
11
+ ## furnished to do so, subject to the following conditions:
12
+ ##
13
+ ## The above copyright notice and this permission notice shall be included in
14
+ ## all copies or substantial portions of the Software.
15
+ ##
16
+ ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ ## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ ## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ ## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ ## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ ## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ ## THE SOFTWARE.
23
+ ##
24
+ ## -------------------------------------------------------------------
25
+ require 'patron/error'
26
+ require 'patron/request'
27
+ require 'patron/response'
28
+ require 'patron/session_ext'
29
+
30
+
31
+ module Patron
32
+
33
+ # This class represents multiple request/response transactions with an HTTP
34
+ # server. This is the primary API for Patron.
35
+ class Session
36
+
37
+ # HTTP connection timeout in milliseconds. Defaults to 1 second (1000 ms).
38
+ attr_accessor :connect_timeout
39
+
40
+ # HTTP transaction timeout in seconds. Defaults to 5 seconds.
41
+ attr_accessor :timeout
42
+
43
+ # Maximum number of times to follow redirects.
44
+ # Set to 0 to disable and -1 to follow all redirects (the default).
45
+ attr_accessor :max_redirects
46
+
47
+ # Prepended to the URL in all requests.
48
+ attr_accessor :base_url
49
+
50
+ # Username and password for http authentication
51
+ attr_accessor :username, :password
52
+
53
+ # Standard set of headers that are used in all requests.
54
+ attr_reader :headers
55
+
56
+ private :ext_initialize, :handle_request
57
+
58
+ # Create an instance of the Session class.
59
+ def initialize
60
+ ext_initialize
61
+ @headers = {}
62
+ @timeout = 5
63
+ @connect_timeout = 1000
64
+ @max_redirects = -1
65
+ end
66
+
67
+ # Retrieve the contents of the specified +url+ optionally sending the
68
+ # specified headers. If the +base_url+ varaible is set then it is prepended
69
+ # to the +url+ parameter. Any custom headers are merged with the contents
70
+ # of the +headers+ instance variable. The results are returned in a
71
+ # Response object.
72
+ def get(url, headers = {})
73
+ do_request(:get, url, headers)
74
+ end
75
+
76
+ # As #get but sends an HTTP HEAD request.
77
+ def head(url, headers = {})
78
+ do_request(:head, url, headers)
79
+ end
80
+
81
+ def delete(url, headers = {})
82
+ do_request(:delete, url, headers)
83
+ end
84
+
85
+ def put(url, data, headers = {})
86
+ do_request(:put, url, headers, data)
87
+ end
88
+
89
+ def post(url, data, headers = {})
90
+ do_request(:post, url, headers, data)
91
+ end
92
+
93
+ private
94
+
95
+ # Creates a new Request object from the parameters and instance variables.
96
+ def do_request(action, url, headers, data = nil)
97
+ req = Request.new
98
+ req.action = action
99
+ req.timeout = self.timeout
100
+ req.connect_timeout = self.connect_timeout
101
+ req.max_redirects = self.max_redirects
102
+ req.headers = self.headers.merge(headers)
103
+ req.username = self.username
104
+ req.password = self.password
105
+ req.upload_data = data
106
+
107
+ req.url = self.base_url.to_s + url.to_s
108
+ raise ArgumentError, "Empty URL" if req.url.empty?
109
+
110
+ handle_request(req)
111
+ end
112
+
113
+ end
114
+ end
@@ -0,0 +1,38 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Copyright (c) 2008 The Hive http://www.thehive.com/
4
+ ##
5
+ ## Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ ## of this software and associated documentation files (the "Software"), to deal
7
+ ## in the Software without restriction, including without limitation the rights
8
+ ## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ ## copies of the Software, and to permit persons to whom the Software is
10
+ ## furnished to do so, subject to the following conditions:
11
+ ##
12
+ ## The above copyright notice and this permission notice shall be included in
13
+ ## all copies or substantial portions of the Software.
14
+ ##
15
+ ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ ## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ ## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ ## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ ## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ ## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ ## THE SOFTWARE.
22
+ ##
23
+ ## -------------------------------------------------------------------
24
+ require File.dirname(__FILE__) + '/spec_helper.rb'
25
+
26
+ describe Patron do
27
+
28
+ it "should return the version number of the Patron library" do
29
+ version = Patron.version
30
+ version.should match(%r|^\d+.\d+.\d+$|)
31
+ end
32
+
33
+ it "should return the version number of the libcurl library" do
34
+ version = Patron.libcurl_version
35
+ version.should match(%r|^libcurl/\d+.\d+.\d+|)
36
+ end
37
+
38
+ end
@@ -0,0 +1,75 @@
1
+ ## -------------------------------------------------------------------
2
+ ##
3
+ ## Copyright (c) 2008 The Hive http://www.thehive.com/
4
+ ##
5
+ ## Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ ## of this software and associated documentation files (the "Software"), to deal
7
+ ## in the Software without restriction, including without limitation the rights
8
+ ## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ ## copies of the Software, and to permit persons to whom the Software is
10
+ ## furnished to do so, subject to the following conditions:
11
+ ##
12
+ ## The above copyright notice and this permission notice shall be included in
13
+ ## all copies or substantial portions of the Software.
14
+ ##
15
+ ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ ## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ ## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ ## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ ## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ ## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ ## THE SOFTWARE.
22
+ ##
23
+ ## -------------------------------------------------------------------
24
+ require File.dirname(__FILE__) + '/spec_helper.rb'
25
+
26
+
27
+ describe Patron::Request do
28
+
29
+ before(:each) do
30
+ @request = Patron::Request.new
31
+ end
32
+
33
+ describe :action do
34
+
35
+ it "should accept :get, :put, :post, :delete and :head" do
36
+ [:get, :put, :post, :delete, :head].each do |action|
37
+ lambda {@request.action = action}.should_not raise_error
38
+ end
39
+ end
40
+
41
+ it "should raise an exception when assigned a bad value" do
42
+ lambda {@request.action = :foo}.should raise_error(ArgumentError)
43
+ end
44
+
45
+ end
46
+
47
+ describe :timeout do
48
+
49
+ it "should raise an exception when assigned a negative number" do
50
+ lambda {@request.timeout = -1}.should raise_error(ArgumentError)
51
+ end
52
+
53
+ it "should raise an exception when assigned 0" do
54
+ lambda {@request.timeout = -1}.should raise_error(ArgumentError)
55
+ end
56
+
57
+ end
58
+
59
+ describe :max_redirects do
60
+
61
+ it "should raise an error when assigned an integer smaller than -1" do
62
+ lambda {@request.max_redirects = -2}.should raise_error(ArgumentError)
63
+ end
64
+
65
+ end
66
+
67
+ describe :headers do
68
+
69
+ it "should raise an error when assigned something other than a hash" do
70
+ lambda {@request.headers = :foo}.should raise_error(ArgumentError)
71
+ end
72
+
73
+ end
74
+
75
+ end