wadl 0.1.3.1 → 0.1.4

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 CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to wadl version 0.1.3
5
+ This documentation refers to wadl version 0.1.4
6
6
 
7
7
 
8
8
  == DESCRIPTION
data/TODO CHANGED
@@ -1,2 +1 @@
1
- * generic API client: just provide the WADL location (path/URL) and make requests
2
1
  * WADL formatters (plain text, HTML?)
@@ -0,0 +1,31 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ #--
4
+ ###############################################################################
5
+ # #
6
+ # wadl -- Super cheap Ruby WADL client #
7
+ # #
8
+ # Copyright (C) 2010 Jens Wille #
9
+ # #
10
+ # Authors: #
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/cli'
30
+
31
+ WADL::CLI.execute(ARGV, STDIN, STDOUT, STDERR)
@@ -0,0 +1,5 @@
1
+ ---
2
+ :consumer_key: your_consumer_key
3
+ :consumer_secret: your_consumer_secret
4
+ :api_base: api.v1 # or whatever
5
+ :wadl: http://localhost/app.wadl
@@ -18,7 +18,7 @@
18
18
 
19
19
  <method name="POST" id="add">
20
20
  <request>
21
- <representation mediaType="application/x-www-form-encoded">
21
+ <representation mediaType="application/x-www-form-urlencoded">
22
22
  <param name="password" type="xsd:string" required="true" />
23
23
  <param name="entry" type="xsd:string" required="true" />
24
24
  <param name="title" type="xsd:string" />
