four_o_four 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c56888a29ff641441b1617489a286c45cdffae74
4
- data.tar.gz: 3ab8f1407a71606a2eae46b5f28e69b127d1904f
3
+ metadata.gz: 429692009ac816f33701d6efa8f8e3722dcfbb2c
4
+ data.tar.gz: cb77dc906dc4f6dc28a52875b0833f249fe778d3
5
5
  SHA512:
6
- metadata.gz: ae1d58a685163bc7391799586ce174046803cd6c6458805d4b5ff7dc5f6d9fa8c178635c0f2f58456aff76bb8e275a05aa89d4dfdd0a2e54d206ea9a5e426303
7
- data.tar.gz: 2beb20375740455db4aba69b56c9d27c808379285a0a2c5f8f1d4a918cb3781b138dd5b21a7140f241d0952ae4af41118645551fc0b53d7382ff0c723d69e01d
6
+ metadata.gz: 5bcbbcc7b705eb382642bda6b9c7c42f27fcdebf905934d6b499e3281480bcf2dcb6faeb78a20d174c8f635dba84720babe513f224e5d20558fbf7220f5fa1bc
7
+ data.tar.gz: bf234d39e81a2f236a8f9c8a795fd7ef7d7875116b17e56ead9b48618978bc5b360b345962b5a28354b059845c9364d968e3b73a44379f56c20608c3460f93d5
data/README.md CHANGED
@@ -1,11 +1,8 @@
1
1
  # FourOFour
2
2
 
