Skip to content

Commit

Permalink
Merge pull request #16 from PRX/feat/add-default-api-responder
Browse files Browse the repository at this point in the history
set up a new default api responder
  • Loading branch information
pstjean authored Aug 12, 2019
2 parents e5007db + d8fceaa commit d5655bf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/hal_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/hal_api/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,6 +23,10 @@ module HalApi::Controller
respond_to :hal, :json

hal_rescue_standard_errors

def self.responder
HalApi::Responders::ApiResponder
end
end

private
Expand Down
19 changes: 19 additions & 0 deletions lib/hal_api/responders/api_responder.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d5655bf

Please sign in to comment.