apotonick/roar — Issue #5: Out of the box custom media type?
Author: chrisupb
Created: 2011-12-29 10:44:54+00:00
State: open

Hey,

I just tried rabl, restfulie, argonaut, draper, json_builder and think that roar could be the one to implement my rest api. ;-)
Configuration for my custom mime type:

config/initializers/mime_types.rb

Mime::Type.register "application/vnd.xxx-v1+json", :xxx_v1

app/controllers/api/v1/base_controller.rb

class Api::V1::BaseController < ActionController::Base
respond_to :xxx_v1

# just a try
# ActionController.add_renderer :xxx_v1 do |obj, options|
# json = obj.to_xxx_v1
# self.content_type ||= Mime::Type.lookup('application/vnd.xxx-v1+json')
# self.response_body = json
# end
end

app/controllers/api/v1/users_controller.rb

class Api::V1::UsersController < Api::V1::BaseController
def show
@user = User.find(params[:id])
@user.extend(UserRepresenter)
respond_with @user
end
end

lib/representers/user_representer.rb

require 'roar/representer/json'

module UserRepresenter
include Roar::Representer::JSON

property :username
end

GET http://127.0.0.1:3000/api/users/4ef853273bdc010ffa000017
Accept application/vn.xxx-v1+json

Error:
Missing template api/v1/users/show, gapi/v1/base/show with {:handlers=>[:erb, :builder], :formats=>[:ginkgo_v1], :locale=>[:en, :en]}. Searched in: * "app/views"

When using Accept application/json and respond to_json everything is working fine. Do I have to add an renderer somewhere?

Another small question: Can I rename properties? Something like

property :username => :user_name