@@ -0,0 +1,318 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of wadl, the super cheap Ruby WADL client. #
5
+ # #
6
+ # Copyright (C) 2010 Jens Wille #
7
+ # #
8
+ # Authors: #
9
+ # Jens Wille <jens.wille@uni-koeln.de> #
10
+ # #
11
+ # wadl is free software; you can redistribute it and/or modify it under the #
12
+ # terms of the GNU General Public License as published by the Free Software #
13
+ # Foundation; either version 3 of the License, or (at your option) any later #
14
+ # version. #
15
+ # #
16
+ # wadl is distributed in the hope that it will be useful, but WITHOUT ANY #
17
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
18
+ # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more #
19
+ # details. #
20
+ # #
21
+ # You should have received a copy of the GNU General Public License along #
22
+ # with wadl. If not, see <http://www.gnu.org/licenses/>. #
23
+ # #
24
+ ###############################################################################
25
+ #++
26
+
27
+ require 'optparse'
28
+ require 'yaml'
29
+ require 'highline'
30
+ require 'stringio'
31
+ require 'wadl'
32
+
33
+ begin
34
+ require 'oauth/cli'
35
+ rescue LoadError
36
+ warn "For OAuth support, install the 'oauth' library."
37
+ end
38
+
39
+ module WADL
40
+
41
+ class CLI
42
+
43
+ USAGE = "Usage: #{$0} [-h|--help] [options] <resource-path> [-- arguments]"
44
+
45
+ DEFAULTS = {
46
+ :config => 'config.yaml',
47
+ :method => 'GET',
48
+ :user => ENV['USER'] || '',
49
+ :request_token_url => '%s/oauth/request_token',
50
+ :access_token_url => '%s/oauth/access_token',
51
+ :authorize_url => '%s/oauth/authorize'
52
+ }
53
+
54
+ OPTION_RE = %r{\A--?(\w+)}
55
+ RESOURCE_PATH_RE = %r{[. /]}
56
+
57
+ def self.execute(*args)
58
+ new.execute(*args)
59
+ end
60
+
61
+ attr_reader :options, :config, :defaults
62
+ attr_reader :stdin, :stdout, :stderr
63
+ attr_reader :resource_path, :opts
64
+
65
+ def initialize(defaults = DEFAULTS)
66
+ @defaults = defaults
67
+
68
+ reset
69
+
70
+ # prevent backtrace on ^C
71
+ trap(:INT) { exit 130 }
72
+ end
73
+
74
+ def execute(arguments = [], *inouterr)
75
+ reset(*inouterr)
76
+
77
+ abort USAGE if arguments.empty?
78
+ parse_options(arguments, defaults)
79
+
80
+ abort YAML.dump(options), 0, stdout if options.delete(:dump_config)
81
+
82
+ parse_arguments(arguments)
83
+ abort USAGE if resource_path.empty?
84
+
85
+ abort 'WADL required' unless options[:wadl]
86
+ options[:wadl] %= options[:base_url] if options[:base_url]
87
+
88
+ response = auth_resource.send(options[:method].downcase, :query => opts)
89
+
90
+ stderr.puts response.code.join(' ')
91
+ stdout.puts response.representation unless response.code.first =~ /\A[45]/
92
+ end
93
+
94
+ def api
95
+ @api ||= WADL::Application.from_wadl(open(options[:wadl]))
96
+ end
97
+
98
+ def resource
99
+ @resource ||= begin
100
+ path = [options[:api_base], *resource_path].compact.join(' ')
101
+ path.split(RESOURCE_PATH_RE).inject(api) { |m, n| m.send(n) }
102
+ end
103
+ end
104
+
105
+ def auth_resource
106
+ @auth_resource ||= if options[:basic]
107
+ basic_auth_resource
108
+ elsif options[:oauth]
109
+ oauth_resource
110
+ else
111
+ resource
112
+ end
113
+ end
114
+
115
+ def reset(stdin = STDIN, stdout = STDOUT, stderr = STDERR)
116
+ @stdin, @stdout, @stderr = stdin, stdout, stderr
117
+ @api = @resource = @auth_resource = nil
118
+ @options, @config = {}, {}
119
+ end
120
+
121
+ private
122
+
123
+ def ask(question, &block)
124
+ HighLine.new(stdin, stdout).ask(question, &block)
125
+ end
126
+
127
+ def abort(msg = nil, status = 1, output = stderr)
128
+ output.puts msg if msg
129
+ exit status
130
+ end
131
+
132
+ def parse_options(arguments, defaults)
133
+ option_parser(defaults).parse!(arguments)
134
+
135
+ config_file = options[:config] || defaults[:config]
136
+ @config = YAML.load_file(config_file) if File.readable?(config_file)
137
+
138
+ [config, defaults].each { |hash| hash.each { |key, value| options[key] ||= value } }
139
+ end
140
+
141
+ def parse_arguments(arguments)
142
+ @resource_path, @opts, skip = [], {}, false
143
+
144
+ arguments.each_with_index { |arg, index|
145
+ if skip
146
+ skip = false
147
+ next
148
+ end
149
+
150
+ case arg
151
+ when OPTION_RE
152
+ key, value = $1, arguments[index + 1]
153
+
154
+ value =~ OPTION_RE ? value = '1' : skip = true
155
+
156
+ opts[key] = value
157
+ else
158
+ resource_path << arg
159
+ end
160
+ }
161
+ end
162
+
163
+ def basic_auth_resource
164
+ user, pass = options.values_at(:user, :password)
165
+ pass ||= ask("Password for user #{user}: ") { |q| q.echo = false }
166
+
167
+ abort 'USER and PASSWORD required' unless user && pass
168
+
169
+ resource.with_basic_auth(user, pass)
170
+ end
171
+
172
+ def oauth_resource
173
+ consumer_key, consumer_secret = options.values_at(:consumer_key, :consumer_secret)
174
+ access_token, token_secret = options.values_at(:token, :secret)
175
+
176
+ abort "CONSUMER KEY and SECRET required" unless consumer_key && consumer_secret
177
+
178
+ unless access_token && token_secret
179
+ access_token, token_secret = oauthorize(consumer_key, consumer_secret)
180
+ abort 'Authorization failed!?' unless access_token && token_secret
181
+ end
182
+
183
+ resource.with_oauth(consumer_key, consumer_secret, access_token, token_secret)
184
+ end
185
+
186
+ def oauthorize(consumer_key, consumer_secret)
187
+ strio = StringIO.new
188
+
189
+ def strio.puts(*args)
190
+ stdout.puts(*args)
191
+ super
192
+ end
193
+
194
+ base_url = options[:base_url] || File.dirname(options[:wadl])
195
+
196
+ OAuth::CLI.execute(stdout, stdin, stderr, [
197
+ '--consumer-key', consumer_key,
198
+ '--consumer-secret', consumer_secret,
199
+ '--request-token-url', options[:request_token_url] % base_url,
200
+ '--access-token-url', options[:access_token_url] % base_url,
201
+ '--authorize-url', options[:authorize_url] % base_url,
202
+ '--callback-url', 'oob',
203
+ 'authorize'
204
+ ])
205
+
206
+ result = strio.string
207
+ access_token = result[/^\s+oauth_token:\s+(.*)$/, 1]
208
+ token_secret = result[/^\s+oauth_token_secret:\s+(.*)$/, 1]
209
+
210
+ return unless access_token && token_secret
211
+
212
+ if File.writable?(options[:config])
213
+ config[:token] = access_token
214
+ config[:token_secret] = token_secret
215
+
216
+ File.open(options[:config], 'w') { |f| YAML.dump(config, f) }
217
+ end
218
+
219
+ [access_token, token_secret]
220
+ end
221
+
222
+ def option_parser(defaults)
223
+ OptionParser.new { |opts|
224
+ opts.banner = USAGE
225
+
226
+ opts.separator ''
227
+ opts.separator 'Options:'
228
+
229
+ opts.on('-c', '--config YAML', "Config file [Default: #{defaults[:config]}#{' (currently not present)' unless File.readable?(defaults[:config])}]") { |config|
230
+ options[:config] = config
231
+ }
232
+
233
+ opts.separator ''
234
+
235
+ opts.on('-w', '--wadl FILE_OR_URL', "Path or URL to WADL file") { |wadl|
236
+ options[:wadl] = wadl
237
+ }
238
+
239
+ opts.on('-m', '--method METHOD', "Request method [Default: #{defaults[:method]}]") { |method|
240
+ options[:method] = method.upcase
241
+ }
242
+
243
+ opts.on('-a', '--api-base PATH', "Base path for API") { |api_base|
244
+ options[:api_base] = api_base
245
+ }
246
+
247
+ opts.separator ''
248
+ opts.separator 'Basic auth options:'
249
+
250
+ opts.on('-B', '--basic [USER]', "Perform Basic auth (with optional user)") { |user|
251
+ options[:basic] = true
252
+ options[:user] = user if user
253
+ }
254
+
255
+ opts.on('--password PASSWORD', "Password for user") { |password|
256
+ options[:password] = password
257
+ }
258
+
259
+ opts.separator ''
260
+ opts.separator 'OAuth options:'
261
+
262
+ opts.on('-O', '--oauth [CONSUMER_KEY]', "Perform OAuth (with optional consumer key)") { |consumer_key|
263
+ options[:oauth] = true
264
+ options[:consumer_key] = consumer_key if consumer_key
265
+ }
266
+
267
+ opts.on('--consumer-secret SECRET', "Consumer secret to use") { |consumer_secret|
268
+ options[:consumer_secret] = consumer_secret
269
+ }
270
+
271
+ opts.separator ''
272
+
273
+ opts.on('--token TOKEN', "Access token to use") { |token|
274
+ options[:token] = token
275
+ }
276
+
277
+ opts.on('--secret SECRET', "Token secret to use") { |secret|
278
+ options[:secret] = secret
279
+ }
280
+
281
+ opts.separator ''
282
+
283
+ opts.on('-b', '--base-url URL', "Base URL [Default: \"dirname\" of WADL]") { |base_url|
284
+ options[:base_url] = base_url
285
+ }
286
+
287
+ opts.on('--request-token-url URL', "Request token URL [Default: #{defaults[:request_token_url] % 'BASE_URL'}]") { |request_token_url|
288
+ options[:request_token_url] = request_token_url
289
+ }
290
+
291
+ opts.on('--access-token-url URL', "Access token URL [Default: #{defaults[:access_token_url] % 'BASE_URL'}]") { |access_token_url|
292
+ options[:access_token_url] = access_token_url
293
+ }
294
+
295
+ opts.on('--authorize-url URL', "Authorize URL [Default: #{defaults[:authorize_url] % 'BASE_URL'}]") { |authorize_url|
296
+ options[:authorize_url] = authorize_url
297
+ }
298
+
299
+ opts.separator ''
300
+ opts.separator 'Generic options:'
301
+
302
+ opts.on('-h', '--help', 'Print this help message and exit') {
303
+ abort opts.to_s
304
+ }
305
+
306
+ opts.on('--version', 'Print program version and exit') {
307
+ abort "#{File.basename($0)} v#{WADL::VERSION}"
308
+ }
309
+
310
+ opts.on('-D', '--dump-config', "Dump config and exit") {
311
+ options[:dump_config] = true
312
+ }
313
+ }
314
+ end
315
+
316
+ end
317
+
318
+ end
@@ -30,6 +30,12 @@ require 'yaml'
30
30
  require 'rest-open-uri'
