Skip to content

Commit

Permalink
Make it possible to parse a WADL file
Browse files Browse the repository at this point in the history
  • Loading branch information
austvik committed Feb 8, 2011
1 parent 5574396 commit 89ab07b
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Echoe.new('wadlgen', '0.1.0') do |p|
p.author = 'Jorgen Austvik'
p.email = '[email protected]'
p.ignore_pattern = ['tmp/*', 'nbproject/**/*']
p.runtime_dependencies = ['builder']
p.runtime_dependencies = ['builder', 'nokogiri']
p.development_dependencies = ['test-unit']
p.require_paths = ['lib']
p.retain_gemspec = true
Expand Down
3 changes: 1 addition & 2 deletions lib/rake/wadlgen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
desc "Generates WADL descriptors from routes"
task :wadlgen => :environment do
require 'wadlgen'
wadl = Wadlgen::Wadl.new
wadl.generate("http://example.com/")
Wadlgen::Wadl.generate("http://example.com/")
end
113 changes: 74 additions & 39 deletions lib/wadlgen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,48 @@ module Wadlgen

require "wadlgen/railtie.rb" if defined?(Rails)
require "wadlgen/classes"
require 'nokogiri'

class Wadl

def generate(base)
def self.generate(base)
application = get_route_structure(base)
puts generate_wadl application
end

def get_route_structure(base)
def self.parse(xml_doc)
xml = Nokogiri::XML::parse(xml_doc)

ns = {'wadl' => 'http://wadl.dev.java.net/2009/02'}

resources = xml.xpath('/wadl:application/wadl:resources', ns).first
base = resources['base']
app = Wadlgen::Application.new(base)
resources.xpath('wadl:resource', ns).each do |resource|
resource_elem = app.add_resource(resource['path'])
resource.xpath('wadl:method', ns).each do |method|
method_elem = resource_elem.add_method(method['name'], method['id'])
method.xpath('wadl:request', ns).each do |request|
request_elem = method_elem.add_request
request.xpath('wadl:param', ns).each do |param|
param_elem = request_elem.add_param(param['name'], param['style'])
param.xpath('wadl:option', ns).each do |option|
param_elem.add_option(option['value'], option['mediaType'])
end
end
end
method.xpath('wadl:response', ns).each do |response|
response_elem = method_elem.add_response(response['status'].to_i)
response.xpath('wadl:representation', ns).each do |repr|
response_elem.add_representation(repr['mediaType'], repr['element'])
end
end
end
end
app
end

def self.get_route_structure(base)
Rails.application.reload_routes!

app = Wadlgen::Application.new(base)
Expand Down Expand Up @@ -42,43 +75,7 @@ def get_route_structure(base)
app
end

def get_representations(controller, action)
if action == 'edit'
{:html => 'html'}
else
res = {}
controller_name = "#{controller}_controller".camelcase
cont_obj = Object::const_get(controller_name).new()
cont_obj.mimes_for_respond_to.each_pair do |format, type|
res[format] = format
# TODO: Only and Except
end
res
end
end

def get_params(action)
case action
when 'show'
{:id => 'query'}
when 'new'
{:id => 'query'}
when 'create'
{:id => 'query'}
when 'update'
{:id => 'query'}
when 'destroy'
{:id => 'query'}
when 'index'
{}
when 'edit'
{}
else
{}
end
end

def generate_wadl(application)
def self.generate_wadl(application)
require 'builder'

out = ""
Expand Down Expand Up @@ -129,6 +126,44 @@ def generate_wadl(application)
out
end

private

def self.get_representations(controller, action)
if action == 'edit'
{:html => 'html'}
else
res = {}
controller_name = "#{controller}_controller".camelcase
cont_obj = Object::const_get(controller_name).new()
cont_obj.mimes_for_respond_to.each_pair do |format, type|
res[format] = format
# TODO: Only and Except
end
res
end
end

def self.get_params(action)
case action
when 'show'
{:id => 'query'}
when 'new'
{:id => 'query'}
when 'create'
{:id => 'query'}
when 'update'
{:id => 'query'}
when 'destroy'
{:id => 'query'}
when 'index'
{}
when 'edit'
{}
else
{}
end
end

end # class Wadl

end
3 changes: 1 addition & 2 deletions test/test_generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def test_very_simple_route
</application>
HERE

wadl = Wadlgen::Wadl.new
app = Wadlgen::Application.new("http://example.com/application/")
accounts = app.add_resource("accounts")
get = accounts.add_method('GET', 'account')
Expand All @@ -47,7 +46,7 @@ def test_very_simple_route
success.add_representation('application/json')
err = get.add_response(400)
err.add_representation('application/xml', 'Error')
result = wadl.generate_wadl app
result = Wadlgen::Wadl.generate_wadl app

assert_equal expected, result

Expand Down
110 changes: 110 additions & 0 deletions test/test_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
require 'test/unit'
require 'wadlgen'

class Testparser < Test::Unit::TestCase

