From 96ce0f017cfe58210d84e5092446395b811ccdab Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Fri, 3 Dec 2010 11:25:21 +0000 Subject: [PATCH] Adds a builder-like template system for rendering xml and json API responses. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4452 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/application_controller.rb | 2 +- lib/redmine.rb | 2 + lib/redmine/views/api_template_handler.rb | 28 ++++++++ lib/redmine/views/builders.rb | 35 ++++++++++ lib/redmine/views/builders/json.rb | 30 ++++++++ lib/redmine/views/builders/structure.rb | 68 +++++++++++++++++++ lib/redmine/views/builders/xml.rb | 45 ++++++++++++ .../lib/redmine/views/builders/json_test.rb | 54 +++++++++++++++ .../lib/redmine/views/builders/xml_test.rb | 54 +++++++++++++++ 9 files changed, 317 insertions(+), 1 deletion(-) create mode 100644 lib/redmine/views/api_template_handler.rb create mode 100644 lib/redmine/views/builders.rb create mode 100644 lib/redmine/views/builders/json.rb create mode 100644 lib/redmine/views/builders/structure.rb create mode 100644 lib/redmine/views/builders/xml.rb create mode 100644 test/unit/lib/redmine/views/builders/json_test.rb create mode 100644 test/unit/lib/redmine/views/builders/xml_test.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d7b1add85..bbcc2653e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -22,7 +22,7 @@ class ApplicationController < ActionController::Base include Redmine::I18n layout 'base' - exempt_from_layout 'builder' + exempt_from_layout 'builder', 'apit' # Remove broken cookie after upgrade from 0.8.x (#4292) # See https://rails.lighthouseapp.com/projects/8994/tickets/3360 diff --git a/lib/redmine.rb b/lib/redmine.rb index 1e6a71810..2b01f25e8 100644 --- a/lib/redmine.rb +++ b/lib/redmine.rb @@ -229,3 +229,5 @@ end Redmine::WikiFormatting.map do |format| format.register :textile, Redmine::WikiFormatting::Textile::Formatter, Redmine::WikiFormatting::Textile::Helper end + +ActionView::Template.register_template_handler :apit, Redmine::Views::ApiTemplateHandler diff --git a/lib/redmine/views/api_template_handler.rb b/lib/redmine/views/api_template_handler.rb new file mode 100644 index 000000000..4f73227ec --- /dev/null +++ b/lib/redmine/views/api_template_handler.rb @@ -0,0 +1,28 @@ +# Redmine - project management software +# Copyright (C) 2006-2010 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +module Redmine + module Views + class ApiTemplateHandler < ActionView::TemplateHandler + include ActionView::TemplateHandlers::Compilable + + def compile(template) + "Redmine::Views::Builders.for(params[:format]) do |api|; #{template.source}; self.output_buffer = api.output; end" + end + end + end +end diff --git a/lib/redmine/views/builders.rb b/lib/redmine/views/builders.rb new file mode 100644 index 000000000..6e0761c2f --- /dev/null +++ b/lib/redmine/views/builders.rb @@ -0,0 +1,35 @@ +# Redmine - project management software +# Copyright (C) 2006-2010 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +module Redmine + module Views + module Builders + def self.for(format, &block) + builder = case format + when 'xml', :xml; Builders::Xml.new + when 'json', :json; Builders::Json.new + else; raise "No builder for format #{format}" + end + if block + block.call(builder) + else + builder + end + end + end + end +end diff --git a/lib/redmine/views/builders/json.rb b/lib/redmine/views/builders/json.rb new file mode 100644 index 000000000..61037eb30 --- /dev/null +++ b/lib/redmine/views/builders/json.rb @@ -0,0 +1,30 @@ +# Redmine - project management software +# Copyright (C) 2006-2010 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +require 'blankslate' + +module Redmine + module Views + module Builders + class Json < Structure + def output + @struct.first.to_json + end + end + end + end +end diff --git a/lib/redmine/views/builders/structure.rb b/lib/redmine/views/builders/structure.rb new file mode 100644 index 000000000..b1add8be8 --- /dev/null +++ b/lib/redmine/views/builders/structure.rb @@ -0,0 +1,68 @@ +# Redmine - project management software +# Copyright (C) 2006-2010 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +require 'blankslate' + +module Redmine + module Views + module Builders + class Structure < BlankSlate + def initialize + @struct = [{}] + end + + def array(tag, &block) + @struct << [] + block.call(self) + ret = @struct.pop + @struct.last[tag] = ret + end + + def method_missing(sym, *args, &block) + if args.any? + if args.first.is_a?(Hash) + if @struct.last.is_a?(Array) + @struct.last << args.first + end + else + if @struct.last.is_a?(Array) + @struct.last << (args.last || {}).merge(:value => args.first) + else + @struct.last[sym] = args.first + end + end + end + + if block + @struct << {} + block.call(self) + ret = @struct.pop + if @struct.last.is_a?(Array) + @struct.last << ret + else + @struct.last[sym] = ret + end + end + end + + def output + raise "Need to implement #{self.class.name}#output" + end + end + end + end +end diff --git a/lib/redmine/views/builders/xml.rb b/lib/redmine/views/builders/xml.rb new file mode 100644 index 000000000..41a767154 --- /dev/null +++ b/lib/redmine/views/builders/xml.rb @@ -0,0 +1,45 @@ +# Redmine - project management software +# Copyright (C) 2006-2010 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +module Redmine + module Views + module Builders + class Xml < ::Builder::XmlMarkup + def initialize + super + instruct! + end + + def output + target! + end + + def method_missing(sym, *args, &block) + if args.size == 1 && args.first.is_a?(Time) + __send__ sym, args.first.xmlschema, &block + else + super + end + end + + def array(name, options={}, &block) + __send__ name, options.merge(:type => 'array'), &block + end + end + end + end +end diff --git a/test/unit/lib/redmine/views/builders/json_test.rb b/test/unit/lib/redmine/views/builders/json_test.rb new file mode 100644 index 000000000..52e6b9ca6 --- /dev/null +++ b/test/unit/lib/redmine/views/builders/json_test.rb @@ -0,0 +1,54 @@ +# Redmine - project management software +# Copyright (C) 2006-2010 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +require File.dirname(__FILE__) + '/../../../../../test_helper' + +class Redmine::Views::Builders::JsonTest < HelperTestCase + + def test_hash + assert_json_output({'person' => {'name' => 'Ryan', 'age' => 32}}) do |b| + b.person do + b.name 'Ryan' + b.age 32 + end + end + end + + def test_array + assert_json_output({'books' => [{'title' => 'Book 1', 'author' => 'B. Smith'}, {'title' => 'Book 2', 'author' => 'G. Cooper'}]}) do |b| + b.array :books do |b| + b.book :title => 'Book 1', :author => 'B. Smith' + b.book :title => 'Book 2', :author => 'G. Cooper' + end + end + end + + def test_array_with_content_tags + assert_json_output({'books' => [{'value' => 'Book 1', 'author' => 'B. Smith'}, {'value' => 'Book 2', 'author' => 'G. Cooper'}]}) do |b| + b.array :books do |b| + b.book 'Book 1', :author => 'B. Smith' + b.book 'Book 2', :author => 'G. Cooper' + end + end + end + + def assert_json_output(expected, &block) + builder = Redmine::Views::Builders::Json.new + block.call(builder) + assert_equal(expected, ActiveSupport::JSON.decode(builder.output)) + end +end diff --git a/test/unit/lib/redmine/views/builders/xml_test.rb b/test/unit/lib/redmine/views/builders/xml_test.rb new file mode 100644 index 000000000..6a278fedb --- /dev/null +++ b/test/unit/lib/redmine/views/builders/xml_test.rb @@ -0,0 +1,54 @@ +# Redmine - project management software +# Copyright (C) 2006-2010 Jean-Philippe Lang +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +require File.dirname(__FILE__) + '/../../../../../test_helper' + +class Redmine::Views::Builders::XmlTest < HelperTestCase + + def test_hash + assert_xml_output('Ryan32') do |b| + b.person do + b.name 'Ryan' + b.age 32 + end + end + end + + def test_array + assert_xml_output('') do |b| + b.array :books do |b| + b.book :title => 'Book 1', :author => 'B. Smith' + b.book :title => 'Book 2', :author => 'G. Cooper' + end + end + end + + def test_array_with_content_tags + assert_xml_output('Book 1Book 2') do |b| + b.array :books do |b| + b.book 'Book 1', :author => 'B. Smith' + b.book 'Book 2', :author => 'G. Cooper' + end + end + end + + def assert_xml_output(expected, &block) + builder = Redmine::Views::Builders::Xml.new + block.call(builder) + assert_equal('' + expected, builder.output) + end +end -- 2.39.5