wadl 0.2.8 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@
4
4
  # A component of wadl, the super cheap Ruby WADL client. #
5
5
  # #
6
6
  # Copyright (C) 2006-2008 Leonard Richardson #
7
- # Copyright (C) 2010-2013 Jens Wille #
7
+ # Copyright (C) 2010-2014 Jens Wille #
8
8
  # #
9
9
  # Authors: #
10
10
  # Leonard Richardson <leonardr@segfault.org> (Original author) #
@@ -26,8 +26,6 @@
26
26
  ###############################################################################
27
27
  #++
28
28
 
29
- require 'wadl'
30
-
31
29
  module WADL
32
30
 
33
31
  class Documentation < CheapSchema
data/lib/wadl/fault.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  # A component of wadl, the super cheap Ruby WADL client. #
5
5
  # #
6
6
  # Copyright (C) 2006-2008 Leonard Richardson #
7
- # Copyright (C) 2010-2013 Jens Wille #
7
+ # Copyright (C) 2010-2014 Jens Wille #
8
8
  # #
9
9
  # Authors: #
10
10
  # Leonard Richardson <leonardr@segfault.org> (Original author) #
@@ -26,8 +26,6 @@
26
26
  ###############################################################################
27
27
  #++
28
28
 
29
- require 'wadl'
30
-
31
29
  module WADL
32
30
 
33
31
  class Fault < Exception
@@ -4,7 +4,7 @@
4
4
  # A component of wadl, the super cheap Ruby WADL client. #
5
5
  # #
6
6
  # Copyright (C) 2006-2008 Leonard Richardson #
7
- # Copyright (C) 2010-2013 Jens Wille #
7
+ # Copyright (C) 2010-2014 Jens Wille #
8
8
  # #
9
9
  # Authors: #
10
10
  # Leonard Richardson <leonardr@segfault.org> (Original author) #
@@ -26,8 +26,6 @@
26
26
  ###############################################################################
27
27
  #++
28
28
 
29
- require 'wadl'
30
-
31
29
  module WADL
32
30
 
33
31
  class FaultFormat < RepresentationFormat
@@ -50,9 +48,9 @@ module WADL
50
48
 
51
49
  me.subclass = if name = me.attributes['id']
52
50
  begin
53
- WADL::Faults.const_defined?(name) ?
54
- WADL::Faults.const_get(name) :
55
- WADL::Faults.const_set(name, Class.new(Fault))
51
+ Faults.const_defined?(name) ?
52
+ Faults.const_get(name) :
53
+ Faults.const_set(name, Class.new(Fault))
56
54
  rescue NameError
57
55
  # This fault format's ID can't be a class name. Use the
58
56
  # generic subclass of Fault.
data/lib/wadl/has_docs.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  # A component of wadl, the super cheap Ruby WADL client. #
5
5
  # #
6
6
  # Copyright (C) 2006-2008 Leonard Richardson #
7
- # Copyright (C) 2010-2013 Jens Wille #
7
+ # Copyright (C) 2010-2014 Jens Wille #
8
8
  # #
9
9
  # Authors: #
10
10
  # Leonard Richardson <leonardr@segfault.org> (Original author) #
@@ -26,8 +26,6 @@
26
26
  ###############################################################################
27
27
  #++
28
28
 
29
- require 'wadl'
30
-
31
29
  module WADL
32
30
 
33
31
  class HasDocs < CheapSchema
@@ -4,7 +4,7 @@
4
4
  # A component of wadl, the super cheap Ruby WADL client. #
5
5
  # #
6
6
  # Copyright (C) 2006-2008 Leonard Richardson #
7
- # Copyright (C) 2010-2013 Jens Wille #
7
+ # Copyright (C) 2010-2014 Jens Wille #
8
8
  # #
9
9
  # Authors: #
10
10
  # Leonard Richardson <leonardr@segfault.org> (Original author) #
@@ -26,29 +26,21 @@
26
26
  ###############################################################################
27
27
  #++
28
28
 
29
- require 'yaml'
30
- require 'rest-open-uri'
31
- require 'wadl'
32
-
33
- begin
34
- require 'oauth/client/helper'
35
- rescue LoadError
36
- warn "For OAuth support, install the 'oauth' library."
37
- end
38
-
39
29
  module WADL
