wrest 0.0.5-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/README.rdoc +101 -0
  2. data/Rakefile +243 -0
  3. data/VERSION.yml +4 -0
  4. data/bin/jwrest +3 -0
  5. data/bin/wrest +3 -0
  6. data/bin/wrest_shell.rb +21 -0
  7. data/lib/wrest.rb +41 -0
  8. data/lib/wrest/components.rb +19 -0
  9. data/lib/wrest/components/attributes_container.rb +123 -0
  10. data/lib/wrest/components/mutators.rb +21 -0
  11. data/lib/wrest/components/mutators/base.rb +27 -0
  12. data/lib/wrest/components/mutators/xml_simple_type_caster.rb +31 -0
  13. data/lib/wrest/core_ext/hash.rb +5 -0
  14. data/lib/wrest/core_ext/hash/conversions.rb +25 -0
  15. data/lib/wrest/core_ext/string.rb +5 -0
  16. data/lib/wrest/core_ext/string/conversions.rb +23 -0
  17. data/lib/wrest/exceptions.rb +1 -0
  18. data/lib/wrest/exceptions/unsupported_content_type_exception.rb +15 -0
  19. data/lib/wrest/resource.rb +18 -0
  20. data/lib/wrest/resource/base.rb +69 -0
  21. data/lib/wrest/resource/collection.rb +12 -0
  22. data/lib/wrest/response.rb +38 -0
  23. data/lib/wrest/translators.rb +25 -0
  24. data/lib/wrest/translators/content_types.rb +20 -0
  25. data/lib/wrest/translators/json.rb +21 -0
  26. data/lib/wrest/translators/xml.rb +24 -0
  27. data/lib/wrest/uri.rb +74 -0
  28. data/lib/wrest/uri_template.rb +32 -0
  29. data/lib/wrest/version.rb +22 -0
  30. data/spec/custom_matchers/custom_matchers.rb +2 -0
  31. data/spec/rcov.opts +4 -0
  32. data/spec/spec.opts +6 -0
  33. data/spec/spec_helper.rb +18 -0
  34. data/spec/wrest/components/attributes_container_spec.rb +184 -0
  35. data/spec/wrest/components/mutators/base_spec.rb +18 -0
  36. data/spec/wrest/components/mutators/xml_simple_type_caster_spec.rb +41 -0
  37. data/spec/wrest/core_ext/hash/conversions_spec.rb +22 -0
  38. data/spec/wrest/core_ext/string/conversions_spec.rb +16 -0
  39. data/spec/wrest/resource/base_spec.rb +158 -0
  40. data/spec/wrest/response_spec.rb +21 -0
  41. data/spec/wrest/translators/xml_spec.rb +12 -0
  42. data/spec/wrest/translators_spec.rb +9 -0
  43. data/spec/wrest/uri_spec.rb +131 -0
  44. data/spec/wrest/uri_template_spec.rb +28 -0
  45. metadata +139 -0
