baywatch 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YjE3ZTliNWY5Y2MxZTk2NTNiODg0NGIzYzJhODU5YmM3ZmNiZDUyNQ==
5
+ data.tar.gz: !binary |-
6
+ ZDhhMjI1ZjFhNzYzMDZiOGJjYmM4NzBlM2IyOGNlNGFjZjcxNzM0YQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ OGU5ZWEzZjgyNDMwNzFhZjAzNmQzYjRhZTczMTY5ZDY2ZWYwMjE5YjU0ZDk4
10
+ YmEwNDkxMmVlYzg5ZmJjZGY3Y2JhMTY5NjIwNmNiYWQ3MWIxNDhkNDY5MTEx
11
+ ZTA4MDVlOGU3M2ZiZTBiMDc2NDU2ZDA5MjY0MzM4MGE1NmQwMTk=
12
+ data.tar.gz: !binary |-
13
+ MzQwZmE2NDc0NDM2NmM4NTYwNWRiZmE2ZjQxYzM5NmRlOWM2YzM2MjgzODcw
14
+ OGNkMTY0Yjc1ZmY4MDFiOGFjYzQzNjkyZjgxNDQwMDZmNDcyNmQ2YWRlNGNk
15
+ YjgzMTc2OWZjZjRmNTQwYmU3YzY4Y2IxYjI3NWFhZGIxZGI1YjQ=
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in baywatch.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Thiago Dantas
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,103 @@
1
+ [![Build Status](https://travis-ci.org/tdantas/baywatch.svg)](https://travis-ci.org/tdantas/baywatch)
2
+
3
+ # Baywatch
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'baywatch'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ ## Usage
16
+
17
+ Declarative way to error handling inside controller
18
+
19
+ ````
20
+
21
+ class UsersController < ApplicationController
22
+ include Baywatch::Rescue
23
+
24
+ # By default will handle([ Errno::ECONNREFUSED, Errno::ECONNRESET ])
25
+ service_down do |on|
26
+ on.index do
27
+ flash[:error] = "Sorry, the service is not working properly"
28
+ end
29
+ on.create do
30
+ flash[:error] = "Sorry, we cannot save right now"
31
+ flash.keep(:error)
32
+ redirect root_url
33
+ end
34
+ end
35
+
36
+ def index
37
+ @users = UserRemoteService.all
38
+ end
39
+
40
+ def create
41
+ UserRemoteService.create(params[:user])
42
+ end
43
+
44
+ end
45
+
46
+ ````
47
+
48
+ Using #only to use the same behavior to many actions.
49
+
50
+
51
+ ````
52
+
53
+ class UserController < ApplicationController
54
+ include Baywatch::Rescue
55
+
56
+ service_down do |on|
57
+ on.only :index, :create do
58
+ flash[:error] = "Sorry, the service is not working properly"
59
+ redirect_to root_url
60
+ end
61
+ end
62
+
63
+ def index
64
+ @users = UserRemoteService.all
65
+ end
66
+
67
+ def create
68
+ UserRemoteService.create(params[:user])
69
+ end
70
+
71
+ end
72
+
73
+ ````
74
+
75
+
76
+ ### TODO
77
+
78
+ ##### Handle Contextual Exception
79
+
80
+ ```
81
+ service_down do |on|
82
+
83
+ on.edit(ActiveRecord::RecordNotFound)
84
+ flash[:error] = "Not Found"
85
+ redirect_to root_url
86
+ end
87
+
88
+ on.edit(Base::WhateverException)
89
+ flash[:error] = "Whatever Message Here"
90
+ MailAdministrator.send(:fail")
91
+ redirect_to root_url
92
+ end
93
+ end
94
+ ```
95
+
96
+ ## Contributing
97
+
98
+ 1. Fork it
99
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
100
+ 3. Cover with tests
101
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
102
+ 5. Push to the branch (`git push origin my-new-feature`)
103
+ 6. Create new Pull Request
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ task :default => :test
5
+ RSpec::Core::RakeTask.new(:test)
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'baywatch/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "baywatch"
8
+ gem.version = Baywatch::VERSION
9
+ gem.authors = ["Thiago Dantas"]
10
+ gem.email = ["thiago.teixeira.dantas@gmail.com"]
11
+ gem.description = %q{Baywatch your code}
12
+ gem.summary = %q{Baywatch rescue}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency "rspec", '~> 2.14.1'
21
+ gem.add_development_dependency "mocha", '~> 1.0.0'
22
+ gem.add_development_dependency "actionpack"
23
+ gem.add_development_dependency 'rspec-rails'
24
+ gem.add_development_dependency 'pry', '~> 0.9.12.6'
25
+ gem.add_development_dependency 'activemodel'
26
+
27
+ end
@@ -0,0 +1,6 @@
1
+ require "baywatch/version"
2
+
3
+ module Baywatch
4
+ autoload :Config, 'baywatch/config'
5
+ autoload :Rescue, 'baywatch/rescue'
6
+ end
@@ -0,0 +1,31 @@
1
+ module Baywatch
2
+
3
+ class Config
4
+ attr_accessor :configurations
5
+
6
+ def initialize
7
+ @configurations = {}
8
+ end
9
+
10
+ def include?(action)
11
+ @configurations.keys.include?(action.to_sym)
12
+ end
13
+
14
+ def [](val)
15
+ @configurations[val.to_sym]
16
+ end
17
+
18
+ def only(*args, &block)
19
+ args.each do |action|
20
+ @configurations[action.to_sym] = block
21
+ end
22
+ end
23
+
24
+ def method_missing(method, *args, &block)
25
+ raise "You should pass one Block #{method}" if block.nil?
26
+ @configurations[method.to_sym] = block
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,55 @@
1
+ module Baywatch
2
+
3
+ module Rescue
4
+
5
+ DEFAULT_EXCEPTIONS = [ Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::ECONNRESET ]
6
+
7
+ def self.included(base)
8
+ base.extend ClassMethods
9
+ end
10
+
11
+ module ClassMethods
12
+
13
+ def service_down(*exceptions, &block)
14
+ on = Baywatch::Config.new
15
+ block.call(on)
16
+ exceptions_appended = exceptions.unshift(*DEFAULT_EXCEPTIONS)
17
+ define_rescues(exceptions_appended, on)
18
+ end
19
+
20
+ private
21
+
22
+ def define_rescues(exceptions, configuration)
23
+ define_baywatch_requested_action
24
+ define_baywatch(configuration)
25
+ register_rescue_from(exceptions)
26
+ end
27
+
28
+ def define_baywatch_requested_action
29
+ self.class_eval do
30
+ def baywatch_requested_action
31
+ request.filtered_parameters["action"]
32
+ end
33
+ end
34
+ end
35
+
36
+ def define_baywatch(configuration)
37
+ self.class_eval do
38
+ define_method :baywatch do |exception|
39
+ raise exception unless configuration.include? baywatch_requested_action
40
+ block = configuration[baywatch_requested_action]
41
+ self.instance_eval &block
42
+ end
43
+ end
44
+ end
45
+
46
+ def register_rescue_from(exceptions)
47
+ self.class_eval do
48
+ self.rescue_from *exceptions, :with => :baywatch
49
+ end
50
+ end
51
+
52
+ end #classMethods
53
+
54
+ end
55
+ end
@@ -0,0 +1,3 @@
1
+ module Baywatch
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe BaywatchController, type: :controller do
5
+
6
+ context "Service Down Interface" do
7
+ it "pass Baywatch::Config as block parameter" do
8
+ WhateverController = Class.new(ActionController::Base) do
9
+ include Baywatch::Rescue
10
+ end
11
+
12
+ WhateverController.service_down do |on|
13
+ expect(on).to be_kind_of(Baywatch::Config)
14
+ end
15
+ end
16
+
17
+ it "register baywatch method to instance" do
18
+ expect(controller).to respond_to(:baywatch)
19
+ end
20
+ end
21
+
22
+ context "Service Unavailable Exceptions" do
23
+
24
+ it "doesn't capture unregistered exception" do
25
+ controller.stub(:index) do
26
+ raise "WHATEVER EXCEPTION"
27
+ end
28
+
29
+ expect{ get :index }.to raise_error
30
+ end
31
+
32
+ it "does not capture exception for not registered action" do
33
+ controller.stub(:edit) do
34
+ raise "WHATEVER EXCEPTION"
35
+ end
36
+
37
+ expect{ get :edit }.to raise_error("WHATEVER EXCEPTION")
38
+ end
39
+
40
+ it "captures explicit exception registered" do
41
+ controller.stub(:timeout) do
42
+ raise BaywatchController::TimeoutException
43
+ end
44
+
45
+ get :timeout
46
+ response.should redirect_to("/baywatch_service_down_rescued")
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe BeachesController, type: :controller do
4
+
5
+ it "captures Errno::ECONNRESET " do
6
+ controller.stub(:long_beach) do
7
+ raise Errno::ECONNRESET.new
8
+ end
9
+
10
+ get :long_beach
11
+ response.should redirect_to("/baywatch_service_down_rescued")
12
+ end
13
+
14
+ it "captures Errno::ECONNREFUSED " do
15
+ controller.stub(:venice) do
16
+ raise Errno::ECONNREFUSED.new
17
+ end
18
+
19
+ get :venice
20
+ response.should redirect_to("/baywatch_service_down_rescued")
21
+ end
22
+
23
+ it "captures Errno::ETIMEDOUT" do
24
+ controller.stub(:costa_da_caparica) do
25
+ raise Errno::ETIMEDOUT.new
26
+ end
27
+
28
+ get :costa_da_caparica
29
+ response.should redirect_to("/baywatch_service_down_rescued")
30
+ end
31
+
32
+ it "captures Errno::ECONNRESET" do
33
+ exception = Errno::ECONNRESET.new
34
+ controller.stub(:sunset) do
35
+ raise exception
36
+ end
37
+
38
+ expect{ get :sunset }.to raise_error(exception)
39
+ end
40
+
41
+ end
@@ -0,0 +1,34 @@
1
+ require 'active_model'
2
+ require 'active_support/all'
3
+ require 'action_controller'
4
+ require 'action_dispatch'
5
+
6
+ module Rails
7
+ class App
8
+
9
+ def env_config; {} end
10
+
11
+ def routes
12
+ return @routes if defined?(@routes)
13
+ @routes = ActionDispatch::Routing::RouteSet.new
14
+ @routes.draw do
15
+
16
+ get "/baywatch" => "baywatch#index"
17
+ get "/timeout" => "baywatch#timeout"
18
+ get "/edit" => "baywatch#edit"
19
+
20
+ get "/long_beach" => "beaches#long_beach"
21
+ get "/venice" => "beaches#venice"
22
+ get "/sunset" => "beaches#sunset"
23
+ get "/costa_da_caparica" => "beaches#costa_da_caparica"
24
+ end
25
+
26
+ @routes
27
+ end
28
+ end
29
+
30
+ def self.application
31
+ @app ||= App.new
32
+ end
33
+
34
+ end
@@ -0,0 +1,7 @@
1
+ class ApplicationController < ActionController::Base
2
+ include Rails.application.routes.url_helpers
3
+ def render(*attributes); end
4
+ end
5
+
6
+ require_relative './controllers/baywatch'
7
+ require_relative './controllers/beaches'
@@ -0,0 +1,31 @@
1
+ class BaywatchController < ApplicationController
2
+ include Baywatch::Rescue
3
+
4
+ class TimeoutException < StandardError; end
5
+
6
+ service_down(BaywatchController::TimeoutException) do |on|
7
+
8
+ on.index do |e|
9
+ redirect_to "/baywatch_service_down_rescued"
10
+ end
11
+
12
+ on.timeout do |e|
13
+ redirect_to "/baywatch_service_down_rescued"
14
+ end
15
+
16
+ end
17
+
18
+
19
+ def index
20
+ redirect_to "/baywatch_index"
21
+ end
22
+
23
+ def timeout
24
+ redirect_to "/baywatch_timeout"
25
+ end
26
+
27
+ def edit
28
+ redirect_to "/baywatch_edit"
29
+ end
30
+
31
+ end
@@ -0,0 +1,26 @@
1
+ class BeachesController < ApplicationController
2
+ include Baywatch::Rescue
3
+
4
+ service_down do |on|
5
+ on.only :venice, :long_beach, :costa_da_caparica do
6
+ redirect_to "/baywatch_service_down_rescued"
7
+ end
8
+ end
9
+
10
+ def long_beach
11
+ redirect_to "beaches_venice"
12
+ end
13
+
14
+ def venice
15
+ redirect_to "beaches_venice"
16
+ end
17
+
18
+ def sunset
19
+ redirect_to "beaches_venice"
20
+ end
21
+
22
+ def costa_da_caparica
23
+ redirect_to "beaches_costa_da_caparica"
24
+ end
25
+
26
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Baywatch do
4
+
5
+ context "Service Down Interface" do
6
+ before(:each) do
7
+ @controller = Class.new(ActionController::Base) do
8
+ include Baywatch::Rescue
9
+ service_down do |on| on.action {} end
10
+ end
11
+ end
12
+
13
+ it "pass Baywatch::Config as block parameter" do
14
+ @controller.service_down do |on|
15
+ expect(on).to be_kind_of(Baywatch::Config)
16
+ end
17
+ end
18
+
19
+ it "register baywatch method to instance" do
20
+ expect(@controller.new).to respond_to(:baywatch)
21
+ end
22
+
23
+ it "pass Baywatch::Config as block parameter" do
24
+ @controller.service_down do |on|
25
+ expect{ on.create }.to raise_error
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ require 'baywatch'
2
+
3
+ require 'fixtures/app'
4
+ require 'fixtures/controller'
5
+ require 'rspec/rails'
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: baywatch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Thiago Dantas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.14.1
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.14.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: mocha
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: actionpack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 0.9.12.6
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 0.9.12.6
83
+ - !ruby/object:Gem::Dependency
84
+ name: activemodel
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Baywatch your code
98
+ email:
99
+ - thiago.teixeira.dantas@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .rspec
106
+ - .travis.yml
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - baywatch.gemspec
112
+ - lib/baywatch.rb
113
+ - lib/baywatch/config.rb
114
+ - lib/baywatch/rescue.rb
115
+ - lib/baywatch/version.rb
116
+ - spec/baywatch_controller_spec.rb
117
+ - spec/beaches_controller_spec.rb
118
+ - spec/fixtures/app.rb
119
+ - spec/fixtures/controller.rb
120
+ - spec/fixtures/controllers/baywatch.rb
121
+ - spec/fixtures/controllers/beaches.rb
122
+ - spec/service_down_spec.rb
123
+ - spec/spec_helper.rb
124
+ homepage: ''
125
+ licenses: []
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ! '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ! '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.2.1
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Baywatch rescue
147
+ test_files:
148
+ - spec/baywatch_controller_spec.rb
149
+ - spec/beaches_controller_spec.rb
150
+ - spec/fixtures/app.rb
151
+ - spec/fixtures/controller.rb
152
+ - spec/fixtures/controllers/baywatch.rb
153
+ - spec/fixtures/controllers/beaches.rb
154
+ - spec/service_down_spec.rb
155
+ - spec/spec_helper.rb