restr 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <loadpath>
3
+ <pathentry path="" type="src"/>
4
+ <pathentry path="org.rubypeople.rdt.launching.RUBY_CONTAINER" type="con"/>
5
+ </loadpath>
@@ -0,0 +1,17 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <projectDescription>
3
+ <name>restr</name>
4
+ <comment></comment>
5
+ <projects>
6
+ </projects>
7
+ <buildSpec>
8
+ <buildCommand>
9
+ <name>org.rubypeople.rdt.core.rubybuilder</name>
10
+ <arguments>
11
+ </arguments>
12
+ </buildCommand>
13
+ </buildSpec>
14
+ <natures>
15
+ <nature>org.rubypeople.rdt.core.rubynature</nature>
16
+ </natures>
17
+ </projectDescription>
@@ -0,0 +1,3 @@
1
+ == 0.3.0 :: 2008-04-03
2
+
3
+ * First public release -- moved out from the Reststop library into its own gem.
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
@@ -0,0 +1,25 @@
1
+ .loadpath
2
+ .project
3
+ History.txt
4
+ License.txt
5
+ Manifest.txt
6
+ README.txt
7
+ Rakefile
8
+ config/hoe.rb
9
+ config/requirements.rb
10
+ lib/restr.rb
11
+ log/debug.log
12
+ script/destroy
13
+ script/generate
14
+ script/txt2html
15
+ setup.rb
16
+ tasks/deployment.rake
17
+ tasks/environment.rake
18
+ tasks/website.rake
19
+ test/test_helper.rb
20
+ test/test_restr.rb
21
+ website/index.html
22
+ website/index.txt
23
+ website/javascripts/rounded_corners_lite.inc.js
24
+ website/stylesheets/screen.css
25
+ website/template.rhtml
@@ -0,0 +1,44 @@
1
+ = Restr
2
+
3
+ <b>Restr is a very simple client for RESTful[http://en.wikipedia.org/wiki/Representational_State_Transfer]
4
+ web services.</b> It is developed as a lightweight alternative to
5
+ ActiveResource[http://api.rubyonrails.com/files/vendor/rails/activeresource/README.html].
6
+
7
+ For project info and downloads please see http://rubyforge.org/projects/restr
8
+
9
+
10
+ *Author*:: Matt Zukowski (matt at roughest dot net)
11
+ *Copyright*:: Copyright (c) 2008 Urbacon Ltd.
12
+ *License*:: GNU Lesser General Public License Version 3
13
+
14
+
15
+
16
+ Restr is basically a wrapper around Ruby's Net::HTTP, offering
17
+ a more RESTfully meaningful interface.
18
+
19
+ See the Restr class documentation for more info, but here's a simple
20
+ example of RESTful interaction with Restr:
21
+
22
+ require 'restr'
23
+ kitten = Restr.get('http://example.com/kittens/1.xml')
24
+ puts kitten['name']
25
+ puts kitten['colour']
26
+
27
+ kitten['colour'] = 'black'
28
+ kitten = Restr.put('http://example.com/kittens/1.xml', kitten)
29
+
30
+
31
+ == License
32
+
33
+ Restr is free software; you can redistribute it and/or modify
34
+ it under the terms of the GNU Lesser General Public License as published
35
+ by the Free Software Foundation; either version 3 of the License, or
36
+ (at your option) any later version.
37
+
38
+ Reststop is distributed in the hope that it will be useful,
39
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
40
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41
+ GNU General Public License for more details.
42
+
43
+ You should have received a copy of the GNU Lesser General Public License
44
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
@@ -0,0 +1,4 @@
1
+ require 'config/requirements'
2
+ require 'config/hoe' # setup Hoe + all gem configuration
3
+
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
@@ -0,0 +1,72 @@
1
+ require 'restr/version'
2
+
3
+ AUTHOR = 'Matt Zukowski' # can also be an array of Authors
4
+ EMAIL = "matt@roughest.net"
5
+ DESCRIPTION = "A very simple REST client."
6
+ GEM_NAME = 'restr'
7
+ RUBYFORGE_PROJECT = 'restr' # The unix name for your project
8
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
9
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
10
+
11
+ @config_file = "~/.rubyforge/user-config.yml"
12
+ @config = nil
13
+ RUBYFORGE_USERNAME = "unknown"
14
+ def rubyforge_username
15
+ unless @config
16
+ begin
17
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
18
+ rescue
19
+ puts <<-EOS
20
+ ERROR: No rubyforge config file found: #{@config_file}
21
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
22
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
23
+ EOS
24
+ exit
25
+ end
26
+ end
27
+ RUBYFORGE_USERNAME.replace @config["username"]
28
+ end
29
+
30
+ ENV['NODOT'] = '1'
31
+
32
+ REV = nil
33
+ # UNCOMMENT IF REQUIRED:
34
+ #REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
35
+ VERS = Restr::VERSION::STRING + (REV ? ".#{REV}" : "")
36
+ RDOC_OPTS = ['--quiet', '--title', 'restr documentation',
37
+ "--opname", "index.html",
38
+ "--line-numbers",
39
+ "--lib",
40
+ "--main", "README",
41
+ "--inline-source"]
42
+
43
+ class Hoe
44
+ def extra_deps
45
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
46
+ @extra_deps
47
+ end
48
+ end
49
+
50
+ # Generate all the Rake tasks
51
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
52
+ hoe = Hoe.new(GEM_NAME, VERS) do |p|
53
+ p.developer(AUTHOR, EMAIL)
54
+ p.description = DESCRIPTION
55
+ p.summary = DESCRIPTION
56
+ p.url = HOMEPATH
57
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
58
+ p.test_globs = ["test/**/test_*.rb"]
59
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
60
+
61
+ # == Optional
62
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
63
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
64
+
65
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
66
+
67
+ end
68
+
69
+ CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
70
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
71
+ hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
72
+ hoe.rsync_args = '-av --delete --ignore-errors'
@@ -0,0 +1,17 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe newgem rubigen].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
16
+
17
+ require 'restr'
@@ -0,0 +1,139 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'net/http'
4
+ require 'net/https'
5
+ require 'uri'
6
+ require 'cgi'
7
+
8
+ begin
9
+ require 'xml_simple'
10
+ rescue LoadError
11
+ require 'rubygems'
12
+ require 'active_support'
13
+ begin
14
+ require 'xml_simple'
15
+ rescue LoadError
16
+ require 'xmlsimple'
17
+ end
18
+ end
19
+
20
+
21
+ # A very simple REST client, best explained by example:
22
+ #
23
+ # # Retrieve a Kitten and print its name and colour
24
+ # kitten = Restr.get('http://example.com/kittens/1.xml')
25
+ # puts kitten['name']
26
+ # puts kitten['colour']
27
+ #
28
+ # # Create a Kitten
29
+ # kitten = Restr.post('http://example.com/kittens.xml',
30
+ # :name => 'batman', :colour => 'black')
31
+ #
32
+ # # Update a Kitten
33
+ # kitten = Restr.put('http://example.com/kittens/1.xml',
34
+ # :age => '6 months')
35
+ #
36
+ # # Delete a Kitten :(
37
+ # kitten = Restr.delete('http://example.com/kittens/1.xml')
38
+ #
39
+ # # Retrieve a list of Kittens
40
+ # kittens = Restr.get('http://example.com/kittens.xml')
41
+ #
42
+ # When the response to a Restr request has content type 'text/xml', the
43
+ # response body will be parsed from XML into a nested Hash (using XmlSimple
44
+ # -- see http://xml-simple.rubyforge.org/). Otherwise the response is
45
+ # returned untouched, as a String.
46
+ #
47
+ # If the remote REST resource requires authentication (Restr only supports
48
+ # HTTP Basic authentication, for now):
49
+ #
50
+ # Restr.get('http://example.com/kittens/1.xml, {},
51
+ # {:username => 'foo', :password => 'bar'})
52
+ #
53
+ class Restr
54
+
55
+ module VERSION #:nodoc:
56
+ MAJOR = 0
57
+ MINOR = 3
58
+ TINY = 0
59
+
60
+ STRING = [MAJOR, MINOR, TINY].join('.')
61
+ end
62
+
63
+
64
+ @@log = nil
65
+
66
+ def self.logger=(logger)
67
+ @@log = logger.dup
68
+ # ActiveSupport's BufferedLogger doesn't seem to support progname= :(
69
+ @@log.progname = self.name if @@log.respond_to?(:progname)
70
+ end
71
+
72
+ def self.method_missing(method, *args)
73
+ self.do(method, args[0], args[1] || {}, args[2])
74
+ end
75
+
76
+ def self.do(method, url, params = {}, auth = nil)
77
+ uri = URI.parse(url)
78
+ params = {} unless params
79
+
80
+ method_mod = method.to_s.downcase.capitalize
81
+ unless Net::HTTP.const_defined?(method_mod)
82
+ raise InvalidRequestMethod,
83
+ "Callback method #{method.inspect} is not a valid HTTP request method."
84
+ end
85
+
86
+ if method_mod == 'Get'
87
+ q = params.collect{|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join("&")
88
+ if uri.query
89
+ uri.query += "&#{q}"
90
+ else
91
+ uri.query = q
92
+ end
93
+ end
94
+
95
+ req = Net::HTTP.const_get(method_mod).new(uri.request_uri)
96
+
97
+
98
+ if auth
99
+ raise ArgumentError,
100
+ "The `auth` parameter must be a Hash with a :username and :password value." unless
101
+ auth.kind_of? Hash
102
+ req.basic_auth auth[:username] || auth['username'], auth[:password] || auth['password']
103
+ end
104
+
105
+ unless method_mod == 'Get'
106
+ req.set_form_data(params, '&')
107
+ end
108
+
109
+ @@log.debug("Sending #{method.inspect} request to #{url.inspect} with data #{params.inspect}"+
110
+ (auth ? " with authentication" : "")+".") if @@log
111
+
112
+ client = Net::HTTP.new(uri.host, uri.port)
113
+ client.use_ssl = (uri.scheme == 'https')
114
+ res = client.start do |http|
115
+ http.request(req)
116
+ end
117
+
118
+ case res
119
+ when Net::HTTPSuccess
120
+ if res.content_type == 'text/xml'
121
+ @@log.debug("Got XML response.") if @@log
122
+ return XmlSimple.xml_in_string(res.body,
123
+ 'forcearray' => false,
124
+ 'keeproot' => false
125
+ )
126
+ else
127
+ @@log.debug("Got #{res.content_type.inspect} response.") if @@log
128
+ return res.body
129
+ end
130
+ else
131
+ $LAST_ERROR_BODY = res.body # FIXME: this is dumb... need a better way of reporting errors
132
+ @@log.error("Got error response '#{res.message}(#{res.code})': #{$LAST_ERROR_BODY}") if @@log
133
+ res.error!
134
+ end
135
+ end
136
+
137
+ class InvalidRequestMethod < Exception
138
+ end
139
+ end