31
31
  require 'wadl'
32
32
 
33
+ begin
34
+ require 'oauth/client/helper'
35
+ rescue LoadError
36
+ warn "For OAuth support, install the 'oauth' library."
37
+ end
38
+
33
39
  module WADL
34
40
 
35
41
  class HTTPMethod < HasDocs
@@ -85,8 +91,6 @@ module WADL
85
91
 
86
92
  consumer_key, consumer_secret, access_token, token_secret = YAML.load(yaml)
87
93
 
88
- require 'oauth/client/helper'
89
-
90
94
  request = OpenURI::Methods[headers[:method]].new(uri.to_s)
91
95
 
92
96
  consumer = OAuth::Consumer.new(consumer_key, consumer_secret)
@@ -39,13 +39,13 @@ module WADL
39
39
  may_be_reference
40
40
 
41
41
  def is_form_representation?
42
- mediaType == 'application/x-www-form-encoded' || mediaType == 'multipart/form-data'
42
+ mediaType == 'application/x-www-form-urlencoded' || mediaType == 'multipart/form-data'
43
43
  end
44
44
 
45
45
  # Creates a representation by plugging a set of parameters
46
46
  # into a representation format.
47
47
  def %(values)
48
- unless mediaType == 'application/x-www-form-encoded'
48
+ unless mediaType == 'application/x-www-form-urlencoded'
49
49
  raise "wadl.rb can't instantiate a representation of type #{mediaType}"