3
- [![Build
4
- Status](https://travis-ci.org/esparta/four_o_four.svg?branch=master)](https://travis-ci.org/esparta/four_o_four)
5
- [![Code
6
- Climate](https://codeclimate.com/github/esparta/four_o_four/badges/gpa.svg)](https://codeclimate.com/github/esparta/four_o_four)
7
- [![Test
8
- Coverage](https://codeclimate.com/github/esparta/four_o_four/badges/coverage.svg)](https://codeclimate.com/github/esparta/four_o_four/coverage)
3
+ [![Build Status][travis]](https://travis-ci.org/SparkHub/four_o_four)
4
+ [![Code Climate][gpa]](https://codeclimate.com/github/SparkHub/four_o_four)
5
+ [![Test Coverage][coverage]](https://codeclimate.com/github/SparkHub/four_o_four/coverage)
9
6
 
10
7
  A super simple Rack middleware to capture 404 responses and change the response
11
8
  for any other logic.
@@ -40,7 +37,7 @@ application. On rails can be done like this:
40
37
  module YourAwesomeApp
41
38
  class Application < Rails::Application
42
39
  # .... extra configuration
43
- config.middleware.use 'FourOFour'
40
+ config.middleware.insert_before ActionDispatch::RemoteIp, FourOFour
44
41
  end
45
42
  end
46
43
  ```
@@ -59,7 +56,7 @@ parameter to the configuration...
59
56
  module YourAwesomeApp
60
57
  class Application < Rails::Application
61
58
  # .... extra configuration
62
- config.middleware.use 'FourOFour', 'DelegatedClass'
59
+ config.middleware.insert_before ActionDispatch::RemoteIp, FourOFour, 'DelegatedClass'
63
60
  end
64
61
  end
65
62
  ```
@@ -87,18 +84,46 @@ class DelegatedClass
87
84
  end
88
85
  ```
89
86
 
87
+ ## Options
88
+
89
+ ### Skip specific formats
90
+
91
+ By default, FourOFour will catch any request format. A parameter can be passed to the middleware initialization in order to skip specific formats. The middleware will then ignore these request types.
92
+
93
+ Example: throw normal 404 page for `json` and `xml` formats:
94
+ ```ruby
95
+ config.middleware.insert_before ActionDispatch::RemoteIp, FourOFour, 'DelegatedClass',
96
+ ['application/json', 'application/xml']
97
+ ```
98
+
90
99
  ## Development
91
100
 
92
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
101
+ After checking out the repo, run `bin/setup` to install dependencies. Then,
102
+ run `rake spec` to run the tests. You can also run `bin/console` for an
103
+ interactive prompt that will allow you to experiment.
93
104
 
94
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
105
+ To install this gem onto your local machine, run `bundle exec rake install`.
106
+ To release a new version, update the version number in `version.rb`, and then
107
+ run `bundle exec rake release`, which will create a git tag for the version,
108
+ push git commits and tags, and push the `.gem` file to [rubygems.org][rubygems].
95
109
 
96
110
  ## Contributing
97
111
 
98
- Bug reports and pull requests are welcome on GitHub at https://github.com/esparta/four_o_four. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
112
+ Bug reports and pull requests are welcome on GitHub at
113
+ https://github.com/SparkHub/four_o_four. This project is intended to be a safe,
114
+ welcoming space for collaboration, and contributors are expected to adhere to
115
+ the [Contributor Covenant][covenant] code of conduct.
99
116
 
100
117
 
101
118
  ## License
102
119
 
103
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
120
+ The gem is available as open source under the terms of the
121
+ [MIT License][license]
104
122
 
123
+ [repo]: https://github.com/SparkHub/four_o_four
124
+ [travis]: https://travis-ci.org/SparkHub/four_o_four.svg?branch=master
125
+ [gpa]: https://codeclimate.com/github/SparkHub/four_o_four/badges/gpa.svg
126
+ [coverage]: https://codeclimate.com/github/SparkHub/four_o_four/badges/coverage.svg
127
+ [license]: http://opensource.org/licenses/MIT
128
+ [rubygems]: https://rubygems.org
129
+ [covenant]: http://contributor-covenant.org
data/four_o_four.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'four_o_four'
7
- spec.version = '0.0.1'
7
+ spec.version = '0.0.2'
8
8
  spec.authors = ['Espartaco Palma']
9
9
  spec.email = ['esparta@gmail.com']
10
10
 
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
17
17
  class for a delegation, separating all the concerns. Routes and logic should
18
18
  have their own space.
19
19
  DESCRIPTION
20
- spec.homepage = 'https://github.com/esparta/four_o_four'
20
+ spec.homepage = 'https://github.com/SparkHub/four_o_four'
21
21
  spec.license = 'MIT'
22
22
 
23
23
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
@@ -27,13 +27,13 @@ Gem::Specification.new do |spec|
27
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
28
  spec.require_paths = ['lib']
29
29
 
30
- spec.add_dependency 'rack'
31
- spec.add_dependency 'activesupport'
30
+ spec.add_dependency 'rack', '~> 1.6.4'
32
31
  spec.add_development_dependency 'bundler', '~> 1.11'
33
32
  spec.add_development_dependency 'rake', '~> 10.0'
34
33
  spec.add_development_dependency 'rspec', '~> 3.0'
35
- spec.add_development_dependency 'rspec-nc'
36
- spec.add_development_dependency 'guard'
34
+ spec.add_development_dependency 'rspec-nc', '~> 0.2.1'
35
+ spec.add_development_dependency 'listen', '~> 3.0.6'
36
+ spec.add_development_dependency 'guard', '~> 2.13.0'
37
37
  spec.add_development_dependency 'guard-rspec'
38
38
  spec.add_development_dependency 'pry'
39
39
  spec.add_development_dependency 'pry-remote'
data/lib/four_o_four.rb CHANGED
@@ -1,17 +1,21 @@
1
1
  # Redirector...
2
- require 'active_support/inflector'
2
+ require 'utils/asset'
3
+
3
4
  # A small Rack middleware to handle 404 responses with a delegation
4
5
  class FourOFour
5
6
  VERSION = '0.0.2'
6
- def initialize(app, delegation = nil)
7
+
8
+ def initialize(app, delegation = nil, skip_formats = [])
7
9
  @app = app
8
- @delegation = delegation || "#{self.class}::DefaultApplication"
10
+ @delegation = (delegation || "#{self.class}::DefaultApplication").to_s
11
+ @skip_formats = Regexp.union(Array(skip_formats))
9
12
  end
10
13
 
11
14
  def call(env)
12
15
  status, headers, response = @app.call(env)
13
- if 404 == status
14
- @delegation.constantize.new.call(env)
16
+
17
+ if !Utils::Asset.match(env['REQUEST_URI']) && can_process_request?(status, headers)
18
+ Object.const_get(@delegation).new.call(env)
15
19
  else
16
20
  [status, headers, response]
17
21
  end
@@ -31,4 +35,30 @@ class FourOFour
31
35
  ''')
32
36
  end
33
37
  end
38
+
39
+ private
40
+
41
+ # Test if the current request can be processed by four_o_four.
42
+ # It will depend on the request and if any skipped formats have been set.
43
+ #
44
+ # @param [Integer] status Status of the current request.
45
+ # @param [Rack::Utils::HeaderHash] headers Header of the current request.
46
+ # @return [Boolean]
47
+ #
48
+ # @api private
49
+ def can_process_request?(status, headers)
50
+ content_type = headers['Content-Type']
51
+
52
+ 404 == status && processable_type?(content_type)
53
+ end
54
+
55
+ # Return true if the request type can be processed (try to match with possible skipped formats).
56
+ #
57
+ # @param [String] content_type Header's content type value.
58
+ # @return [Boolean]
59
+ #
60
+ # @api private
61
+ def processable_type?(content_type)
62
+ content_type.nil? || (content_type && content_type !~ @skip_formats)
63
+ end
34
64
  end
@@ -0,0 +1,22 @@
1
+ module Utils
2
+ module Asset
3
+
4
+ class << self
5
+
6
+ # Return true if the request is an asset returned by the server
7
+ # by matching the extension.
8
+ #
9
+ # @return [Boolean]
10
+ #
11
+ # @api public
12
+ def match(path)
13
+ !(path !~ /\.(?:p?jpe?g?|png|gif|bmp|webp|tiff|ico|
14
+ avi|wmv|flv|mpe?g?|mp4|
15
+ wav|mp3|ogg|
16
+ font|eot|ttf|woff|svg|
17
+ js|css|te?xt|yaml|zip|xml|rss|pdf|csv|htc|msg|swf|
18
+ docx?|dotx?|xla|xlsx?|xltx?|potx?|pptx?|mdbx?)$/xi)
19
+ end
20
+ end
21
+ end
22
+ end
metadata CHANGED
@@ -1,43 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: four_o_four
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Espartaco Palma
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-16 00:00:00.000000000 Z
11
+ date: 2016-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - '>='
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: activesupport
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - '>='
17
+ - - ~>
32
18
  - !ruby/object:Gem::Version
33
- version: '0'
19
+ version: 1.6.4
34
20
  type: :runtime
35
21
  prerelease: false
36
22
  version_requirements: !ruby/object:Gem::Requirement
37
23
  requirements:
38
- - - '>='
24
+ - - ~>
39
25
  - !ruby/object:Gem::Version
40
- version: '0'
26
+ version: 1.6.4
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: bundler
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -84,30 +70,44 @@ dependencies:
84
70
  name: rspec-nc
85
71
  requirement: !ruby/object:Gem::Requirement
86
72
  requirements:
87
- - - '>='
73
+ - - ~>
88
74
  - !ruby/object:Gem::Version
89
- version: '0'
75
+ version: 0.2.1
90
76
  type: :development
91
77
  prerelease: false
92
78
  version_requirements: !ruby/object:Gem::Requirement
93
79
  requirements:
94
- - - '>='
80
+ - - ~>
95
81
  - !ruby/object:Gem::Version
96
- version: '0'
82
+ version: 0.2.1
83
+ - !ruby/object:Gem::Dependency
84
+ name: listen
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 3.0.6
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 3.0.6
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: guard
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '>='
101
+ - - ~>
102
102
  - !ruby/object:Gem::Version
103
- version: '0'
103
+ version: 2.13.0
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - '>='
108
+ - - ~>
109
109
  - !ruby/object:Gem::Version
110
- version: '0'
110
+ version: 2.13.0
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: guard-rspec
113
113
  requirement: !ruby/object:Gem::Requirement
@@ -203,7 +203,8 @@ files:
203
203
  - bin/setup
204
204
  - four_o_four.gemspec
205
205
  - lib/four_o_four.rb
206
- homepage: https://github.com/esparta/four_o_four
206
+ - lib/utils/asset.rb
207
+ homepage: https://github.com/SparkHub/four_o_four
207
208
  licenses:
208
209
  - MIT
209
210
  metadata: {}
@@ -223,7 +224,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
223
224
  version: '0'
224
225
  requirements: []
225
226
  rubyforge_project:
226
- rubygems_version: 2.2.2
227
+ rubygems_version: 2.4.8
227
228
  signing_key:
228
229
  specification_version: 4
229
230
  summary: FourOFour, a simple Rack middleware to handle 404 responses with delegation