restr 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,11 @@
1
+ == 0.5.0 :: 2009-01-07
2
+
3
+ * A logger can be provided on a per-call basis, rather than just for the
4
+ Restr class as a whole.
5
+ e.g.:
6
+ kitten_logger = Logger.new('kitten.log'}
7
+ Restr.get('http://example.com/kittens/1.xml, {}, {:logger => kitten_logger})
8
+
1
9
  == 0.4.0 :: 2008-06-19
2
10
 
3
11
  * Better recognition of XML content; response MIME types ending in "/xml" and
data/README.txt CHANGED
@@ -35,7 +35,7 @@ it under the terms of the GNU Lesser General Public License as published
35
35
  by the Free Software Foundation; either version 3 of the License, or
36
36
  (at your option) any later version.
37
37
 
38
- Reststop is distributed in the hope that it will be useful,
38
+ Restr is distributed in the hope that it will be useful,
39
39
  but WITHOUT ANY WARRANTY; without even the implied warranty of
40
40
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41
41
  GNU General Public License for more details.
@@ -1,5 +1,3 @@
1
- require 'restr/version'
2
-
3
1
  AUTHOR = 'Matt Zukowski' # can also be an array of Authors
4
2
  EMAIL = "matt@roughest.net"
5
3
  DESCRIPTION = "A very simple REST client."
@@ -60,38 +60,57 @@ end
60
60
  #
61
61
  # logger = Logger.new('restr.log')
62
62
  # logger.level = Logger::DEBUG
63
- # Restr.log = logger
63
+ # Restr.logger = logger
64
64
  #
65
- # Restr will now log its activity to the given Logger. Be careful when using
65
+ # Restr will now log its activity to the given Logger.
66
+ # The default_logger can be overridden by supplying a :logger option to
67
+ # a client call:
68
+ #
69
+ # kitten_logger = Logger.new('kitten.log'}
70
+ # Restr.get('http://example.com/kittens/1.xml, {},
71
+ # {:logger => kitten_logger)
66
72
  class Restr
67
73
 
68
74
  module VERSION #:nodoc:
69
75
  MAJOR = 0
70
- MINOR = 4
76
+ MINOR = 5
71
77
  TINY = 0
72
78
 
73
79
  STRING = [MAJOR, MINOR, TINY].join('.')
74
80
  end
75
81
 
76
82
 
77
- @@log = nil
83
+ @@logger = nil
78
84
  @@request_timeout = 3.minutes
79
85
 
80
86
  cattr_accessor :request_timeout
81
87
 
82
88
  def self.logger=(logger)
83
- @@log = logger.dup
89
+ @@logger = logger.dup
84
90
  # ActiveSupport's BufferedLogger doesn't seem to support progname= :(
85
- @@log.progname = self.name if @@log.respond_to?(:progname)
91
+ @@logger.progname = self.name if @@logger.respond_to?(:progname)
92
+ end
93
+
94
+ def self.logger
95
+ @@logger
86
96
  end
87
97
 
88
98
  def self.method_missing(method, *args)
89
99
  self.do(method, args[0], args[1] || {}, args[2])
90
100
  end
91
101
 
92
- def self.do(method, url, params = {}, auth = nil)
102
+ def self.do(method, url, params = {}, options = {})
103
+ puts "METHOD: #{method.inspect}"
104
+ puts "URL: #{url.inspect}"
105
+ puts "PARAMS: #{params.inspect}"
106
+ puts "OPTIONS: #{options.inspect}"
107
+
93
108
  uri = URI.parse(url)
109
+
94
110
  params = {} unless params
111
+ options = {} unless options
112
+
113
+ logger = options[:logger] || self.logger
95
114
 
96
115
  method_mod = method.to_s.downcase.capitalize
97
116
  unless Net::HTTP.const_defined?(method_mod)
@@ -111,19 +130,16 @@ class Restr
111
130
  req = Net::HTTP.const_get(method_mod).new(uri.request_uri)
112
131
 
113
132
 
114
- if auth
115
- raise ArgumentError,
116
- "The `auth` parameter must be a Hash with a :username and :password value." unless
117
- auth.kind_of? Hash
118
- req.basic_auth auth[:username] || auth['username'], auth[:password] || auth['password']
133
+ if options[:username] || options['username']
134
+ req.basic_auth options[:username] || options['username'], options[:password] || options['password']
119
135
  end
120
136
 
121
137
  if params.kind_of?(Hash) && method_mod != 'Get' && method_mod != 'get'
122
138
  req.set_form_data(params, '&')
123
139
  end
124
140
 
125
- @@log.debug("Sending #{method.inspect} request to #{url.inspect} with data #{params.inspect}"+
126
- (auth ? " with authentication" : "")+".") if @@log
141
+ logger.debug("Sending #{method.inspect} request to #{url.inspect} with data #{params.inspect}"+
142
+ (options ? " with options" : "")+".") if logger
127
143
 
128
144
  client = Net::HTTP.new(uri.host, uri.port)
129
145
  client.use_ssl = (uri.scheme == 'https')
@@ -142,24 +158,25 @@ class Restr
142
158
  case res
143
159
  when Net::HTTPSuccess
144
160
  if res.content_type =~ /[\/+]xml$/
145
- @@log.debug("Got XML response: \n#{res.body}") if @@log
161
+ logger.debug("Got XML response: \n#{res.body}") if logger
146
162
  return XmlSimple.xml_in_string(res.body,
147
163
  'forcearray' => false,
148
164
  'keeproot' => false
149
165
  )
150
166
  else
151
- @@log.debug("Got #{res.content_type.inspect} response: \n#{res.body}") if @@log
167
+ logger.debug("Got #{res.content_type.inspect} response: \n#{res.body}") if logger
152
168
  return res.body
153
169
  end
154
170
  when TimeoutError
155
- @@log.debug(res) if @@log
171
+ logger.debug(res) if logger
156
172
  return XmlSimple.xml_in_string(res,
157
173
  'forcearray' => false,
158
174
  'keeproot' => false
159
175
  )
160
176
  else
161
177
  $LAST_ERROR_BODY = res.body # FIXME: this is dumb... need a better way of reporting errors
162
- @@log.error("Got error response '#{res.message}(#{res.code})': #{$LAST_ERROR_BODY}") if @@log
178
+ $LAST_ERROR_RESPONSE = res # this is currently unused within Restr, but may be useful for debugging
179
+ logger.error("Got error response '#{res.message}(#{res.code})': #{res.body.blank? ? '(blank response body)' : res.body}") if logger
163
180
  res.error!
164
181
  end
165
182
  end
File without changes
File without changes
@@ -11,7 +11,7 @@ end
11
11
  require 'redcloth'
12
12
  require 'syntax/convertors/html'
13
13
  require 'erb'
14
- require File.dirname(__FILE__) + '/../lib/restr/version.rb'
14
+ require File.dirname(__FILE__) + '/../lib/restr.rb'
15
15
 
16
16
  version = Restr::VERSION::STRING
17
17
  download = 'http://rubyforge.org/projects/restr'
@@ -33,83 +33,51 @@
33
33
  <h1>restr</h1>
34
34
  <div id="version" class="clickable" onclick='document.location = "http://rubyforge.org/projects/restr"; return false'>
35
35
  <p>Get Version</p>
36
- <a href="http://rubyforge.org/projects/restr" class="numbers">0.3.0</a>
36
+ <a href="http://rubyforge.org/projects/restr" class="numbers">0.5.0</a>
37
37
  </div>
38
- <h1>&#x2192; &#8216;restr&#8217;</h1>
39
-
40
-
41
- <h3>Copyright&#169; 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>
38
+ <h3>Copyright &#169; 2008 Urbacon Ltd.</h3>
39
+ <h2>What is it?</h2>
40
+ <p><b>Restr is a very simple client for <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">RESTful</a> web services.</b> <br />
48
41
  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&#8217;s <a href="http://rubyforge.org/projects/restr">RubyForge Page</a></p>
52
-
53
-
54
- <h2>Installing</h2>
55
-
56
-
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>
58
-
59
-
60
- <h2>Usage Example</h2>
61
-
62
-
63
- <p>Fetch Kitten with id 1 and print out its name and colour:</p>
64
-
65
-
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>
70
-
71
-
72
- <p>Change the Kitten&#8217;s colour and store the modification:</p>
73
-
74
-
75
- <pre><code>kitten['colour'] = 'black'
76
- kitten = Restr.put('http://example.com/kittens/1.xml', kitten)</code></pre>
77
-
78
-
79
- <h2>Further Resources</h2>
80
-
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>
85
-
86
-
87
- <h2>How to submit patches</h2>
88
-
89
-
90
- <ul>
42
+ <p>For project info and downloads please see the Restr&#8217;s <br />
43
+ <a href="http://rubyforge.org/projects/restr">RubyForge Page</a></p>
44
+ <h2>Installing</h2>
45
+ <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>
46
+ <h2>Usage Example</h2>
47
+ <p>Fetch Kitten with id 1 and print out its name and colour:</p>
48
+ <p><pre class='syntax'>
49
+ <span class="ident">require</span> <span class="punct">'</span><span class="string">restr</span><span class="punct">'</span>
50
+ <span class="ident">kitten</span> <span class="punct">=</span> <span class="constant">Restr</span><span class="punct">.</span><span class="ident">get</span><span class="punct">('</span><span class="string">http://example.com/kittens/1.xml</span><span class="punct">')</span>
51
+ <span class="ident">puts</span> <span class="ident">kitten</span><span class="punct">['</span><span class="string">name</span><span class="punct">']</span>
52
+ <span class="ident">puts</span> <span class="ident">kitten</span><span class="punct">['</span><span class="string">colour</span><span class="punct">']</span>
53
+ </pre></p>
54
+ <p>Change the Kitten&#8217;s colour and store the modification:</p>
55
+ <p><pre class='syntax'>
56
+ <span class="ident">kitten</span><span class="punct">['</span><span class="string">colour</span><span class="punct">']</span> <span class="punct">=</span> <span class="punct">'</span><span class="string">black</span><span class="punct">'</span>
57
+ <span class="ident">kitten</span> <span class="punct">=</span> <span class="constant">Restr</span><span class="punct">.</span><span class="ident">put</span><span class="punct">('</span><span class="string">http://example.com/kittens/1.xml</span><span class="punct">',</span> <span class="ident">kitten</span><span class="punct">)</span>
58
+ </pre></p>
59
+ <h2>Further Resources</h2>
60
+ <ul>
61
+ <li><a href="http://restr.rubyforge.org/rdoc/">Full <span class="caps">API</span> Documentation RDocs</a></li>
62
+ <li><a href="http://rubyforge.org/projects/reststop/">Reststop</a> &#8212; makes it easy to implement RESTful controllers in the Camping microframework</li>
63
+ <li><a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">Wikipedia Article on <span class="caps">REST</span></a></li>
64
+ </ul>
65
+ <h2>How to submit patches</h2>
66
+ <ul>
91
67
  <li>See below for author&#8217;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>
94
-
95
-
96
- <h2>License</h2>
97
-
98
-
99
- <ul>
68
+ <li>The trunk repository is <code>svn://rubyforge.org/var/svn/restr/trunk</code> for anonymous access.</li>
69
+ </ul>
70
+ <h2>License</h2>
71
+ <ul>
100
72
  <li><a href="http://www.gnu.org/licenses/lgpl.html">Lesser <span class="caps">GPL</span> Version 3</a></li>
101
- </ul>
102
-
103
-
104
- <h2>Contact</h2>
105
-
106
-
107
- <ul>
73
+ </ul>
74
+ <h2>Contact</h2>
75
+ <ul>
108
76
  <li><a href="http://rubyforge.org/users/gunark/">Author&#8217;s Rubyforge Page</a></li>
109
- <li><a href="http://rubyforge.org/forum/?group_id=5848">Restr RubyForge Forum</a></li>
110
- </ul>
77
+ <li><a href="http://rubyforge.org/forum/?group_id=5848">Restr RubyForge Forum</a></li>
78
+ </ul>
111
79
  <p class="coda">
112
- <a href="FIXME email">FIXME full name</a>, 3rd April 2008<br>
80
+ <a href="mailto:matt@roughest.net">Matt Zukowski</a>, 20th October 2008<br>
113
81
  Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
114
82
  </p>
115
83
  </div>
@@ -1,7 +1,5 @@
1
1
  h1. restr
2
2
 
3
- h1. &#x2192; 'restr'
4
-
5
3
  h3. Copyright (c) 2008 Urbacon Ltd.
6
4
 
7
5
  h2. What is it?
@@ -9,7 +7,8 @@ h2. What is it?
9
7
  <b>Restr is a very simple client for "RESTful":http://en.wikipedia.org/wiki/Representational_State_Transfer web services.</b>
10
8
  It was developed as a lightweight alternative to "ActiveResource":http://api.rubyonrails.com/files/vendor/rails/activeresource/README.html.
11
9
 
12
- For project info and downloads please see the Restr's "RubyForge Page":http://rubyforge.org/projects/restr
10
+ For project info and downloads please see the Restr's
11
+ "RubyForge Page":http://rubyforge.org/projects/restr
13
12
 
14
13
  h2. Installing
15
14
 
@@ -19,19 +18,25 @@ h2. Usage Example
19
18
 
20
19
  Fetch Kitten with id 1 and print out its name and colour:
21
20
 
22
- require 'restr'
23
- kitten = Restr.get('http://example.com/kittens/1.xml')
24
- puts kitten['name']
25
- puts kitten['colour']
26
-
21
+ <pre syntax="ruby">
22
+ require 'restr'
23
+ kitten = Restr.get('http://example.com/kittens/1.xml')
24
+ puts kitten['name']
25
+ puts kitten['colour']
26
+ </pre>
27
+
27
28
  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)