50
50
  end
51
51
 
@@ -4,7 +4,7 @@ module WADL
4
4
 
5
5
  MAJOR = 0
6
6
  MINOR = 1
7
- TINY = 3
7
+ TINY = 4
8
8
 
9
9
  class << self
10
10
 
@@ -273,7 +273,7 @@ class PathParameters < WADLTest
273
273
 
274
274
  <method name="POST" id="set_graphic">
275
275
  <request>
276
- <representation mediaType="application/x-www-form-encoded">
276
+ <representation mediaType="application/x-www-form-urlencoded">
277
277
  <param name="new_graphic" type="xsd:string" required="true" />
278
278
  <param name="filename" type="xsd:string" required="true" />
279
279
  </representation>
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wadl
3
3
  version: !ruby/object:Gem::Version
4
- hash: 73
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 3
10
- - 1
11
- version: 0.1.3.1
9
+ - 4
10
+ version: 0.1.4
12
11
  platform: ruby
13
12
  authors:
14
13
  - Leonard Richardson
@@ -17,7 +16,7 @@ autorequire:
17
16
  bindir: bin
18
17
  cert_chain: []
19
18
 
20
- date: 2010-05-27 00:00:00 +02:00
19
+ date: 2010-05-28 00:00:00 +02:00
21
20
  default_executable:
22
21
  dependencies:
23
22
  - !ruby/object:Gem::Dependency
@@ -52,8 +51,8 @@ description: Ruby client for the Web Application Description Language.
52
51
  email:
53
52
  - leonardr@segfault.org
54
53
  - jens.wille@uni-koeln.de
55
- executables: []
56
-
54
+ executables:
55
+ - wadl
57
56
  extensions: []
58
57
 
59
58
  extra_rdoc_files:
@@ -69,6 +68,7 @@ files:
69
68
  - lib/wadl/representation_container.rb
70
69
  - lib/wadl/documentation.rb
71
70
  - lib/wadl/param.rb
71
+ - lib/wadl/cli.rb
72
72
  - lib/wadl/xml_representation.rb
73
73
  - lib/wadl/uri_parts.rb
74
74
  - lib/wadl/application.rb
@@ -98,9 +98,11 @@ files:
98
98
  - examples/yahoo.rb
99
99
  - examples/crummy.wadl
100
100
  - examples/delicious.wadl
101
+ - examples/config.yaml
101
102
  - examples/YahooSearch.wadl
102
103
  - examples/delicious.rb
103
104
  - test/test_wadl.rb
105
+ - bin/wadl
104
106
  has_rdoc: true
105
107
  homepage: http://github.com/blackwinter/wadl
106
108
  licenses: []