40
30
 
41
31
  class HTTPMethod < HasDocs
42
32
 
43
- OAUTH_HEADER = 'Authorization'
44
- OAUTH_PREFIX = 'OAuth:'
45
-
46
33
  in_document 'method'
47
34
  as_collection 'http_methods'
48
35
  has_required :id, :name
49
- has_one RequestFormat, ResponseFormat
36
+ has_one RequestFormat
37
+ has_many ResponseFormat
50
38
  may_be_reference
51
39
 
40
+ def response
41
+ responses.first
42
+ end
43
+
52
44
  # Args:
53
45
  # :path - Values for path parameters
54
46
  # :query - Values for query parameters
@@ -64,47 +56,12 @@ module WADL
64
56
  method = dereference
65
57
 
66
58
  uri = method.request ? method.request.uri(resource, args) : resource.uri(args)
67
- headers = uri.headers.dup
68
-
69
- headers['Accept'] = expect_representation.mediaType if args[:expect_representation]
70
- headers['User-Agent'] = 'Ruby WADL client' unless headers['User-Agent']
71
- headers['Content-Type'] = 'application/x-www-form-urlencoded'
72
- headers[:method] = name.downcase.to_sym
73
- headers[:body] = args[:send_representation]
74
59
 
75
- set_oauth_header(headers, uri)
76
-
77
- response = begin
78
- open(uri, headers)
79
- rescue OpenURI::HTTPError => err
80
- err.io
81
- end
82
-
83
- method.response.build(response)
84
- end
85
-
86
- def set_oauth_header(headers, uri)
87
- args = headers[OAUTH_HEADER] or return
88
-
89
- yaml = args.dup
90
- yaml.sub!(/\A#{OAUTH_PREFIX}/, '') or return
91
-
92
- consumer_key, consumer_secret, access_token, token_secret = YAML.load(yaml)
93
-
94
- request = OpenURI::Methods[headers[:method]].new(uri.to_s)
95
-
96
- consumer = OAuth::Consumer.new(consumer_key, consumer_secret)
97
- token = OAuth::AccessToken.new(consumer, access_token, token_secret)
98
-
99
- helper = OAuth::Client::Helper.new(request,
100
- :request_uri => request.path,
101
- :consumer => consumer,
102
- :token => token,
103
- :scheme => 'header',
104
- :signature_method => 'HMAC-SHA1'
105
- )
60
+ headers = uri.headers.dup
61
+ headers['Accept'] = expect_representation.mediaType if args[:expect_representation]
106
62
 
107
- headers[OAUTH_HEADER] = helper.header
63
+ method.response.build(HTTPRequest.execute(
64
+ uri, name, args[:send_representation], headers))
108
65
  end
109
66
 
110
67
  end
@@ -0,0 +1,139 @@
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-2014 Jens Wille #
8
+ # #
9
+ # Authors: #
10
+ # Leonard Richardson <leonardr@segfault.org> (Original author) #
11
+ # Jens Wille <jens.wille@gmail.com> #
12
+ # #
13
+ # wadl is free software; you can redistribute it and/or modify it under the #
14
+ # terms of the GNU Affero General Public License as published by the Free #
15
+ # Software Foundation; either version 3 of the License, or (at your option) #
16
+ # any later 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 Affero General Public License for #
21
+ # more details. #
22
+ # #
23
+ # You should have received a copy of the GNU Affero General Public License #
24
+ # along with wadl. If not, see <http://www.gnu.org/licenses/>. #
25
+ # #
26
+ ###############################################################################
27
+ #++
28
+
29
+ require 'net/https'
30
+ require 'safe_yaml/load'
31
+
32
+ module WADL
33
+
34
+ require_oauth 'client/helper'
35
+
36
+ class HTTPRequest
37
+
38
+ DEFAULT_METHOD = :get
39
+
40
+ DEFAULT_USER_AGENT = "Ruby WADL client/#{VERSION}"
41
+ DEFAULT_CONTENT_TYPE = 'application/x-www-form-urlencoded'
42
+
43
+ OAUTH_HEADER = 'Authorization'
44
+ OAUTH_PREFIX = 'OAuth:'
45
+
46
+ class << self
47
+
48
+ def execute(uri, *args)
49
+ new(uri).execute(*args)
50
+ end
51
+
52
+ def oauth_header(args)
53
+ return unless valid_oauth_args?(args)
54
+ [OAUTH_HEADER, "#{OAUTH_PREFIX}#{args.to_yaml}"]
55
+ end
56
+
57
+ def valid_oauth_args?(args)
58
+ args.is_a?(Array) && args.size == 4
59
+ end
60
+
61
+ end
62
+
63
+ def initialize(uri)
64
+ self.uri = URI(uri)
65
+ end
66
+
67
+ def start
68
+ self.http = Net::HTTP.start(
69
+ uri.hostname, uri.port,
70
+ use_ssl: uri.scheme == 'https'
71
+ )
72
+
73
+ self
74
+ end
75
+
76
+ def finish
77
+ http.finish if started?
78
+ self
79
+ end
80
+
81
+ def started?
82
+ http && http.started?
83
+ end
84
+
85
+ def execute(*args)
86
+ start unless started?
87
+
88
+ req = prepare_request(*args)
89
+ res = http.request(req)
90
+
91
+ HTTPResponse.new(res)
92
+ end
93
+
94
+ private
95
+
96
+ attr_accessor :uri, :http
97
+
98
+ def prepare_request(method, body, headers)
99
+ req = make_request(method || DEFAULT_METHOD)
100
+ req.body = body if req.request_body_permitted?
101
+
102
+ set_headers(req, headers)
103
+
104
+ req
105
+ end
106
+
107
+ def make_request(method)
108
+ Net::HTTP.const_get(method.to_s.capitalize).new(uri)
109
+ rescue NameError
110
+ raise ArgumentError, "method not supported: #{method}"
111
+ end
112
+
113
+ def set_headers(req, headers)
114
+ set_oauth_header(req, headers)
115
+
116
+ headers['User-Agent'] ||= DEFAULT_USER_AGENT
117
+ headers['Content-Type'] ||= DEFAULT_CONTENT_TYPE
118
+
119
+ headers.each { |key, value|
120
+ Array(value).each { |val| req.add_field(key, val) }
121
+ }
122
+ end
123
+
124
+ def set_oauth_header(req, headers)
125
+ args = SafeYAML.load($') if headers[OAUTH_HEADER] =~ /\A#{OAUTH_PREFIX}/
126
+ return unless self.class.valid_oauth_args?(args)
127
+
128
+ headers[OAUTH_HEADER] = OAuth::Client::Helper.new(req,
129
+ request_uri: req.request_uri,
130
+ consumer: consumer = OAuth::Consumer.new(*args[0, 2]),
131
+ token: OAuth::AccessToken.new(consumer, *args[2, 2]),
132
+ scheme: 'header',
133
+ signature_method: 'HMAC-SHA1'
134
+ ).header
135
+ end
136
+
137
+ end
138
+
139
+ 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-2014 Jens Wille #
8
+ # #
9
+ # Authors: #
10
+ # Leonard Richardson <leonardr@segfault.org> (Original author) #
11
+ # Jens Wille <jens.wille@gmail.com> #
12
+ # #
13
+ # wadl is free software; you can redistribute it and/or modify it under the #
14
+ # terms of the GNU Affero General Public License as published by the Free #
15
+ # Software Foundation; either version 3 of the License, or (at your option) #
16
+ # any later 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 Affero General Public License for #
21
+ # more details. #
22
+ # #
23
+ # You should have received a copy of the GNU Affero General Public License #
24
+ # along with wadl. If not, see <http://www.gnu.org/licenses/>. #
25
+ # #
26
+ ###############################################################################
27
+ #++
28
+
29
+ module WADL
30
+
31
+ class HTTPResponse
32
+
33
+ def initialize(res)
34
+ @status, @content_type, @body, @headers =
35
+ [res.code, res.message], res.content_type, res.body, res.to_hash
36
+ end
37
+
38
+ attr_reader :status, :content_type, :body, :headers
39
+
40
+ alias_method :read, :body
41
+
42
+ end
43
+
44
+ end
data/lib/wadl/link.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  # A component of wadl, the super cheap Ruby WADL client. #
5
5
  # #
6
6
  # Copyright (C) 2006-2008 Leonard Richardson #
7
- # Copyright (C) 2010-2013 Jens Wille #
7
+ # Copyright (C) 2010-2014 Jens Wille #
8
8
  # #
9
9
  # Authors: #
10
10
  # Leonard Richardson <leonardr@segfault.org> (Original author) #
@@ -26,8 +26,6 @@
26
26
  ###############################################################################
27
27
  #++
28
28
 
29
- require 'wadl'
30
-
31
29
  module WADL
32
30
 
33
31
  class Link < HasDocs
data/lib/wadl/option.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  # A component of wadl, the super cheap Ruby WADL client. #
5
5
  # #
6
6
  # Copyright (C) 2006-2008 Leonard Richardson #
7
- # Copyright (C) 2010-2013 Jens Wille #
7
+ # Copyright (C) 2010-2014 Jens Wille #
8
8
  # #
9
9
  # Authors: #
10
10
  # Leonard Richardson <leonardr@segfault.org> (Original author) #
@@ -26,8 +26,6 @@
26
26
  ###############################################################################
27
27
  #++
28
28
 
29
- require 'wadl'
30
-
31
29
  module WADL
32
30
 
33
31
  class Option < HasDocs
data/lib/wadl/param.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  # A component of wadl, the super cheap Ruby WADL client. #
5
5
  # #
6
6
  # Copyright (C) 2006-2008 Leonard Richardson #
7
- # Copyright (C) 2010-2013 Jens Wille #
7
+ # Copyright (C) 2010-2014 Jens Wille #
8
8
  # #
9
9
  # Authors: #
10
10
  # Leonard Richardson <leonardr@segfault.org> (Original author) #
@@ -26,7 +26,7 @@
26
26
  ###############################################################################
27
27
  #++
28
28
 
29
- require 'wadl'
29
+ require 'uri'
30
30
 
31
31
  module WADL
32
32
 
@@ -4,7 +4,7 @@
4
4
  # A component of wadl, the super cheap Ruby WADL client. #
5
5
  # #
6
6
  # Copyright (C) 2006-2008 Leonard Richardson #
7
- # Copyright (C) 2010-2013 Jens Wille #
7
+ # Copyright (C) 2010-2014 Jens Wille #
8
8
  # #
9
9
  # Authors: #
10
10
  # Leonard Richardson <leonardr@segfault.org> (Original author) #
@@ -26,8 +26,6 @@
26
26
  ###############################################################################
27
27
  #++
28
28
 
29
- require 'wadl'
30
-
31
29
  module WADL
32
30
 
33
31
  # A mixin for objects that contain representations
@@ -4,7 +4,7 @@
4
4
  # A component of wadl, the super cheap Ruby WADL client. #
5
5
  # #
6
6
  # Copyright (C) 2006-2008 Leonard Richardson #
7
- # Copyright (C) 2010-2013 Jens Wille #
7
+ # Copyright (C) 2010-2014 Jens Wille #
8
8
  # #
9
9
  # Authors: #
10
10
  # Leonard Richardson <leonardr@segfault.org> (Original author) #
@@ -27,7 +27,6 @@
27
27
  #++
28
28
 
29
29
  require 'cgi'
30
- require 'wadl'
31
30
 
32
31
  module WADL
33
32
 
@@ -38,15 +37,17 @@ module WADL
38
37
  has_many Param
39
38
  may_be_reference
40
39
 
40
+ FORM_TYPES = %w[application/x-www-form-urlencoded multipart/form-data]
41
+
41
42
  def is_form_representation?
42
- mediaType == 'application/x-www-form-urlencoded' || mediaType == 'multipart/form-data'
43
+ FORM_TYPES.include?(mediaType)
43
44
  end
44
45
 
45
46
  # Creates a representation by plugging a set of parameters
46
47
  # into a representation format.
47
48
  def %(values)
48
- unless mediaType == 'application/x-www-form-urlencoded'
49
- raise "wadl.rb can't instantiate a representation of type #{mediaType}"
49
+ unless is_form_representation?
50
+ raise "wadl can't instantiate a representation of type #{mediaType}"
50
51
  end
51
52
 
52
53
  representation = []