wadl 0.1.2 → 0.1.3.1

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.
@@ -0,0 +1,47 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of wadl, the super cheap Ruby WADL client. #
5
+ # #
6
+ # Copyright (C) 2006-2008 Leonard Richardson #
7
+ # Copyright (C) 2010 Jens Wille #
8
+ # #
9
+ # Authors: #
10
+ # Leonard Richardson <leonardr@segfault.org> (Original author) #
11
+ # Jens Wille <jens.wille@uni-koeln.de> #
12
+ # #
13
+ # wadl is free software; you can redistribute it and/or modify it under the #
14
+ # terms of the GNU General Public License as published by the Free Software #
15
+ # Foundation; either version 3 of the License, or (at your option) any later #
16
+ # version. #
17
+ # #
18
+ # wadl is distributed in the hope that it will be useful, but WITHOUT ANY #
19
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
20
+ # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more #
21
+ # details. #
22
+ # #
23
+ # You should have received a copy of the GNU General Public License along #
24
+ # with wadl. If not, see <http://www.gnu.org/licenses/>. #
25
+ # #
26
+ ###############################################################################
27
+ #++
28
+
29
+ require 'wadl'
30
+
31
+ module WADL
32
+
33
+ # A mixin for objects that contain representations
34
+
35
+ module RepresentationContainer
36
+
37
+ def find_representation_by_media_type(type)
38
+ representations.find { |r| r.mediaType == type }
39
+ end
40
+
41
+ def find_form
42
+ representations.find { |r| r.is_form_representation? }
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,73 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of wadl, the super cheap Ruby WADL client. #
5
+ # #
6
+ # Copyright (C) 2006-2008 Leonard Richardson #
7
+ # Copyright (C) 2010 Jens Wille #
8
+ # #
9
+ # Authors: #
10
+ # Leonard Richardson <leonardr@segfault.org> (Original author) #
11
+ # Jens Wille <jens.wille@uni-koeln.de> #
12
+ # #
13
+ # wadl is free software; you can redistribute it and/or modify it under the #
14
+ # terms of the GNU General Public License as published by the Free Software #
15
+ # Foundation; either version 3 of the License, or (at your option) any later #
16
+ # version. #
17
+ # #
18
+ # wadl is distributed in the hope that it will be useful, but WITHOUT ANY #
19
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
20
+ # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more #
21
+ # details. #
22
+ # #
23
+ # You should have received a copy of the GNU General Public License along #
24
+ # with wadl. If not, see <http://www.gnu.org/licenses/>. #
25
+ # #
26
+ ###############################################################################
27
+ #++
28
+
29
+ require 'cgi'
30
+ require 'wadl'
31
+
32
+ module WADL
33
+
34
+ class RepresentationFormat < HasDocs
35
+
36
+ in_document 'representation'
37
+ has_attributes :id, :mediaType, :element
38
+ has_many Param
39
+ may_be_reference
40
+
41
+ def is_form_representation?
42
+ mediaType == 'application/x-www-form-encoded' || mediaType == 'multipart/form-data'
43
+ end
44
+
45
+ # Creates a representation by plugging a set of parameters
46
+ # into a representation format.
47
+ def %(values)
48
+ unless mediaType == 'application/x-www-form-encoded'
49
+ raise "wadl.rb can't instantiate a representation of type #{mediaType}"
50
+ end
51
+
52
+ representation = []
53
+
54
+ params.each { |param|
55
+ name = param.name
56
+
57
+ if param.fixed
58
+ p_values = [param.fixed]
59
+ elsif p_values = values[name] || values[name.to_sym]
60
+ p_values = [p_values] if !param.repeating? || !p_values.respond_to?(:each) || p_values.respond_to?(:to_str)
61
+ else
62
+ raise ArgumentError, "Your proposed representation is missing a value for #{param.name}" if param.required?
63
+ end
64
+
65
+ p_values.each { |v| representation << "#{CGI::escape(name)}=#{CGI::escape(v.to_s)}" } if p_values
66
+ }
67
+
68
+ representation.join('&')
69
+ end
70
+
71
+ end
72
+
73
+ end
@@ -0,0 +1,68 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of wadl, the super cheap Ruby WADL client. #
5
+ # #
6
+ # Copyright (C) 2006-2008 Leonard Richardson #
7
+ # Copyright (C) 2010 Jens Wille #
8
+ # #
9
+ # Authors: #
10
+ # Leonard Richardson <leonardr@segfault.org> (Original author) #
11
+ # Jens Wille <jens.wille@uni-koeln.de> #
12
+ # #
13
+ # wadl is free software; you can redistribute it and/or modify it under the #
14
+ # terms of the GNU General Public License as published by the Free Software #
15
+ # Foundation; either version 3 of the License, or (at your option) any later #
16
+ # version. #
17
+ # #
18
+ # wadl is distributed in the hope that it will be useful, but WITHOUT ANY #
19
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
20
+ # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more #
21
+ # details. #
22
+ # #
23
+ # You should have received a copy of the GNU General Public License along #
24
+ # with wadl. If not, see <http://www.gnu.org/licenses/>. #
25
+ # #
26
+ ###############################################################################
27
+ #++
28
+
29
+ require 'wadl'
30
+
31
+ module WADL
32
+
33
+ class RequestFormat < HasDocs
34
+
35
+ include RepresentationContainer
36
+
37
+ in_document 'request'
38
+ has_many RepresentationFormat, Param
39
+
40
+ # Returns a URI and a set of HTTP headers for this request.
41
+ def uri(resource, args = {})
42
+ uri = resource.uri(args)
43
+
44
+ query_values = args[:query] || {}
45
+ header_values = args[:headers] || {}
46
+
47
+ params.each { |param|
48
+ name = param.name
49
+
50
+ if param.style == 'header'
51
+ value = header_values[name] || header_values[name.to_sym]
52
+ value = param % value
53
+
54
+ uri.headers[name] = value if value
55
+ else
56
+ value = query_values[name] || query_values[name.to_sym]
57
+ value = param.format(value, nil, 'query')
58
+
59
+ uri.query << value if value
60
+ end
61
+ }
62
+
63
+ uri
64
+ end
65
+
66
+ end
67
+
68
+ end
@@ -0,0 +1,163 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of wadl, the super cheap Ruby WADL client. #
5
+ # #
6
+ # Copyright (C) 2006-2008 Leonard Richardson #
7
+ # Copyright (C) 2010 Jens Wille #
8
+ # #
9
+ # Authors: #
10
+ # Leonard Richardson <leonardr@segfault.org> (Original author) #
11
+ # Jens Wille <jens.wille@uni-koeln.de> #
12
+ # #
13
+ # wadl is free software; you can redistribute it and/or modify it under the #
14
+ # terms of the GNU General Public License as published by the Free Software #
15
+ # Foundation; either version 3 of the License, or (at your option) any later #
16
+ # version. #
17
+ # #
18
+ # wadl is distributed in the hope that it will be useful, but WITHOUT ANY #
19
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
20
+ # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more #
21
+ # details. #
22
+ # #
23
+ # You should have received a copy of the GNU General Public License along #
24
+ # with wadl. If not, see <http://www.gnu.org/licenses/>. #
25
+ # #
26
+ ###############################################################################
27
+ #++
28
+
29
+ require 'set'
30
+ require 'wadl'
31
+
32
+ module WADL
33
+
34
+ class Resource < HasDocs
35
+
36
+ include ResourceContainer
37
+
38
+ in_document 'resource'
39
+ has_attributes :id, :path
40
+ has_many Resource, HTTPMethod, Param, ResourceType
41
+ may_be_reference # not conforming to spec (20090831), but tests make use of it
42
+
43
+ def initialize(*args)
44
+ super
45
+ end
46
+
47
+ def dereference_with_context(child)
48
+ ResourceAndAddress.new(child, parent.address)
49
+ end
50
+
51
+ # Returns a ResourceAndAddress object bound to this resource
52
+ # and the given query variables.
53
+ def bind(args = {})
54
+ ResourceAndAddress.new(self).bind!(args)
55
+ end
56
+
57
+ # Sets basic auth parameters
58
+ def with_basic_auth(user, pass, header = 'Authorization')
59
+ bind(:headers => { header => "Basic #{["#{user}:#{pass}"].pack('m')}" })
60
+ end
61
+
62
+ # Sets OAuth parameters
63
+ #
64
+ # Args:
65
+ # :consumer_key
66
+ # :consumer_secret
67
+ # :access_token
68
+ # :token_secret
69
+ def with_oauth(*args)
70
+ header, prefix = HTTPMethod::OAUTH_HEADER, HTTPMethod::OAUTH_PREFIX
71
+ bind(:headers => { header => "#{prefix}#{args.to_yaml}" })
72
+ end
73
+
74
+ def uri(args = {}, working_address = nil)
75
+ address(working_address).uri(args)
76
+ end
77
+
78
+ # Returns an Address object refering to this resource
79
+ def address(working_address = nil)
80
+ working_address &&= working_address.deep_copy
81
+
82
+ working_address ||= if parent.respond_to?(:base)
83
+ address = Address.new
84
+ address.path_fragments << parent.base
85
+ address
86
+ else
87
+ parent.address.deep_copy
88
+ end
89
+
90
+ working_address.path_fragments << path.dup
91
+
92
+ # Install path, query, and header parameters in the Address. These
93
+ # may override existing parameters with the same names, but if
94
+ # you've got a WADL application that works that way, you should
95
+ # have bound parameters to values earlier.
96
+ new_path_fragments = []
97
+ embedded_param_names = Set.new(Address.embedded_param_names(path))
98
+
99
+ params.each { |param|
100
+ name = param.name
101
+
102
+ if embedded_param_names.include?(name)
103
+ working_address.path_params[name] = param
104
+ else
105
+ if param.style == 'query'
106
+ working_address.query_params[name] = param
107
+ elsif param.style == 'header'
108
+ working_address.header_params[name] = param
109
+ else
110
+ new_path_fragments << param
111
+ working_address.path_params[name] = param
112
+ end
113
+ end
114
+ }
115
+
116
+ working_address.path_fragments << new_path_fragments unless new_path_fragments.empty?
117
+
118
+ working_address
119
+ end
120
+
121
+ def representation_for(http_method, request = true, all = false)
122
+ method = find_method_by_http_method(http_method)
123
+ representations = (request ? method.request : method.response).representations
124
+
125
+ all ? representations : representations[0]
126
+ end
127
+
128
+ def find_by_id(id)
129
+ id = id.to_s
130
+ resources.find { |r| r.dereference.id == id }
131
+ end
132
+
133
+ # Find HTTP methods in this resource and in the mixed-in types
134
+ def each_http_method
135
+ [self, *resource_types].each { |t| t.http_methods.each { |m| yield m } }
136
+ end
137
+
138
+ def find_method_by_id(id)
139
+ id = id.to_s
140
+ each_http_method { |m| return m if m.dereference.id == id }
141
+ end
142
+
143
+ def find_method_by_http_method(action)
144
+ action = action.to_s.downcase
145
+ each_http_method { |m| return m if m.dereference.name.downcase == action }
146
+ end
147
+
148
+ # Methods for reading or writing this resource
149
+ def self.define_http_methods(klass = self, methods = %w[get post put delete])
150
+ methods.each { |method|
151
+ klass.class_eval <<-EOT, __FILE__, __LINE__ + 1
152
+ def #{method}(*args, &block)
153
+ find_method_by_http_method(:#{method}).call(self, *args, &block)
154
+ end
155
+ EOT
156
+ }
157
+ end
158
+
159
+ define_http_methods
160
+
161
+ end
162
+
163
+ end
@@ -0,0 +1,108 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of wadl, the super cheap Ruby WADL client. #
5
+ # #
6
+ # Copyright (C) 2006-2008 Leonard Richardson #
7
+ # Copyright (C) 2010 Jens Wille #
8
+ # #
9
+ # Authors: #
10
+ # Leonard Richardson <leonardr@segfault.org> (Original author) #
11
+ # Jens Wille <jens.wille@uni-koeln.de> #
12
+ # #
13
+ # wadl is free software; you can redistribute it and/or modify it under the #
14
+ # terms of the GNU General Public License as published by the Free Software #
15
+ # Foundation; either version 3 of the License, or (at your option) any later #
16
+ # version. #
17
+ # #
18
+ # wadl is distributed in the hope that it will be useful, but WITHOUT ANY #
19
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
20
+ # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more #
21
+ # details. #
22
+ # #
23
+ # You should have received a copy of the GNU General Public License along #
24
+ # with wadl. If not, see <http://www.gnu.org/licenses/>. #
25
+ # #
26
+ ###############################################################################
27
+ #++
28
+
29
+ require 'delegate'
30
+ require 'wadl'
31
+
32
+ module WADL
33
+
34
+ # A resource bound beneath a certain address. Used to keep track of a
35
+ # path through a twisting resource hierarchy that includes references.
36
+
37
+ class ResourceAndAddress < DelegateClass(Resource)
38
+
39
+ def initialize(resource, address = nil, combine_address_with_resource = true)
40
+ @resource = resource
41
+ @address = combine_address_with_resource ? resource.address(address) : address
42
+
43
+ super(resource)
44
+ end
45
+
46
+ # The id method is not delegated, because it's the name of a
47
+ # (deprecated) built-in Ruby method. We wnat to delegate it.
48
+ def id
49
+ @resource.id
50
+ end
51
+
52
+ def to_s
53
+ inspect
54
+ end
55
+
56
+ def inspect
57
+ "ResourceAndAddress\n Resource: #{@resource}\n #{@address.inspect}"
58
+ end
59
+
60
+ def address
61
+ @address
62
+ end
63
+
64
+ def bind(*args)
65
+ ResourceAndAddress.new(@resource, @address.deep_copy, false).bind!(*args)
66
+ end
67
+
68
+ def bind!(args = {})
69
+ @address.bind!(args)
70
+ self
71
+ end
72
+
73
+ def uri(args = {})
74
+ @address.deep_copy.bind!(args).uri
75
+ end
76
+
77
+ # method_missing is to catch generated methods that don't get delegated.
78
+ def method_missing(name, *args, &block)
79
+ if @resource.respond_to?(name)
80
+ result = @resource.send(name, *args, &block)
81
+ result.is_a?(Resource) ? ResourceAndAddress.new(result, @address.dup) : result
82
+ else
83
+ super
84
+ end
85
+ end
86
+
87
+ # method_missing won't catch these guys because they were defined in
88
+ # the delegation operation.
89
+ def resource(*args, &block)
90
+ resource = @resource.resource(*args, &block)
91
+ resource && ResourceAndAddress.new(resource, @address)
92
+ end
93
+
94
+ def find_resource(*args, &block)
95
+ resource = @resource.find_resource(*args, &block)
96
+ resource && ResourceAndAddress.new(resource, @address)
97
+ end
98
+
99
+ def find_resource_by_path(*args, &block)
100
+ resource = @resource.find_resource_by_path(*args, &block)
101
+ resource && ResourceAndAddress.new(resource, @address)
102
+ end
103
+
104
+ Resource.define_http_methods(self)
105
+
106
+ end
107
+
108
+ end