From 4be8bdd5256ac6f585f52d9e853b3c2f6d11660d Mon Sep 17 00:00:00 2001 From: Anton Date: Sun, 25 Jan 2015 18:03:30 +0300 Subject: [PATCH 1/3] Add a controller hooking feature --- app/controllers/api_taster/routes_controller.rb | 1 + lib/api_taster.rb | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/app/controllers/api_taster/routes_controller.rb b/app/controllers/api_taster/routes_controller.rb index bd6b40a..a5f2d13 100644 --- a/app/controllers/api_taster/routes_controller.rb +++ b/app/controllers/api_taster/routes_controller.rb @@ -1,5 +1,6 @@ module ApiTaster class RoutesController < ApiTaster::ApplicationController + ApiTaster.controller_hook(self) before_filter :map_routes layout false, except: :index diff --git a/lib/api_taster.rb b/lib/api_taster.rb index e74e8e8..ee7aa35 100644 --- a/lib/api_taster.rb +++ b/lib/api_taster.rb @@ -25,5 +25,21 @@ def self.routes(&block) ApiTaster::RouteCollector.routes << block end + # Controller hooking may used for custom filters, authorizations, etc. + # + # Example with adding basic authentication: + # + # ApiTaster.controller_hook do + # http_basic_authenticate_with name: "admin", password: "123456" + # end + # + def self.controller_hook(klass=nil, &block) + if block_given? + @@controller_hook = Proc.new {|klass| klass.instance_eval(&block) } + elsif @@controller_hook && klass + @@controller_hook.call(klass) + end + end + class Exception < ::Exception; end end From e95191302918b2584923f3e24fc7b4ed91aa9569 Mon Sep 17 00:00:00 2001 From: Anton Date: Sun, 25 Jan 2015 18:21:09 +0300 Subject: [PATCH 2/3] Fix undefined variable --- lib/api_taster.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/api_taster.rb b/lib/api_taster.rb index ee7aa35..f88023d 100644 --- a/lib/api_taster.rb +++ b/lib/api_taster.rb @@ -25,6 +25,8 @@ def self.routes(&block) ApiTaster::RouteCollector.routes << block end + @@controller_hook = nil + # Controller hooking may used for custom filters, authorizations, etc. # # Example with adding basic authentication: From e338aed1794df2ed803bf4ac235c3d3e26f622b6 Mon Sep 17 00:00:00 2001 From: Anton Date: Sun, 25 Jan 2015 19:15:09 +0300 Subject: [PATCH 3/3] Add controller-hook tests --- .../api_taster/routes_controller_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spec/controllers/api_taster/routes_controller_spec.rb b/spec/controllers/api_taster/routes_controller_spec.rb index 7c09df9..7f074fd 100644 --- a/spec/controllers/api_taster/routes_controller_spec.rb +++ b/spec/controllers/api_taster/routes_controller_spec.rb @@ -61,5 +61,19 @@ module ApiTaster end end end + + context "when defined ApiTaster.controller_hook" do + before do + ApiTaster.controller_hook { before_action {|c| c.response.headers['X-Controller-Hook'] = '1'} } + ApiTaster.controller_hook(described_class) + end + + it 'applies the hook' do + ApiTaster.controller_hook.should.kind_of? Proc + + get :index, :use_route => :api_taster + response.headers.keys.should include('X-Controller-Hook') + end + end end end