rubyrest 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,5 +1,10 @@
1
- == Release 0.0.2
1
+ == Release 0.0.3
2
+
3
+ * Added module RubyRest::Client, providing a convenience client to REST APIs deployed with Ruby-on-Rest
4
+ * Improved Atom representation of resources (feeds, entries and service documents)
5
+ * Dashboard (service document) can now be customized by developpers by implementing a class with name Dashboard.
2
6
 
7
+ == Release 0.0.2
3
8
 
4
9
  * The engine now starts a daemonized server by default
5
10
  * The engine is now embeddable in ruby code, thanks to RubyRest::Engine new implementation.
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'fileutils'
6
6
  include FileUtils
7
7
 
8
8
  NAME = "rubyrest"
9
- VERS = "0.0.2"
9
+ VERS = "0.0.3"
10
10
  CLEAN.include ['**/.*.sw?', 'pkg/*', '.config', 'doc/*', 'coverage/*']
11
11
  RDOC_OPTS = ['--quiet', '--title', "Ruby-on-Rest: A simple REST framework for Ruby",
12
12
  "--opname", "index.html",
data/lib/rubyrest/atom.rb CHANGED
@@ -19,7 +19,10 @@ module RubyRest
19
19
  HTML_TYPE = "text/html".freeze
20
20
  WORKSPACE_METHOD = "dashboard".freeze
21
21
  MODULEID = "Ruby-on-Rest (http://rubyrest.rubyforge.org)".freeze
22
-
22
+
23
+
24
+
25
+
23
26
  #
24
27
  # Formats the response as a Atom feed, Atom Entry or
25
28
  # Atom Service Document
@@ -34,7 +37,8 @@ module RubyRest
34
37
  else
35
38
  response[ "content-type" ] = ATOM_TYPE
36
39
  if @result.respond_to? "each"
37
- build_feed( @result, builder, request.request_uri, request.path, @method )
40
+ title = @property || @model
41
+ build_feed( @result, builder, request.request_uri, request.path, title )
38
42
  else build_entry( @result, builder, request.request_uri ) end
39
43
  end
40
44
  end
@@ -42,16 +46,18 @@ module RubyRest
42
46
 
43
47
  # Builds an Atom Service Document. This is a representation of the
44
48
  # user's dashboard or initial workspace.
45
- def build_service_document( workspace, builder, uri )
49
+ def build_service_document( collections, builder, uri )
46
50
  builder.service( NAMESPACES ){
47
51
  builder.workspace {
48
52
  builder.title "Dashboard"
49
- workspace.each { |w|
50
- builder.collection( { "href" => "#{uri}#{w}" } ) {
51
- builder.title "#{w}_all"
52
- builder.accept "entry"
53
- }
54
- }
53
+ if collections != nil and collections.respond_to?( :each )
54
+ collections.each { |col|
55
+ builder.collection( { "href" => "/#{col}" } ) {
56
+ builder.title col
57
+ builder.accept "entry"
58
+ }
59
+ }
60
+ end
55
61
  }
56
62
  }
57
63
  end
@@ -93,7 +99,7 @@ module RubyRest
93
99
  builder.link( "rel" => "alternate", "href" => entry_link )
94
100
 
95
101
  if object.respond_to?( :atom_related )
96
- related_entities = object.atom_related
102
+ related_entities = object.atom_related( @principal )
97
103
  if related_entities.respond_to?( :each )
98
104
  related_entities.each{ |related|
99
105
  builder.link( "rel" => "related", "href" => "#{entry_link}/#{related}", "title" => "#{related}", "type" => ATOM_TYPE )
@@ -140,6 +146,28 @@ module RubyRest
140
146
  def atom_author
141
147
  self.createdby
142
148
  end
149
+
150
+ # Adds some convenience methods to a standard REXML
151
+ # document. This class supposes that the document is an Atom
152
+ # entry
153
+ class Document < REXML::Document
154
+
155
+ # Resolves the missing method into a content property
156
+ # and returns its text value
157
+ def method_missing( name, *args )
158
+ location = "/entry/moodisland:content/#{name}"
159
+ method_name = name.to_s.chomp
160
+ if method_name == name.to_s
161
+ text( location )
162
+ else get_text( location ).value = args[0] end
163
+ end
164
+
165
+ # Shortcut for the id element contained
166
+ # within the content.
167
+ def id
168
+ text( "/entry/moodisland:content/id" )
169
+ end
170
+ end
143
171
 
144
172
  end
145
173
 
@@ -0,0 +1,72 @@
1
+ module RubyRest
2
+
3
+ # This module provides a convenient interface
4
+ # to REST services offered by Ruby-on-REST.
5
+ module Client
6
+
7
+ # Builds a headers hash, with the specified
8
+ # authorization token if not nil
9
+ def prepare_headers( auth = nil )
10
+ headers = { "Content-Type" => "text/xml; charset=utf-8" }
11
+ headers[ "token" ] = auth if auth != nil
12
+ return headers
13
+ end
14
+
15
+ # Reusable method that builds an Atom entry out of the specified hash of data
16
+ def hash2entry( data )
17
+ doc = RubyRest::Atom::Entry::Document.new
18
+ entry = REXML::Element.new( "entry", doc )
19
+ content = REXML::Element.new( "content", entry )
20
+ data.each { |name,value|
21
+ body = REXML::Element.new( name.to_s, content )
22
+ body.text = value
23
+ }
24
+ return doc
25
+ end
26
+
27
+ # Parses the response body string as a Ruby-on-Rest Entry document,
28
+ def str2entry( rsp )
29
+ begin
30
+ RubyRest::Atom::Entry::Document.new( rsp.body ) if response.body
31
+ rescue => e
32
+ return nil
33
+ end
34
+ end
35
+
36
+ # Reusable method that creates a query string
37
+ # with the specified hash of data
38
+ def to_query_string( data )
39
+ qs = "?"
40
+ params.each{ |k,v| qs << "#{k}=#{v}&" }
41
+ qs.chop!
42
+ end
43
+
44
+ # Performs a POST request and returns the raw response object
45
+ # The data (a hash of name,value pairs) is converted to an
46
+ # simplified Atom Entry
47
+ def post( server, port, path, data, token )
48
+ body = nil
49
+ body = hash2entry( data ).to_s if data != nil
50
+ headers = prepare_headers( token )
51
+ rsp = Net::HTTP.new( server, port ).post( path, body, headers )
52
+ [ rsp.code.to_i, str2entry( rsp ) ]
53
+ end
54
+
55
+ # Performs a GET request and returns the raw response object
56
+ # The data (a hash of name,value pairs) is converted to a query
57
+ # string, then concatenated to the path.
58
+ def get( server, port, path, data, token )
59
+ path << to_query_string( data ) if data
60
+ headers = prepare_headers( token )
61
+ rsp = Net::HTTP.new( server, port ).get( path, headers )
62
+ [ rsp.code.to_i, str2entry( rsp ) ]
63
+ end
64
+
65
+
66
+
67
+ end
68
+
69
+
70
+
71
+
72
+ end
@@ -24,11 +24,6 @@ module RubyRest
24
24
  if server.rubyrest.has( :authmodel )
25
25
  @authmodel = server.rubyrest[ :authmodel ]
26
26
  end
27
-
28
- if server.rubyrest.has( :dashboard )
29
- @dashboard = server.rubyrest[ :dashboard ]
30
- else @dashboard = server.rubyrest[ :servicemodel ][0] end
31
-
32
27
  end
33
28
 
34
29
  # Inspects the request, and resolve the parameters
@@ -71,7 +66,7 @@ module RubyRest
71
66
  raise WEBrick::HTTPStatus::Unauthorized
72
67
  else
73
68
  response.status = @success_code
74
- format_response( request, response ) if @result != nil
69
+ format_response( request, response )
75
70
  end
76
71
  end
77
72
 
@@ -162,9 +157,9 @@ module RubyRest
162
157
 
163
158
  # Returns the model defined by the configuration
164
159
  # option :dashboard
165
- #
166
160
  def dashboard( request )
167
- [ @dashboard ]
161
+ clazz = to_class( @servicemodule, "dashboard" )
162
+ clazz.rest_retrieve( @principal )
168
163
  end
169
164
 
170
165
  end
data/lib/rubyrest.rb CHANGED
@@ -7,7 +7,7 @@ require "extensions/all"
7
7
  require "builder"
8
8
  require "rexml/document"
9
9
  require "webrick"
10
- require "atom"
10
+
11
11
 
12
12
  dir = File.join( File.dirname( __FILE__ ), 'rubyrest' )
13
13
 
@@ -16,6 +16,7 @@ require File.join( dir, "atom" )
16
16
  require File.join( dir, "servlets" )
17
17
  require File.join( dir, "config" )
18
18
  require File.join( dir, "engine" )
19
+ require File.join( dir, "client" )
19
20
 
20
21
  module RubyRest #:nodoc:
21
22
  class << self
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.1
3
3
  specification_version: 1
4
4
  name: rubyrest
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.2
7
- date: 2007-03-30 00:00:00 +02:00
6
+ version: 0.0.3
7
+ date: 2007-04-08 00:00:00 +02:00
8
8
  summary: REST framework for Ruby.
9
9
  require_paths:
10
10
  - lib
@@ -38,6 +38,7 @@ files:
38
38
  - lib/rubyrest
39
39
  - lib/rubyrest.rb
40
40
  - lib/rubyrest/atom.rb
41
+ - lib/rubyrest/client.rb
41
42
  - lib/rubyrest/config.rb
42
43
  - lib/rubyrest/engine.rb
43
44
  - lib/rubyrest/servlets.rb