def test_parse_simple_wadl
document = <<HERE
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://wadl.dev.java.net/2009/02 wadl.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://wadl.dev.java.net/2009/02">
<resources base="http://example.com/application/">
<resource path="accounts">
<method name="GET" id="GET_accounts">
<request>
<param name="format" style="query">
<option value="html" mediaType="application/html"/>
<option value="xml" mediaType="application/xml"/>
<option value="json" mediaType="application/json"/>
</param>
</request>
<response status="200">
<representation mediaType="application/html" element="html"/>
<representation mediaType="application/xml" element="accounts"/>
<representation mediaType="application/json"/>
</response>
<response status="400">
<representation mediaType="application/xml" element="Error"/>
</response>
</method>
</resource>
</resources>
</application>
HERE

wadl = Wadlgen::Wadl.parse(document)
assert_equal "http://example.com/application/", wadl.base
assert_equal 1, wadl.resources.length
resource = wadl.resources.first
assert_equal 'accounts', resource.path
assert_equal 1, resource.methods.length
method = resource.methods.first
assert_equal 'GET', method.verb
assert_equal 1, method.requests.length
request = method.requests.first
assert_equal 1, request.parameters.length
param = request.parameters.first
assert_equal 'format', param.name
assert_equal 'query', param.style
assert_equal 3, param.options.length
assert_equal 'html', param.options[0].value
assert_equal 'application/html', param.options[0].media_type
assert_equal 'xml', param.options[1].value
assert_equal 'application/xml', param.options[1].media_type
assert_equal 'json', param.options[2].value
assert_equal 'application/json', param.options[2].media_type
assert_equal 2, method.responses.length

response = method.responses.first
assert_equal 200, response.status
assert_equal 3, response.representations.length
assert_equal 'html', response.representations[0].element
assert_equal 'application/html', response.representations[0].media_type
assert_equal 'accounts', response.representations[1].element
assert_equal 'application/xml', response.representations[1].media_type
assert_nil response.representations[2].element
assert_equal 'application/json', response.representations[2].media_type

response = method.responses.last
assert_equal 400, response.status
assert_equal 1, response.representations.length
assert_equal 'Error', response.representations[0].element
assert_equal 'application/xml', response.representations[0].media_type
end

def test_roundtrip
document = <<HERE
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://wadl.dev.java.net/2009/02 wadl.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://wadl.dev.java.net/2009/02">
<resources base="http://example.com/application/">
<resource path="accounts">
<method name="GET" id="GET_accounts">
<request>
<param name="format" style="query">
<option value="html" mediaType="application/html"/>
<option value="xml" mediaType="application/xml"/>
<option value="json" mediaType="application/json"/>
</param>
</request>
<response status="200">
<representation mediaType="application/html" element="html"/>
<representation mediaType="application/xml" element="accounts"/>
<representation mediaType="application/json"/>
</response>
<response status="400">
<representation mediaType="application/xml" element="Error"/>
</response>
</method>
</resource>
</resources>
</application>
HERE

wadl = Wadlgen::Wadl.parse document
document2 = Wadlgen::Wadl.generate_wadl(wadl)
assert_equal document, document2
wadl2 = Wadlgen::Wadl.parse document2
document3 = Wadlgen::Wadl.generate_wadl(wadl2)
assert_equal document, document3
end

end
7 changes: 5 additions & 2 deletions wadlgen.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,30 @@ Gem::Specification.new do |s|
s.description = %q{Generate WADL from rails routes}
s.email = %q{[email protected]}
s.extra_rdoc_files = ["README", "lib/rake/wadlgen.rb", "lib/wadlgen.rb", "lib/wadlgen/classes.rb", "lib/wadlgen/railtie.rb"]
s.files = ["MIT-LICENSE", "README", "Rakefile", "lib/rake/wadlgen.rb", "lib/wadlgen.rb", "lib/wadlgen/classes.rb", "lib/wadlgen/railtie.rb", "test/test_classes.rb", "test/test_generate.rb", "wadlgen.gemspec", "Manifest"]
s.files = ["MIT-LICENSE", "README", "Rakefile", "lib/rake/wadlgen.rb", "lib/wadlgen.rb", "lib/wadlgen/classes.rb", "lib/wadlgen/railtie.rb", "test/test_classes.rb", "test/test_generate.rb", "test/test_parser.rb", "wadlgen.gemspec", "Manifest"]
s.homepage = %q{https://github.com/austvik/wadlgen}
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Wadlgen", "--main", "README"]
s.require_paths = ["lib"]
s.rubyforge_project = %q{wadlgen}
s.rubygems_version = %q{1.5.0}
s.summary = %q{Generate WADL from rails routes}
s.test_files = ["test/test_classes.rb", "test/test_generate.rb"]
s.test_files = ["test/test_classes.rb", "test/test_parser.rb", "test/test_generate.rb"]

if s.respond_to? :specification_version then
s.specification_version = 3

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<builder>, [">= 0"])
s.add_runtime_dependency(%q<nokogiri>, [">= 0"])
s.add_development_dependency(%q<test-unit>, [">= 0"])
else
s.add_dependency(%q<builder>, [">= 0"])
s.add_dependency(%q<nokogiri>, [">= 0"])
s.add_dependency(%q<test-unit>, [">= 0"])
end
else
s.add_dependency(%q<builder>, [">= 0"])
s.add_dependency(%q<nokogiri>, [">= 0"])
s.add_dependency(%q<test-unit>, [">= 0"])
end
end

0 comments on commit 89ab07b

Please sign in to comment.