data/README.rdoc ADDED
@@ -0,0 +1,101 @@
1
+ = Wrest
2
+
3
+ (c) Copyright 2009 {Sidu Ponnappa}[http://blog.sidu.in]. All Rights Reserved.
4
+
5
+ Wrest is a ruby REST client library which allows you to quickly build object oriented wrappers around any web service. It has two components - Wrest Core and Wrest::Resource.
6
+
7
+ == Installation
8
+
9
+ The source is available at git://github.com/kaiwren/wrest.git
10
+
11
+ To install as a Rails plugin, do <tt>script/plugin install git://github.com/kaiwren/wrest.git</tt>
12
+
13
+ To install the Wrest gem, do <tt>sudo gem install wrest</tt>
14
+
15
+ == Wrest Core
16
+
17
+ * Designed to be used as a library, not just a command line REST client
18
+ * Provides basic infrastructure (including an interactive shell) and convenient HTTP wrappers
19
+ * Makes it easy to extend and modify both serialisation, deserialisation and object creation
20
+ * Isn't coupled to Rails (usable in a pure Ruby application to consume any REST api)
21
+ * Can be used both stand alone as well as to build object oriented abstractions around web services (Wrest::Resource is an example of the latter)
22
+
23
+ === Usage: Shell
24
+
25
+ You can launch the interactive Wrest shell by running bin/wrest if you have the source or invoking <tt>wrest</tt> from your prompt if you've installed the gem.
26
+ $ wrest
27
+ >> "http://search.yahooapis.com/NewsSearchService/V1/newsSearch?appid=YahooDemo&output=json&query=India&results=3&start=1".to_uri.get
28
+
29
+ === Usage: Library
30
+
31
+ require 'rubygems'
32
+ require 'wrest'
33
+ y "http://search.yahooapis.com/NewsSearchService/V1/newsSearch".to_uri.get(
34
+ :appid => 'YahooDemo',
35
+ :output => 'xml',
36
+ :query => 'India',
37
+ :results=> '3',
38
+ :start => '1'
39
+ )
40
+ === Basic Http Calls
41
+
42
+ ==== Get
43
+
44
+ A couple of ways to get the Yahoo news as hash map (needs the JSON gem - gem install json).
45
+
46
+ * This example simply does a get on a uri and figures out the appropriate deserialiser using the content-type (in this case 'text/javascript', which uses Wrest::Translators::Json). See content_types.rb under lib/wrest/mappers/translators.
47
+ "http://search.yahooapis.com/NewsSearchService/V1/newsSearch?appid=YahooDemo&output=json&query=India&results=3&start=1".to_uri.get.deserialise
48
+
49
+ * This example does a get on a base uri with several parameters passed to it, resulting in a uri essentially the same as the one above. It also shows how you can use a custom deserialiser to produce a hash-map from the response.
50
+ "http://search.yahooapis.com/NewsSearchService/V1/newsSearch".to_uri.get(
51
+ :appid => 'YahooDemo',
52
+ :output => 'xml',
53
+ :query => 'India',
54
+ :results=> '3',
55
+ :start => '1'
56
+ ).deserialise_using(Wrest::Translators::Xml)
57
+
58
+
59
+ === Logging
60
+
61
+ The Wrest logger can be set and accessed through Wrest.logger and is configured by default to log to STDOUT. If you're using Wrest in a Rails application, you can configure logging by placing the following line in your environment file:
62
+ Wrest.logger = ActiveRecord::Base.logger
63
+
64
+ === Build
65
+
66
+ Standard options are available and can be listed using <tt>rake -T</tt>. Use rake:rcov for coverage and rake:rdoc to generate documentation.
67
+
68
+ == Documentation
69
+
70
+ Wrest RDocs can be found at http://wrest.rubyforge.org/
71
+
72
+ == Wrest::Resource
73
+
74
+ Wrest::Resource is an alternative to ActiveResource which targets Rails REST services; it is currently under development.
75
+
76
+ * Out of the box support for collections
77
+ * Out of the box support for collection pagination (including support for WillPaginate), both header based and xml attribute based
78
+ * Out of the box support for operations on all the records on the collection
79
+ * Out of the box support for nested resources
80
+ * Out of the box support for cached resources
81
+ * Out of the box support for type-casting data that comes in as parameter strings
82
+ * More natural mapping of deserialised entities to existing classes
83
+ * No communication via exceptions for http error status codes
84
+ * Better extensibility - allows access to request/response objects, avoids class variables, favours symbols over strings etc.
85
+
86
+ == Dependencies
87
+
88
+ * gems
89
+ * xmlsimple
90
+ * json
91
+ * rspec
92
+ * rcov
93
+ * active_support
94
+
95
+ == Support
96
+
97
+ This project uses Assembla for ticketing: http://www.assembla.com/spaces/wrest/tickets
98
+
99
+ == Licence
100
+
101
+ Wrest is released under the Apache 2.0 licence
data/Rakefile ADDED
@@ -0,0 +1,243 @@
1
+ # Copyright 2009 Sidu Ponnappa
2
+
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
7
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
+ # See the License for the specific language governing permissions and limitations under the License.
9
+
10
+ require 'rubygems'
11
+ gem 'rspec'
12
+ require 'rake'
13
+ require 'rake/rdoctask'
14
+ require 'spec'
15
+ require 'spec/rake/spectask'
16
+
17
+ puts "Building on Ruby #{RUBY_VERSION}, #{RUBY_RELEASE_DATE}, #{RUBY_PLATFORM}"
18
+
19
+ desc 'Default: run spec tests.'
20
+ task :default => :spec
21
+
22
+ desc "Run all specs"
23
+ Spec::Rake::SpecTask.new(:spec) do |task|
24
+ task.spec_files = FileList['spec/wrest/**/*_spec.rb']
25
+ task.spec_opts = ['--options', 'spec/spec.opts']
26
+ end
27
+
28
+ desc 'Generate documentation for Wrest'
29
+ Rake::RDocTask.new(:rdoc) do |rdoc|
30
+ rdoc.rdoc_dir = 'rdoc'
31
+ rdoc.title = 'WRest'
32
+ rdoc.options << '--line-numbers' << '--inline-source'
33
+ rdoc.rdoc_files.include('README.rdoc')
34
+ rdoc.rdoc_files.include('lib/**/*.rb')
35
+ end
36
+
37
+ begin
38
+ require 'rcov'
39
+ require 'rcov/rcovtask'
40
+ desc "Run all specs in spec directory with RCov"
41
+ Spec::Rake::SpecTask.new(:rcov) do |t|
42
+ t.spec_opts = ['--options', "spec/spec.opts"]
43
+ t.spec_files = FileList["spec/wrest/**/*_spec.rb"]
44
+ t.rcov = true
45
+ t.rcov_opts = lambda do
46
+ IO.readlines("spec/rcov.opts").map {|l| l.chomp.split " "}.flatten
47
+ end
48
+ # t.verbose = true
49
+ end
50
+ rescue LoadError
51
+ puts "Rcov not available."
52
+ end
53
+
54
+ begin
55
+ require 'jeweler'
56
+ Jeweler::Tasks.new do |gemspec|
57
+ gemspec.name = "wrest"
58
+ gemspec.summary = "REST client library for Ruby."
59
+ gemspec.description = "Wrest is a REST client library which allows you to quickly build object oriented wrappers around any web service. It has two main components - Wrest Core and Wrest::Resource."
60
+ gemspec.authors = ["Sidu Ponnappa"]
61
+ gemspec.email = "ckponnappa@gmail.com"
62
+ gemspec.homepage = "http://github.com/kaiwren/wrest"
63
+ gemspec.has_rdoc = true
64
+ gemspec.rubyforge_project = 'wrest'
65
+ gemspec.executables = ['wrest']
66
+ gemspec.require_path = "lib"
67
+ gemspec.files.exclude 'spec/wrest/meh_spec.rb'
68
+ gemspec.test_files.exclude 'spec/wrest/meh_spec.rb'
69
+ gemspec.add_dependency('activesupport', '>= 2.1.0')
70
+ gemspec.add_dependency('xml-simple', '>= 1.0.11')
71
+ case RUBY_PLATFORM
72
+ when /java/
73
+ gemspec.add_dependency('json-jruby', '>= 1.1.3')
74
+ gemspec.platform = 'java'
75
+ else
76
+ gemspec.add_dependency('json', '>= 1.1.3')
77
+ gemspec.platform = Gem::Platform::RUBY
78
+ end
79
+ end
80
+ rescue LoadError
81
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
82
+ end
83
+
84
+ begin
85
+ require 'rake/contrib/sshpublisher'
86
+ namespace :rubyforge do
87
+
88
+ desc "Release gem and RDoc documentation to RubyForge"
89
+ task :release => ["rubyforge:release:gem", "rubyforge:release:docs"]
90
+
91
+ namespace :release do
92
+ desc "Publish RDoc to RubyForge."
93
+ task :docs => [:rdoc] do
94
+ config = YAML.load(
95
+ File.read(File.expand_path('~/.rubyforge/user-config.yml'))
96
+ )
97
+
98
+ host = "#{config['username']}@rubyforge.org"
99
+ remote_dir = "/var/www/gforge-projects/wrest/"
100
+ local_dir = 'rdoc'
101
+
102
+ Rake::SshDirPublisher.new(host, remote_dir, local_dir).upload
103
+ end
104
+ end
105
+ end
106
+ rescue LoadError
107
+ puts "Rake SshDirPublisher is unavailable or your rubyforge environment is not configured."
108
+ end
109
+
110
+
111
+ namespace (:benchmark) do
112
+ desc "Create classes to be used in Wrest::Resource vs. ActiveResource"
113
+ task :setup_test_classes do
114
+ require 'active_resource'
115
+ require 'wrest'
116
+
117
+ class Ooga < Wrest::Mappers::Resource::Base;end
118
+ class Booga < ActiveResource::Base; self.site='';end
119
+ end
120
+
121
+ desc "Benchmark when objects are created each time before getting data; i.e there are few queries per instantiation"
122
+ task :create_and_get => :setup_test_classes do |t|
123
+
124
+ n = 10000
125
+ puts "Running #{n} times per report"
126
+ Benchmark.bmbm(10) do |rpt|
127
+ rpt.report("Wrest::Resource") do
128
+ n.times {
129
+ ooga = Ooga.new(:id => 5, :profession => 'Natural Magician', :enhanced_by => 'Kai Wren')
130
+ ooga.profession
131
+ ooga.profession?
132
+ ooga.enhanced_by
133
+ ooga.enhanced_by?
134
+ }
135
+ end
136
+
137
+ rpt.report("ActiveResource") do
138
+ n.times {
139
+ booga = Booga.new(:id => 5, :profession => 'Natural Magician', :enhanced_by => 'Kai Wren')
140
+ booga.profession
141
+ booga.profession?
142
+ booga.enhanced_by
143
+ booga.enhanced_by?
144
+ }
145
+ end
146
+ end
147
+ end
148
+
149
+ desc "Benchmark when objects are created beforehand; i.e there are many queries per instantiation"
150
+ task :create_once_and_get => :setup_test_classes do |t|
151
+
152
+ n = 10000
153
+ puts "Running #{n} times per report"
154
+
155
+ Benchmark.bmbm(10) do |rpt|
156
+ rpt.report("Wrest::Resource") do
157
+ ooga = Ooga.new(:id => 5, :profession => 'Natural Magician', :enhanced_by => 'Kai Wren')
158
+
159
+ n.times {
160
+ ooga.profession
161
+ ooga.profession?
162
+ ooga.enhanced_by
163
+ ooga.enhanced_by?
164
+ }
165
+ end
166
+
167
+ rpt.report("ActiveResource") do
168
+ booga = Booga.new(:id => 5, :profession => 'Natural Magician', :enhanced_by => 'Kai Wren')
169
+
170
+ n.times {
171
+ booga.profession
172
+ booga.profession?
173
+ booga.enhanced_by
174
+ booga.enhanced_by?
175
+ }
176
+ end
177
+ end
178
+ end
179
+
180
+ desc "Benchmark objects respond_to? performance without invocation"
181
+ task :responds_to_before => :setup_test_classes do |t|
182
+
183
+ n = 10000
184
+ puts "Running #{n} times per report"
185
+
186
+ Benchmark.bmbm(10) do |rpt|
187
+ rpt.report("Wrest::Resource") do
188
+ ooga = Ooga.new(:id => 5, :profession => 'Natural Magician', :enhanced_by => 'Kai Wren')
189
+
190
+ n.times {
191
+ ooga.respond_to?(:profession)
192
+ ooga.respond_to?(:profession?)
193
+ ooga.respond_to?(:profession=)
194
+ }
195
+ end
196
+
197
+ rpt.report("ActiveResource") do
198
+ booga = Booga.new(:id => 5, :profession => 'Natural Magician', :enhanced_by => 'Kai Wren')
199
+
200
+ n.times {
201
+ booga.respond_to?(:profession)
202
+ booga.respond_to?(:profession?)
203
+ booga.respond_to?(:profession=)
204
+ }
205
+ end
206
+ end
207
+ end
208
+
209
+ desc "Benchmark objects respond_to? performance after invocation"
210
+ task :responds_to_after => :setup_test_classes do |t|
211
+
212
+ n = 10000
213
+ puts "Running #{n} times per report"
214
+
215
+ Benchmark.bmbm(10) do |rpt|
216
+ rpt.report("Wrest::Resource") do
217
+ ooga = Ooga.new(:id => 5, :profession => 'Natural Magician', :enhanced_by => 'Kai Wren')
218
+ ooga.profession
219
+ ooga.profession?
220
+ ooga.profession = ''
221
+
222
+ n.times {
223
+ ooga.respond_to?(:profession)
224
+ ooga.respond_to?(:profession?)
225
+ ooga.respond_to?(:profession=)
226
+ }
227
+ end
228
+
229
+ rpt.report("ActiveResource") do
230
+ booga = Booga.new(:id => 5, :profession => 'Natural Magician', :enhanced_by => 'Kai Wren')
231
+ booga.profession
232
+ booga.profession?
233
+ booga.profession = ''
234
+
235
+ n.times {
236
+ booga.respond_to?(:profession)
237
+ booga.respond_to?(:profession?)
238
+ booga.respond_to?(:profession=)
239
+ }
240
+ end
241
+ end
242
+ end
243
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 5
3
+ :minor: 0
4
+ :major: 0
data/bin/jwrest ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env jruby
2
+
3
+ load(File.dirname(__FILE__)+'/wrest_shell.rb')
data/bin/wrest ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ load(File.dirname(__FILE__)+'/wrest_shell.rb')
@@ -0,0 +1,21 @@
1
+ puts "Ruby #{RUBY_VERSION}, #{RUBY_RELEASE_DATE}, #{RUBY_PLATFORM}"
2
+
3
+ entry_point = "#{File.dirname(__FILE__)}/../lib/wrest.rb"
4
+ version = "#{File.dirname(__FILE__)}/../lib/wrest/version"
5
+
6
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
7
+
8
+ require 'optparse'
9
+ options = { :irb => irb }
10
+ OptionParser.new do |opt|
11
+ opt.banner = "Usage: console [options]"
12
+ opt.on("--irb=[#{irb}]", 'Invoke a different irb.') { |v| options[:irb] = v }
13
+ opt.parse!(ARGV)
14
+ end
15
+
16
+ libs = " -r irb/completion"
17
+ libs << " -r #{entry_point}"
18
+
19
+ require version
20
+ puts "Loading Wrest #{Wrest::VERSION::STRING}"
21
+ exec "#{options[:irb]} #{libs} --simple-prompt"
data/lib/wrest.rb ADDED
@@ -0,0 +1,41 @@
1
+ # Copyright 2009 Sidu Ponnappa
2
+ # Licensed under the Apache License, Version 2.0 (the "License");
3
+ # you may not use this file except in compliance with the License.
4
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
5
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
6
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7
+ # See the License for the specific language governing permissions and limitations under the License.
8
+
9
+ require 'rubygems'
10
+ require 'net/http'
11
+ require 'net/https'
12
+ require 'forwardable'
13
+ require 'date'
14
+ require 'cgi'
15
+ require 'base64'
16
+ require 'logger'
17
+ require 'benchmark'
18
+ require 'active_support'
19
+
20
+ WREST_ROOT = File.dirname(__FILE__)
21
+
22
+ module Wrest
23
+ def self.logger=(logger)
24
+ @logger = logger
25
+ end
26
+
27
+ def self.logger
28
+ @logger
29
+ end
30
+ end
31
+
32
+ Wrest.logger = Logger.new(STDOUT)
33
+ Wrest.logger.level = Logger::DEBUG
34
+
35
+ source_dirs = ["/wrest/core_ext/*.rb", "/wrest/*.rb"]
36
+
37
+ source_dirs.each{|directory|
38
+ Dir["#{File.expand_path(File.dirname(__FILE__) + directory)}"].each { |file|
39
+ require file
40
+ }
41
+ }
@@ -0,0 +1,19 @@
1
+ # Copyright 2009 Sidu Ponnappa
2
+
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ # Unless required by applicable law or agreed to in writing, software distributed under the License
7
+ # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
+ # See the License for the specific language governing permissions and limitations under the License.
9
+
10
+ module Wrest #:nodoc:
11
+ # A component is a building block that can
12
+ # be used while building an object oriented wrapper
13
+ # around a REST service
14
+ module Components
15
+ end
16
+ end
17
+
18
+ require "#{WREST_ROOT}/wrest/components/attributes_container"
19
+ require "#{WREST_ROOT}/wrest/components/mutators"