Skip to content
Browse files

Fixed rake task, and added documentation

  • Loading branch information...
1 parent 0a5481f commit 12771031ea136ff32d6f9b56f232eed2fb2c1641 @austvik committed Feb 23, 2011
Showing with 109 additions and 12 deletions.
  1. +46 −3 README
  2. +15 −1 lib/rake/wadlgen.rb
  3. +20 −1 lib/wadlgen.rb
  4. +10 −3 lib/wadlgen/merge.rb
  5. +14 −0 test/test_merge.rb
  6. +4 −4 wadlgen.gemspec
View
49 README
@@ -11,13 +11,56 @@ Approach:
- Let the user edit application.wadl to add information that can't be
extracted, and preserve what the user has added the next time the
task is run
+- Needs Ruby 1.9.2 for ordered hashes in tests, also works on 1.8.7.
What works:
- Parse a WADL file to an object model
- Generate a WADL file from an object model
- Parse a Rails Route file to a object model
- Rake task to parse the rails route
-
-What remains
- merge two object models
-- make the rails task write a public/application.wadl file
+- make the rails task write a public/application.wadl file
+- 100% code coverage by unit tests
+
+Rails 3 Usage:
+
+ Add this to the Gemfile:
+ gem 'wadlgen', :git => 'git://github.com/austvik/wadlgen.git'
+
+ To generate a public/application.wadl:
+ rake wadlgen
+
+ Now you can edit public/application.wadl. The next time you run
+ rake wadlgen, any changes to the application will be merged into your
+ WADL file, while your changes are preserved.
+
+Programmatic usage:
+
+ There are several methods that can be used to generate, merge and write
+ wadl object structures:
+
+ require 'wadlgen'
+
+ # Generate a WADL XML document based on a Rails application
+ # and base.
+ Wadlgen::Wadl.generate(application, base)
+
+ # parses a given XML string, and generates a Wadlgen::Application
+ # object from it.
+ Wadlgen::Wadl.parse_xml(xml_doc)
+
+ # Parses routes from Rails 3, and generates a Wadlgen::Application
+ # object from it.
+ Wadlgen::Wadl.parse_route(application, base)
+
+ # Generates a WADL XML string based on a Wadlgen::Application object
+ Wadlgen::Wadl.generate_wadl(application)
+
+ # Merges two Wadlgen::Application objects and returns the result
+ # as another Wadlgen::Application object.
+ Wadlgen::Wadl.merge(initial_application, additional_application)
+
+ In addition the Wadlgen::* classes in src/wadlgen/classes.rb can be used
+ to understand the object model created from these methods, or to generate
+ structures to write to a WADL XML file.
+
View
16 lib/rake/wadlgen.rb
@@ -5,5 +5,19 @@
desc "Generates WADL descriptors from routes"
task :wadlgen => :environment do
require 'wadlgen'
- puts Wadlgen::Wadl.generate Rails.application, "http://example.com/"
+
+ file_name = 'public/application.wadl'
+ base = "http://example.com/"
+ if File.exist?(file_name)
+ original_file = File.open(file_name, "rb").read
+ original = Wadlgen::Wadl.parse_xml original_file
+ routes = Wadlgen::Wadl.parse_route Rails.application, base
+ merge = Wadlgen::Wadl.merge original, routes
+ wadl = Wadlgen::Wadl.generate_wadl merge
+ File.open(file_name, 'w') {|f| f.write(wadl) }
+ else
+ wadl = Wadlgen::Wadl.generate Rails.application, base
+ File.open(file_name, 'w') {|f| f.write(wadl) }
+ end
+ puts "Wrote WADL to '#{file_name}', please edit to add details."
end
View
21 lib/wadlgen.rb
@@ -2,34 +2,53 @@ module Wadlgen
require "wadlgen/railtie.rb" if defined?(Rails)
require "wadlgen/classes"
- require "wadlgen/merge"
require "wadlgen/xml_parser"
require "wadlgen/route_parser"
require "wadlgen/xml_generator"
+ require "wadlgen/merge"
require 'nokogiri'
class Wadl
+ #
+ # Generate a WADL XML document based on a Rails application
+ # and base.
+ #
def self.generate(application, base)
application = parse_route(application, base)
generate_wadl application
end
+ #
+ # parses a given XML string, and generates a Wadlgen::Application
+ # object from it.
+ #
def self.parse_xml(xml_doc)
parser = Wadlgen::XMLParser.new xml_doc
parser.parse
end
+ #
+ # Parses routes from Rails 3, and generates a Wadlgen::Application
+ # object from it.
+ #
def self.parse_route(application, base)
parser = Wadlgen::RouteParser.new application, base
parser.parse
end
+ #
+ # Generates a WADL XML string based on a Wadlgen::Application object
+ #
def self.generate_wadl(application)
generator = Wadlgen::XMLGenerator.new application
generator.generate
end
+ #
+ # Merges two Wadlgen::Application objects and returns the result
+ # as another Wadlgen::Application object.
+ #
def self.merge(initial_application, additional_application)
merger = Wadlgen::Merge.new initial_application, additional_application
merger.merge
View
13 lib/wadlgen/merge.rb
@@ -215,11 +215,18 @@ def merge_link(target, initial, additional = [])
def merge_grammars(target, initial, additional)
grammars = target.add_grammars
- merge_docs grammars, initial.docs, additional.docs
- merge_includes grammars, initial.includes, additional.includes
+ if initial
+ if additional
+ merge_docs grammars, initial.docs, additional.docs
+ merge_includes grammars, initial.includes, additional.includes
+ else
+ merge_docs grammars, initial.docs
+ merge_includes grammars, initial.includes
+ end
+ end
end
- def merge_includes(target, initial, additional)
+ def merge_includes(target, initial, additional = [])
initial.each do |incl|
incl = target.add_include incl.href
merge_docs incl, incl.docs
View
14 test/test_merge.rb
@@ -82,5 +82,19 @@ def test_merge_application
assert_equal 'query', merge.resources.first.resources.first.methods.first.request.params.first.link.resource_type
end
+ def test_merge_no_grammars
+ app1 = Wadlgen::Application.new
+ app1.add_doc('app1', "App1")
+ gram1 = app1.add_grammars
+
+ app2 = Wadlgen::Application.new
+ app2.add_doc('app2', "App2")
+
+ merge = Wadlgen::Wadl.merge app1, app2
+
+ assert_equal 1, merge.docs.length
+ assert_equal merge, merge.grammars.application
+ end
+
end
View
8 wadlgen.gemspec
@@ -6,18 +6,18 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
s.authors = ["Jorgen Austvik"]
- s.date = %q{2011-02-17}
+ s.date = %q{2011-02-23}
s.description = %q{Generate WADL from rails routes}
s.email = %q{jaustvik@acm.org}
- s.extra_rdoc_files = ["README", "lib/rake/wadlgen.rb", "lib/wadlgen.rb", "lib/wadlgen/classes.rb", "lib/wadlgen/railtie.rb", "lib/wadlgen/route_parser.rb", "lib/wadlgen/xml_generator.rb", "lib/wadlgen/xml_parser.rb"]
- s.files = ["Gemfile", "Gemfile.lock", "MIT-LICENSE", "README", "Rakefile", "lib/rake/wadlgen.rb", "lib/wadlgen.rb", "lib/wadlgen/classes.rb", "lib/wadlgen/railtie.rb", "lib/wadlgen/route_parser.rb", "lib/wadlgen/xml_generator.rb", "lib/wadlgen/xml_parser.rb", "test/test_classes.rb", "test/test_generate.rb", "test/test_route_parser.rb", "test/test_xml_parser.rb", "wadlgen.gemspec", "Manifest"]
+ s.extra_rdoc_files = ["README", "lib/rake/wadlgen.rb", "lib/wadlgen.rb", "lib/wadlgen/classes.rb", "lib/wadlgen/merge.rb", "lib/wadlgen/railtie.rb", "lib/wadlgen/route_parser.rb", "lib/wadlgen/xml_generator.rb", "lib/wadlgen/xml_parser.rb"]
+ s.files = ["Gemfile", "Gemfile.lock", "MIT-LICENSE", "README", "Rakefile", "lib/rake/wadlgen.rb", "lib/wadlgen.rb", "lib/wadlgen/classes.rb", "lib/wadlgen/merge.rb", "lib/wadlgen/railtie.rb", "lib/wadlgen/route_parser.rb", "lib/wadlgen/xml_generator.rb", "lib/wadlgen/xml_parser.rb", "test/test_classes.rb", "test/test_generate.rb", "test/test_merge.rb", "test/test_route_parser.rb", "test/test_xml_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_route_parser.rb", "test/test_classes.rb", "test/test_generate.rb", "test/test_xml_parser.rb"]
+ s.test_files = ["test/test_merge.rb", "test/test_route_parser.rb", "test/test_classes.rb", "test/test_generate.rb", "test/test_xml_parser.rb"]
if s.respond_to? :specification_version then
s.specification_version = 3

0 comments on commit 1277103

Please sign in to comment.
Something went wrong with that request. Please try again.