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.
- data/README +1 -1
- data/Rakefile +1 -1
- data/TODO +2 -0
- data/examples/README +2 -0
- data/lib/wadl.rb +24 -1234
- data/lib/wadl/address.rb +193 -0
- data/lib/wadl/application.rb +71 -0
- data/lib/wadl/cheap_schema.rb +343 -0
- data/lib/wadl/documentation.rb +41 -0
- data/lib/wadl/fault.rb +48 -0
- data/lib/wadl/fault_format.rb +67 -0
- data/lib/wadl/has_docs.rb +49 -0
- data/lib/wadl/http_method.rb +108 -0
- data/lib/wadl/link.rb +40 -0
- data/lib/wadl/option.rb +40 -0
- data/lib/wadl/param.rb +134 -0
- data/lib/wadl/representation_container.rb +47 -0
- data/lib/wadl/representation_format.rb +73 -0
- data/lib/wadl/request_format.rb +68 -0
- data/lib/wadl/resource.rb +163 -0
- data/lib/wadl/resource_and_address.rb +108 -0
- data/lib/wadl/resource_container.rb +58 -0
- data/lib/wadl/resource_type.rb +44 -0
- data/lib/wadl/resources.rb +44 -0
- data/lib/wadl/response.rb +36 -0
- data/lib/wadl/response_format.rb +108 -0
- data/lib/wadl/uri_parts.rb +63 -0
- data/lib/wadl/version.rb +1 -1
- data/lib/wadl/xml_representation.rb +64 -0
- data/test/test_wadl.rb +7 -0
- metadata +43 -8
@@ -0,0 +1,58 @@
|
|
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 resources. If you include this, be
|
34
|
+
# sure to alias :find_resource to :find_resource_autogenerated
|
35
|
+
# beforehand.
|
36
|
+
|
37
|
+
module ResourceContainer
|
38
|
+
|
39
|
+
def resource(name_or_id)
|
40
|
+
name_or_id = name_or_id.to_s
|
41
|
+
find_resource { |r| r.id == name_or_id || r.path == name_or_id }
|
42
|
+
end
|
43
|
+
|
44
|
+
def find_resource_by_path(path, auto_dereference = nil)
|
45
|
+
path = path.to_s
|
46
|
+
find_resource(auto_dereference) { |r| r.path == path }
|
47
|
+
end
|
48
|
+
|
49
|
+
def finalize_creation
|
50
|
+
resources.each { |r|
|
51
|
+
define_singleton(r, :id, :find_resource)
|
52
|
+
define_singleton(r, :path, :find_resource_by_path)
|
53
|
+
} if resources
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,44 @@
|
|
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 type of resource. Basically a mixin of methods and params for actual
|
34
|
+
# resources.
|
35
|
+
|
36
|
+
class ResourceType < HasDocs
|
37
|
+
|
38
|
+
in_document 'resource_type'
|
39
|
+
has_attributes :id
|
40
|
+
has_many HTTPMethod, Param
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,44 @@
|
|
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 Resources < HasDocs
|
34
|
+
|
35
|
+
include ResourceContainer
|
36
|
+
|
37
|
+
in_document 'resources'
|
38
|
+
as_member 'resource_list'
|
39
|
+
has_attributes :base
|
40
|
+
has_many Resource
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,36 @@
|
|
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 Response < Struct.new(:code, :headers, :representation, :format)
|
34
|
+
end
|
35
|
+
|
36
|
+
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
|
+
begin
|
30
|
+
require 'mime/types'
|
31
|
+
rescue LoadError
|
32
|
+
end
|
33
|
+
|
34
|
+
require 'wadl'
|
35
|
+
|
36
|
+
module WADL
|
37
|
+
|
38
|
+
class ResponseFormat < HasDocs
|
39
|
+
|
40
|
+
include RepresentationContainer
|
41
|
+
|
42
|
+
in_document 'response'
|
43
|
+
has_many RepresentationFormat, FaultFormat
|
44
|
+
|
45
|
+
# Builds a service response object out of an HTTPResponse object.
|
46
|
+
def build(http_response)
|
47
|
+
# Figure out which fault or representation to use.
|
48
|
+
|
49
|
+
status = http_response.status[0]
|
50
|
+
|
51
|
+
unless response_format = faults.find { |f| f.dereference.status == status }
|
52
|
+
# Try to match the response to a response format using a media
|
53
|
+
# type.
|
54
|
+
response_media_type = http_response.content_type
|
55
|
+
response_format = representations.find { |f|
|
56
|
+
t = f.dereference.mediaType and response_media_type.index(t) == 0
|
57
|
+
}
|
58
|
+
|
59
|
+
# If an exact media type match fails, use the mime-types gem to
|
60
|
+
# match the response to a response format using the underlying
|
61
|
+
# subtype. This will match "application/xml" with "text/xml".
|
62
|
+
response_format ||= begin
|
63
|
+
mime_type = MIME::Types[response_media_type]
|
64
|
+
raw_sub_type = mime_type[0].raw_sub_type if mime_type && !mime_type.empty?
|
65
|
+
|
66
|
+
representations.find { |f|
|
67
|
+
if t = f.dereference.mediaType
|
68
|
+
response_mime_type = MIME::Types[t]
|
69
|
+
response_raw_sub_type = response_mime_type[0].raw_sub_type if response_mime_type && !response_mime_type.empty?
|
70
|
+
response_raw_sub_type == raw_sub_type
|
71
|
+
end
|
72
|
+
}
|
73
|
+
end if defined?(MIME::Types)
|
74
|
+
|
75
|
+
# If all else fails, try to find a response that specifies no
|
76
|
+
# media type. TODO: check if this would be valid WADL.
|
77
|
+
response_format ||= representations.find { |f| !f.dereference.mediaType }
|
78
|
+
end
|
79
|
+
|
80
|
+
body = http_response.read
|
81
|
+
|
82
|
+
if response_format && response_format.mediaType =~ /xml/
|
83
|
+
begin
|
84
|
+
body = REXML::Document.new(body)
|
85
|
+
|
86
|
+
# Find the appropriate element of the document
|
87
|
+
if response_format.element
|
88
|
+
# TODO: don't strip the damn namespace. I'm not very good at
|
89
|
+
# namespaces and I don't see how to deal with them here.
|
90
|
+
element = response_format.element.sub(/.*:/, '')
|
91
|
+
body = REXML::XPath.first(body, "//#{element}")
|
92
|
+
end
|
93
|
+
rescue REXML::ParseException
|
94
|
+
end
|
95
|
+
|
96
|
+
body.extend(XMLRepresentation)
|
97
|
+
body.representation_of(response_format)
|
98
|
+
end
|
99
|
+
|
100
|
+
klass = response_format.is_a?(FaultFormat) ? response_format.subclass : Response
|
101
|
+
obj = klass.new(http_response.status, http_response, body, response_format)
|
102
|
+
|
103
|
+
obj.is_a?(Exception) ? raise(obj) : obj
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
@@ -0,0 +1,63 @@
|
|
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
|
+
# Classes to keep track of the logical structure of a URI.
|
34
|
+
|
35
|
+
class URIParts < Struct.new(:uri, :query, :headers)
|
36
|
+
|
37
|
+
def to_s
|
38
|
+
qs = "#{uri.include?('?') ? '&' : '?'}#{query_string}" unless query.empty?
|
39
|
+
"#{uri}#{qs}"
|
40
|
+
end
|
41
|
+
|
42
|
+
alias_method :to_str, :to_s
|
43
|
+
|
44
|
+
def inspect
|
45
|
+
hs = " Plus headers: #{headers.inspect}" if headers
|
46
|
+
"#{to_s}#{hs}"
|
47
|
+
end
|
48
|
+
|
49
|
+
def query_string
|
50
|
+
query.join('&')
|
51
|
+
end
|
52
|
+
|
53
|
+
def hash(x)
|
54
|
+
to_str.hash
|
55
|
+
end
|
56
|
+
|
57
|
+
def ==(x)
|
58
|
+
x.respond_to?(:to_str) ? to_str == x : super
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
data/lib/wadl/version.rb
CHANGED
@@ -0,0 +1,64 @@
|
|
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 'rexml/document'
|
30
|
+
require 'wadl'
|
31
|
+
|
32
|
+
module WADL
|
33
|
+
|
34
|
+
# A module mixed in to REXML documents to make them representations in the
|
35
|
+
# WADL sense.
|
36
|
+
|
37
|
+
module XMLRepresentation
|
38
|
+
|
39
|
+
def representation_of(format)
|
40
|
+
@params = format.params
|
41
|
+
end
|
42
|
+
|
43
|
+
def lookup_param(name)
|
44
|
+
param = @params.find { |p| p.name == name }
|
45
|
+
|
46
|
+
raise ArgumentError, "No such param #{name}" unless param
|
47
|
+
raise ArgumentError, "Param #{name} has no path!" unless param.path
|
48
|
+
|
49
|
+
param
|
50
|
+
end
|
51
|
+
|
52
|
+
# Yields up each XML element for the given Param object.
|
53
|
+
def each_by_param(param_name)
|
54
|
+
REXML::XPath.each(self, lookup_param(param_name).path) { |e| yield e }
|
55
|
+
end
|
56
|
+
|
57
|
+
# Returns an XML element for the given Param object.
|
58
|
+
def get_by_param(param_name)
|
59
|
+
REXML::XPath.first(self, lookup_param(param_name).path)
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|