restr 0.3.0 → 0.4.0
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.
- data/History.txt +14 -0
- data/lib/restr.rb +38 -8
- data/website/index.html +38 -11
- data/website/index.txt +24 -15
- metadata +2 -2
data/History.txt
CHANGED
@@ -1,3 +1,17 @@
|
|
1
|
+
== 0.4.0 :: 2008-06-19
|
2
|
+
|
3
|
+
* Better recognition of XML content; response MIME types ending in "/xml" and
|
4
|
+
"+xml" are now all parsed as XML (for example: "application/xml",
|
5
|
+
"image/svg+xml", "text/xml", etc). Note that "application/xhtml+xml" is now
|
6
|
+
considered to be XML content too, even though you may think of it as just
|
7
|
+
"HTML".
|
8
|
+
* You can set how long Restr will wait for a request to complete by setting
|
9
|
+
Restr.request_timeout = <seconds>. The default timeout is 3 minutes
|
10
|
+
(180 seconds).
|
11
|
+
* The full response body is now included in DEBUG-level log entries. Be careful
|
12
|
+
if you have the logging level set to DEBUG -- your log will now grow very
|
13
|
+
quickly.
|
14
|
+
|
1
15
|
== 0.3.0 :: 2008-04-03
|
2
16
|
|
3
17
|
* First public release -- moved out from the Reststop library into its own gem.
|
data/lib/restr.rb
CHANGED
@@ -4,6 +4,7 @@ require 'net/http'
|
|
4
4
|
require 'net/https'
|
5
5
|
require 'uri'
|
6
6
|
require 'cgi'
|
7
|
+
require 'timeout'
|
7
8
|
|
8
9
|
begin
|
9
10
|
require 'xml_simple'
|
@@ -44,17 +45,29 @@ end
|
|
44
45
|
# -- see http://xml-simple.rubyforge.org/). Otherwise the response is
|
45
46
|
# returned untouched, as a String.
|
46
47
|
#
|
48
|
+
#
|
49
|
+
# === Authentication
|
50
|
+
#
|
47
51
|
# If the remote REST resource requires authentication (Restr only supports
|
48
52
|
# HTTP Basic authentication, for now):
|
49
53
|
#
|
50
54
|
# Restr.get('http://example.com/kittens/1.xml, {},
|
51
55
|
# {:username => 'foo', :password => 'bar'})
|
52
56
|
#
|
57
|
+
# === Logging
|
58
|
+
#
|
59
|
+
# A standard Ruby Logger can be attached to the Restr client like so:
|
60
|
+
#
|
61
|
+
# logger = Logger.new('restr.log')
|
62
|
+
# logger.level = Logger::DEBUG
|
63
|
+
# Restr.log = logger
|
64
|
+
#
|
65
|
+
# Restr will now log its activity to the given Logger. Be careful when using
|
53
66
|
class Restr
|
54
67
|
|
55
68
|
module VERSION #:nodoc:
|
56
69
|
MAJOR = 0
|
57
|
-
MINOR =
|
70
|
+
MINOR = 4
|
58
71
|
TINY = 0
|
59
72
|
|
60
73
|
STRING = [MAJOR, MINOR, TINY].join('.')
|
@@ -62,6 +75,9 @@ class Restr
|
|
62
75
|
|
63
76
|
|
64
77
|
@@log = nil
|
78
|
+
@@request_timeout = 3.minutes
|
79
|
+
|
80
|
+
cattr_accessor :request_timeout
|
65
81
|
|
66
82
|
def self.logger=(logger)
|
67
83
|
@@log = logger.dup
|
@@ -102,7 +118,7 @@ class Restr
|
|
102
118
|
req.basic_auth auth[:username] || auth['username'], auth[:password] || auth['password']
|
103
119
|
end
|
104
120
|
|
105
|
-
|
121
|
+
if params.kind_of?(Hash) && method_mod != 'Get' && method_mod != 'get'
|
106
122
|
req.set_form_data(params, '&')
|
107
123
|
end
|
108
124
|
|
@@ -111,26 +127,40 @@ class Restr
|
|
111
127
|
|
112
128
|
client = Net::HTTP.new(uri.host, uri.port)
|
113
129
|
client.use_ssl = (uri.scheme == 'https')
|
114
|
-
|
115
|
-
|
130
|
+
|
131
|
+
timeout = Restr.request_timeout
|
132
|
+
client.read_timeout = timeout
|
133
|
+
|
134
|
+
begin
|
135
|
+
res = client.start do |http|
|
136
|
+
http.request(req)
|
137
|
+
end
|
138
|
+
rescue Timeout::Error
|
139
|
+
res = TimeoutError, "Request timed out after #{timeout} seconds."
|
116
140
|
end
|
117
141
|
|
118
142
|
case res
|
119
143
|
when Net::HTTPSuccess
|
120
|
-
if res.content_type
|
121
|
-
@@log.debug("Got XML response.") if @@log
|
144
|
+
if res.content_type =~ /[\/+]xml$/
|
145
|
+
@@log.debug("Got XML response: \n#{res.body}") if @@log
|
122
146
|
return XmlSimple.xml_in_string(res.body,
|
123
147
|
'forcearray' => false,
|
124
148
|
'keeproot' => false
|
125
149
|
)
|
126
150
|
else
|
127
|
-
@@log.debug("Got #{res.content_type.inspect} response.") if @@log
|
151
|
+
@@log.debug("Got #{res.content_type.inspect} response: \n#{res.body}") if @@log
|
128
152
|
return res.body
|
129
153
|
end
|
154
|
+
when TimeoutError
|
155
|
+
@@log.debug(res) if @@log
|
156
|
+
return XmlSimple.xml_in_string(res,
|
157
|
+
'forcearray' => false,
|
158
|
+
'keeproot' => false
|
159
|
+
)
|
130
160
|
else
|
131
161
|
$LAST_ERROR_BODY = res.body # FIXME: this is dumb... need a better way of reporting errors
|
132
162
|
@@log.error("Got error response '#{res.message}(#{res.code})': #{$LAST_ERROR_BODY}") if @@log
|
133
|
-
res.error!
|
163
|
+
res.error!
|
134
164
|
end
|
135
165
|
end
|
136
166
|
|
data/website/index.html
CHANGED
@@ -38,7 +38,17 @@
|
|
38
38
|
<h1>→ ‘restr’</h1>
|
39
39
|
|
40
40
|
|
41
|
-
<
|
41
|
+
<h3>Copyright© 2008 Urbacon Ltd.</h3>
|
42
|
+
|
43
|
+
|
44
|
+
<h2>What is it?</h2>
|
45
|
+
|
46
|
+
|
47
|
+
<p><b>Restr is a very simple client for <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">RESTful</a> web services.</b>
|
48
|
+
It was developed as a lightweight alternative to <a href="http://api.rubyonrails.com/files/vendor/rails/activeresource/README.html">ActiveResource</a>.</p>
|
49
|
+
|
50
|
+
|
51
|
+
<p>For project info and downloads please see the Restr’s <a href="http://rubyforge.org/projects/restr">RubyForge Page</a></p>
|
42
52
|
|
43
53
|
|
44
54
|
<h2>Installing</h2>
|
@@ -47,40 +57,57 @@
|
|
47
57
|
<p><pre class='syntax'><span class="ident">sudo</span> <span class="ident">gem</span> <span class="ident">install</span> <span class="ident">restr</span></pre></p>
|
48
58
|
|
49
59
|
|
50
|
-
<h2>
|
60
|
+
<h2>Usage Example</h2>
|
51
61
|
|
52
62
|
|
53
|
-
<
|
63
|
+
<p>Fetch Kitten with id 1 and print out its name and colour:</p>
|
54
64
|
|
55
65
|
|
56
|
-
<
|
66
|
+
<pre><code>require 'restr'
|
67
|
+
kitten = Restr.get('http://example.com/kittens/1.xml')
|
68
|
+
puts kitten['name']
|
69
|
+
puts kitten['colour']</code></pre>
|
57
70
|
|
58
71
|
|
59
|
-
<p
|
72
|
+
<p>Change the Kitten’s colour and store the modification:</p>
|
60
73
|
|
61
74
|
|
62
|
-
<
|
75
|
+
<pre><code>kitten['colour'] = 'black'
|
76
|
+
kitten = Restr.put('http://example.com/kittens/1.xml', kitten)</code></pre>
|
63
77
|
|
64
78
|
|
65
|
-
<h2>
|
79
|
+
<h2>Further Resources</h2>
|
80
|
+
|
66
81
|
|
82
|
+
<ul>
|
83
|
+
<li><a href="http://restr.rubyforge.org/rdoc/" title="RDocs">Full <span class="caps">API</span> Documentation</a></li>
|
84
|
+
</ul>
|
67
85
|
|
68
|
-
|
86
|
+
|
87
|
+
<h2>How to submit patches</h2>
|
69
88
|
|
70
89
|
|
71
|
-
<
|
90
|
+
<ul>
|
91
|
+
<li>See below for author’s contact info.</li>
|
92
|
+
<li>The trunk repository is <code>svn://rubyforge.org/var/svn/restr/trunk</code> for anonymous access.</li>
|
93
|
+
</ul>
|
72
94
|
|
73
95
|
|
74
96
|
<h2>License</h2>
|
75
97
|
|
76
98
|
|
77
|
-
<
|
99
|
+
<ul>
|
100
|
+
<li><a href="http://www.gnu.org/licenses/lgpl.html">Lesser <span class="caps">GPL</span> Version 3</a></li>
|
101
|
+
</ul>
|
78
102
|
|
79
103
|
|
80
104
|
<h2>Contact</h2>
|
81
105
|
|
82
106
|
|
83
|
-
<
|
107
|
+
<ul>
|
108
|
+
<li><a href="http://rubyforge.org/users/gunark/">Author’s Rubyforge Page</a></li>
|
109
|
+
<li><a href="http://rubyforge.org/forum/?group_id=5848">Restr RubyForge Forum</a></li>
|
110
|
+
</ul>
|
84
111
|
<p class="coda">
|
85
112
|
<a href="FIXME email">FIXME full name</a>, 3rd April 2008<br>
|
86
113
|
Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
|
data/website/index.txt
CHANGED
@@ -2,38 +2,47 @@ h1. restr
|
|
2
2
|
|
3
3
|
h1. → 'restr'
|
4
4
|
|
5
|
+
h3. Copyright (c) 2008 Urbacon Ltd.
|
5
6
|
|
6
|
-
h2. What
|
7
|
+
h2. What is it?
|
7
8
|
|
9
|
+
<b>Restr is a very simple client for "RESTful":http://en.wikipedia.org/wiki/Representational_State_Transfer web services.</b>
|
10
|
+
It was developed as a lightweight alternative to "ActiveResource":http://api.rubyonrails.com/files/vendor/rails/activeresource/README.html.
|
11
|
+
|
12
|
+
For project info and downloads please see the Restr's "RubyForge Page":http://rubyforge.org/projects/restr
|
8
13
|
|
9
14
|
h2. Installing
|
10
15
|
|
11
16
|
<pre syntax="ruby">sudo gem install restr</pre>
|
12
17
|
|
13
|
-
h2.
|
14
|
-
|
15
|
-
|
16
|
-
h2. Demonstration of usage
|
18
|
+
h2. Usage Example
|
17
19
|
|
20
|
+
Fetch Kitten with id 1 and print out its name and colour:
|
18
21
|
|
22
|
+
require 'restr'
|
23
|
+
kitten = Restr.get('http://example.com/kittens/1.xml')
|
24
|
+
puts kitten['name']
|
25
|
+
puts kitten['colour']
|
26
|
+
|
27
|
+
Change the Kitten's colour and store the modification:
|
28
|
+
|
29
|
+
kitten['colour'] = 'black'
|
30
|
+
kitten = Restr.put('http://example.com/kittens/1.xml', kitten)
|
19
31
|
|
20
|
-
h2.
|
32
|
+
h2. Further Resources
|
21
33
|
|
22
|
-
"
|
23
|
-
|
24
|
-
TODO - create Google Group - restr
|
34
|
+
* "Full API Documentation (RDocs)":http://restr.rubyforge.org/rdoc/
|
25
35
|
|
26
36
|
h2. How to submit patches
|
27
37
|
|
28
|
-
|
29
|
-
|
30
|
-
The trunk repository is <code>svn://rubyforge.org/var/svn/restr/trunk</code> for anonymous access.
|
38
|
+
* See below for author's contact info.
|
39
|
+
* The trunk repository is <code>svn://rubyforge.org/var/svn/restr/trunk</code> for anonymous access.
|
31
40
|
|
32
41
|
h2. License
|
33
42
|
|
34
|
-
|
43
|
+
* "Lesser GPL Version 3":http://www.gnu.org/licenses/lgpl.html
|
35
44
|
|
36
45
|
h2. Contact
|
37
46
|
|
38
|
-
|
39
|
-
|
47
|
+
* "Author's Rubyforge Page":http://rubyforge.org/users/gunark/
|
48
|
+
* "Restr RubyForge Forum":http://rubyforge.org/forum/?group_id=5848
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Zukowski
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-06-19 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|