I started playing around with RABL and here is what I came up with:
Support for versioning via case statement in new views/bookmarks/index.rabl:
collection @bookmarks, :root => "bookmarks"
case params[:apiversion]
when "0.4"
attributes :id
else
default 'current' api
attributes :id, :query, :name
end
http://192.168.1.46:3000/bookmarks.json?apiversion=0.4
{"bookmarks":[{"bookmark":{"id":2}},{"bookmark":{"id":5}},{"bookmark":{"id":4}},{"bookmark":{"id":1}},{"bookmark":{"id":3}}]}
http://192.168.1.46:3000/bookmarks.json
{"bookmarks":[{"bookmark":{"id":2,"name":"active","query":"last_report > "35 minutes ago" and (status.applied > 0 or status.restarted > 0)"}},{"bookmark":{"id":5,"name":"disabled","query":"status.enabled = false"}},{"bookmark":{"id":4,"name":"error","query":"last_report > "35 minutes ago" and (status.failed > 0 or status.failed_restarts > 0 or status.skipped > 0)"}},{"bookmark":{"id":1,"name":"eventful","query":"eventful = true"}},{"bookmark":{"id":3,"name":"out of sync","query":"last_report < "30 minutes ago" and status.enabled = true"}}]}
Its trivial to get this working. Took about 5 minutes. I haven't found a way to list all attributes but I have a feeling it will not be necessary in most cases.
Attached is a patch to implement what I coded already. I don't expect it to be merged into develop but just to serve as an example.
I believe the apiversion could also be passed in the header.
What do you guys think? Seems easy enough. Anytime we have to fork our api code for a new version we can just create a new case to support the previous version.
From 1a563e39c566d6e8bdbdbd12cb480df90ff2dfff Mon Sep 17 00:00:00 2001
···
From: Corey Osman Date: Mon, 30 Apr 2012 22:23:10 -0700 Subject: [PATCH] Added view based json renderering using rablGemfile | 2 ±
app/controllers/bookmarks_controller.rb | 5 +++±
app/views/bookmarks/index.rabl | 10 ++++++++++
config/initializers/rabl_init.rb | 19 +++++++++++++++++++
4 files changed, 34 insertions(+), 2 deletions(-)
create mode 100644 app/views/bookmarks/index.rabl
create mode 100644 config/initializers/rabl_init.rb
diff --git a/Gemfile b/Gemfile
index 1ab5b7c…bf5f789 100644
— a/Gemfile
+++ b/Gemfile
@@ -12,7 +12,7 @@ gem ‘scoped_search’, '>= 2.3.7’
gem 'net-ldap’
gem “safemode”, "~> 1.0.1"
gem ‘uuidtools’
+gem 'rabl’
local_gemfile = File.dirname(FILE) + "/Gemfile.local.rb"
if File.file?(local_gemfile)
self.instance_eval(Bundler.read_file(local_gemfile))
diff --git a/app/controllers/bookmarks_controller.rb b/app/controllers/bookmarks_controller.rb
index ba4a723…4bf1a68 100644
— a/app/controllers/bookmarks_controller.rb
+++ b/app/controllers/bookmarks_controller.rb
@@ -6,7 +6,10 @@ class BookmarksController < ApplicationController
respond_to do |format|
format.html
-
format.json { render :json => @bookmarks }
-
# Before RAPL
-
#format.json { render :json => @bookmarks }
-
# After RAPL
-
endformat.json
end
diff --git a/app/views/bookmarks/index.rabl b/app/views/bookmarks/index.rabl
new file mode 100644
index 0000000…aaa9827
— /dev/null
+++ b/app/views/bookmarks/index.rabl
@@ -0,0 +1,10 @@
+collection @bookmarks, :root => “bookmarks”
+
+
+case params[:apiversion]
- when “0.4”
- attributes :id
- else
-
default ‘current’ api
- attributes :id, :query, :name
- end
diff --git a/config/initializers/rabl_init.rb b/config/initializers/rabl_init.rb
new file mode 100644
index 0000000…96df777
— /dev/null
+++ b/config/initializers/rabl_init.rb
@@ -0,0 +1,19 @@
+# config/initializers/rabl_init.rb
+Rabl.configure do |config| -
Commented as these are defaults
-
config.cache_all_output = false
-
config.cache_sources = false
-
config.escape_all_output = false
-
config.json_engine = nil # Any multi_json engines
-
config.msgpack_engine = nil # Defaults to ::MessagePack
-
config.bson_engine = nil # Defaults to ::BSON
-
config.plist_engine = nil # Defaults to ::Plist::Emit
- config.include_json_root = true
-
config.include_msgpack_root = true
-
config.include_bson_root = true
-
config.include_plist_root = true
-
config.include_xml_root = false
-
config.enable_json_callbacks = false
-
config.xml_options = { :dasherize => true, :skip_types => false }
-
config.view_paths = []
+end
1.7.1
Corey Osman
corey@logicminds.biz
678-348-0582 (Pacific Time)
Green IT and Datacenter Automation Specialist