29
+
30
+ <pre syntax="ruby">
31
+ kitten['colour'] = 'black'
32
+ kitten = Restr.put('http://example.com/kittens/1.xml', kitten)
33
+ </pre>
31
34
 
32
35
  h2. Further Resources
33
36
 
34
- * "Full API Documentation (RDocs)":http://restr.rubyforge.org/rdoc/
37
+ * "Full API Documentation RDocs":http://restr.rubyforge.org/rdoc/
38
+ * "Reststop":http://rubyforge.org/projects/reststop/ -- makes it easy to implement RESTful controllers in the Camping microframework
39
+ * "Wikipedia Article on REST":http://en.wikipedia.org/wiki/Representational_State_Transfer
35
40
 
36
41
  h2. How to submit patches
37
42
 
@@ -37,7 +37,7 @@
37
37
  </div>
38
38
  <%= body %>
39
39
  <p class="coda">
40
- <a href="FIXME email">FIXME full name</a>, <%= modified.pretty %><br>
40
+ <a href="mailto:matt@roughest.net">Matt Zukowski</a>, <%= modified.pretty %><br>
41
41
  Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
42
42
  </p>
43
43
  </div>
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.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Zukowski
@@ -9,10 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-06-19 00:00:00 -04:00
12
+ date: 2009-01-07 00:00:00 -05:00
13
13
  default_executable:
14
- dependencies: []
15
-
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.8.2
24
+ version:
16
25
  description: A very simple REST client.
17
26
  email:
18
27
  - matt@roughest.net
@@ -75,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
84
  requirements: []
76
85
 
77
86
  rubyforge_project: restr
78
- rubygems_version: 1.0.1
87
+ rubygems_version: 1.3.1
79
88
  signing_key:
80
89
  specification_version: 2
81
90
  summary: A very simple REST client.