diff --git a/lib/hal_api.rb b/lib/hal_api.rb index 4f6a1d0..442387b 100644 --- a/lib/hal_api.rb +++ b/lib/hal_api.rb @@ -3,6 +3,7 @@ module HalApi require 'hal_api/errors' require 'hal_api/controller' require 'hal_api/represented_model' + require 'hal_api/responders/api_responder' def self.rails_major_version ::Rails.version.split('.')[0].to_i diff --git a/lib/hal_api/controller.rb b/lib/hal_api/controller.rb index 4cc28e1..c3f898b 100644 --- a/lib/hal_api/controller.rb +++ b/lib/hal_api/controller.rb @@ -9,6 +9,7 @@ module HalApi::Controller require 'hal_api/controller/cache' require 'hal_api/controller/resources' require 'hal_api/controller/exceptions' + require 'hal_api/responders/api_responder' include HalApi::Controller::Actions include HalApi::Controller::Cache @@ -22,6 +23,10 @@ module HalApi::Controller respond_to :hal, :json hal_rescue_standard_errors + + def self.responder + HalApi::Responders::ApiResponder + end end private diff --git a/lib/hal_api/responders/api_responder.rb b/lib/hal_api/responders/api_responder.rb new file mode 100644 index 0000000..4c6c5af --- /dev/null +++ b/lib/hal_api/responders/api_responder.rb @@ -0,0 +1,19 @@ +require 'roar/rails/responder' + +module HalApi::Responders + class ApiResponder < Roar::Rails::Responder + def api_behavior + raise MissingRenderer.new(format) unless has_renderer? + + if post? + display(resource, status: :created) + elsif put? + display(resource, status: :ok) + elsif delete? + display(resource, status: :no_content) + else + super + end + end + end +end