Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add WADL

  • Loading branch information...
commit 9622db7854a09fc2b4708861c75e35ac6cc15020 1 parent fbcb400
François Perrad authored
5 CHANGES
View
@@ -1,5 +1,10 @@
Revision history for lua-Spore
+0.2.2
+ - use WADL document as Spore specification
+ LIMITATION: cross-references are not supported
+ - add wadl2spore utility
+
0.2.1 Sun Sep 16 11:30:00 2012
- add middleware Auth.DataPublica
- refactor tests for LuaJIT
2  CMakeLists.txt
View
@@ -34,7 +34,9 @@ install_lua_module ( Spore.Middleware.UserAgent src/Spore/Middleware/UserAgent.l
install_lua_module ( Spore.Protocols src/Spore/Protocols.lua )
install_lua_module ( Spore.Request src/Spore/Request.lua )
install_lua_module ( Spore.XML src/Spore/XML.lua )
+install_lua_module ( Spore.WADL src/Spore/WADL.lua )
install_lua_executable ( discovery2spore src/discovery2spore )
+install_lua_executable ( wadl2spore src/wadl2spore )
install_data ( CHANGES COPYRIGHT README.md )
install_test ( test/ )
2  Makefile
View
@@ -16,11 +16,13 @@ all: dist.cmake
install:
$(INSTALL) -m 755 -D src/discovery2spore $(BINDIR)/discovery2spore
+ $(INSTALL) -m 755 -D src/wadl2spore $(BINDIR)/wadl2spore
$(INSTALL) -m 644 -D src/Spore.lua $(LIBDIR)/Spore.lua
$(INSTALL) -m 644 -D src/Spore/Core.lua $(LIBDIR)/Spore/Core.lua
$(INSTALL) -m 644 -D src/Spore/GoogleDiscovery.lua $(LIBDIR)/Spore/GoogleDiscovery.lua
$(INSTALL) -m 644 -D src/Spore/Protocols.lua $(LIBDIR)/Spore/Protocols.lua
$(INSTALL) -m 644 -D src/Spore/Request.lua $(LIBDIR)/Spore/Request.lua
+ $(INSTALL) -m 644 -D src/Spore/WADL.lua $(LIBDIR)/Spore/WADL.lua
$(INSTALL) -m 644 -D src/Spore/XML.lua $(LIBDIR)/Spore/XML.lua
$(INSTALL) -m 644 -D src/Spore/Middleware/Cache.lua $(LIBDIR)/Spore/Middleware/Cache.lua
$(INSTALL) -m 644 -D src/Spore/Middleware/DoNotTrack.lua $(LIBDIR)/Spore/Middleware/DoNotTrack.lua
6 rockspec.in
View
@@ -57,10 +57,14 @@ build = {
['Spore.Middleware.UserAgent'] = 'src/Spore/Middleware/UserAgent.lua',
['Spore.Protocols'] = 'src/Spore/Protocols.lua',
['Spore.Request'] = 'src/Spore/Request.lua',
+ ['Spore.WADL'] = 'src/Spore/WADL.lua',
['Spore.XML'] = 'src/Spore/XML.lua',
},
install = {
- bin = { 'src/discovery2spore' }
+ bin = {
+ 'src/discovery2spore',
+ 'src/wadl2spore',
+ }
},
copy_directories = { 'doc', 'test' },
}
132 src/Spore/WADL.lua
View
@@ -0,0 +1,132 @@
+
+--
+-- lua-Spore : <http://fperrad.github.com/lua-Spore>
+--
+-- Web Application Description Language
+-- see http://www.w3.org/Submission/wadl/
+
+-- LIMITATION: cross-references are not supported
+
+local assert = assert
+local ipairs = ipairs
+local tonumber = tonumber
+local type = type
+local checktype = require 'Spore'.checktype
+local new_from_lua = require 'Spore'.new_from_lua
+local slurp = require 'Spore.Protocols'.slurp
+local parse = require 'Spore.XML'.parse
+
+_ENV = nil
+local m = {}
+
+local function convert_uri_template (uri)
+ -- see RFC 6570
+ return uri:gsub('{([%w_]+)}', ':%1')
+end
+
+local function convert (doc)
+ local wadl = assert(parse(doc))
+ wadl = assert(wadl.application)
+ local base_url
+ local spore = {
+ methods = {},
+ }
+
+ local function get_params (params, required_prm, optional_prm)
+ local function clone (t)
+ if t then
+ local r = {}
+ for i = 1, #t do
+ r[i] = t[i]
+ end
+ return r
+ end
+ end -- clone
+
+ local required_params = clone(required_prm)
+ local optional_params = clone(optional_prm)
+ for _, param in ipairs(params or {}) do
+ if param.style ~= 'header' then
+ if param.required and (param.required == 'true'
+ or param.required == '1') then
+ if not required_params then
+ required_params = {}
+ end
+ required_params[#required_params+1] = param.name
+ else
+ if not optional_params then
+ optional_params = {}
+ end
+ optional_params[#optional_params+1] = param.name
+ end
+ end
+ end
+ return required_params, optional_params
+ end -- get_params
+
+ local function populate (methods, path, required_prm, optional_prm)
+ local path = path and convert_uri_template(path)
+ for _, meth in ipairs(methods or {}) do
+ local methname = assert(meth.id, "method name missing")
+ local method = meth.name
+ local request = meth.request and meth.request[1]
+ local params = request and request.param
+ local required_params, optional_params = get_params(params, required_prm, optional_prm)
+ local expected_status
+ for _, response in ipairs(meth.response or {}) do
+ local status = response.status
+ if status then
+ if not expected_status then
+ expected_status = {}
+ end
+ expected_status[#expected_status+1] = tonumber(status)
+ end
+ end
+ spore.methods[methname] = {
+ base_url = base_url,
+ path = path,
+ method = method,
+ required_params = required_params,
+ optional_params = optional_params,
+ required_payload = (method == 'POST')
+ or (method == 'PUT')
+ or nil,
+ expected_status = expected_status,
+ }
+ end
+ end -- populate
+
+ local function walk (resource, required_prm, optional_prm)
+ for _, _resource in ipairs(resource or {}) do
+ local required_params, optional_params = get_params(params, required_prm, optional_prm)
+ walk(_resource.resource, required_params, optional_params)
+ populate(_resource.method, _resource.path, required_params, optional_params)
+ end
+ end --walk
+
+ for _, _resources in ipairs(wadl.resources or {}) do
+ base_url = _resources.base
+ walk(_resources.resource)
+ end
+
+ populate(wadl.method)
+
+ return spore
+end
+m.convert = convert
+
+function m.new_from_wadl (name, opts)
+ opts = opts or {}
+ checktype('new_from_wadl', 1, name, 'string')
+ checktype('new_from_wadl', 2, opts, 'table')
+ local content = slurp(name)
+ return new_from_lua(convert(content), opts)
+end
+
+return m
+--
+-- Copyright (c) 2012 Francois Perrad
+--
+-- This library is licensed under the terms of the MIT/X11 license,
+-- like Lua itself.
+--
23 src/wadl2spore
View
@@ -0,0 +1,23 @@
+#!/usr/bin/lua
+--
+-- lua-Spore : <http://fperrad.github.com/lua-Spore>
+--
+
+if #arg ~= 1 then
+ print 'Usage: wadl2spore url'
+ os.exit(0)
+end
+
+local slurp = require 'Spore.Protocols'.slurp
+local convert = require 'Spore.WADL'.convert
+local encode = require 'json'.encode
+
+local content = slurp(arg[1])
+print(encode(convert(content)))
+
+--
+-- Copyright (c) 2012 Francois Perrad
+--
+-- This program is licensed under the terms of the MIT/X11 license,
+-- like Lua itself.
+--
13 test/08-wadl.t
View
@@ -0,0 +1,13 @@
+#!/usr/bin/env lua
+
+require 'Test.More'
+
+plan(4)
+
+local m = require 'Spore.WADL'
+type_ok( m, 'table', "Spore.WADL" )
+is( m, package.loaded['Spore.WADL'] )
+
+type_ok( m.new_from_wadl, 'function' )
+type_ok( m.convert, 'function' )
+
Please sign in to comment.
Something went wrong with that request. Please try again.