rack_redirect 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a08ee00bb4eeb0b9263087dc3937a44dc634cf67
4
+ data.tar.gz: 4dd0f63bb894f92e324be6169464dc348fa7fb2f
5
+ SHA512:
6
+ metadata.gz: 30e3f4ab86da97baaa0f1a301f4c49c0fda1525c30a451ea17b092db869b066bda58bc885822a55227e586a9a285eaa4f9c7642ecfdc4a40597a012755e0b297
7
+ data.tar.gz: 0d4e6a189bc43d13c635b896a44365a47b56ce1ef98ceab29ceed558f780bf95eb1cc3273024a77e40cf6f88f0103423070bfdba84dbd2724cafa5f9d320a05e
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,89 @@
1
+ # Rack::Redirect
2
+
3
+ Rack::Redirect, generic Rack redirect middleware.
4
+
5
+ By default to redirect requests based on path, but can be easily extended to match hostname, user-agent, or whatever might be available in the request.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rack-redirect'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ ## Usage
20
+
21
+ ### Rackup
22
+
23
+ Add to `config.ru`:
24
+
25
+ ```rb
26
+ require 'rack/redirect'
27
+ use Rack::Redirect, {
28
+ '/some/path' => '/new/path',
29
+ %r{^/profiles}' => '/users'
30
+ }
31
+
32
+ use Rack::Redirect, {
33
+ '/home' => '/'
34
+ }, 301
35
+ ```
36
+
37
+ ### Rails
38
+
39
+ Add to `config/application.rb` (or any of the environment files):
40
+
41
+ ```rb
42
+ config.middleware.insert_before 0, Rack::Redirect, {
43
+ '/some/path' => '/new/path',
44
+ %r{^/profiles}' => '/users'
45
+ }
46
+
47
+ config.middleware.insert_before 0, Rack::Redirect, {
48
+ '/home' => '/'
49
+ }, 301
50
+
51
+ ```
52
+
53
+ ## Extending
54
+
55
+ To extend the functionality for e.g. user-agent detection, it's as simple as:
56
+
57
+ ```rb
58
+ class UserAgentRedirect < Rack::Redirect
59
+ def request_matches?(request, pattern)
60
+ request.user_agent =~ pattern
61
+ end
62
+ end
63
+
64
+ use UserAgentRedirect, {
65
+ /iP(hone|od)/ => '/mobile'
66
+ }
67
+ ```
68
+
69
+ The pattern can actually be anything, so if you wan't use more variables in a single check, that's also possible:
70
+
71
+ ```rb
72
+ class UserAgentAndPathRedirect < Rack::Redirect
73
+ def request_matches?(request, pattern)
74
+ request.user_agent =~ pattern[0] && request.path == pattern[1]
75
+ end
76
+ end
77
+
78
+ use UserAgentAndPathRedirect, {
79
+ [/iP(hone|od)/, '/mobile'] => '/iphone'
80
+ }
81
+ ```
82
+
83
+ ## Development
84
+
85
+ After checking out the repo, run `bin/setup` to install dependencies.
86
+
87
+ ## Contributing
88
+
89
+ Bug reports and pull requests are welcome on GitHub at https://github.com/koenpunt/rack-redirect.
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,58 @@
1
+ require "rack"
2
+ require "rack/redirect/version"
3
+
4
+ module Rack
5
+ class Redirect
6
+
7
+ def initialize(app, pairs, status = 302)
8
+ @app = app
9
+ @pairs = pairs
10
+ @status = status
11
+ end
12
+
13
+ def call(env)
14
+ request = Rack::Request.new(env)
15
+ @pairs.each do |pattern, target|
16
+ next unless request_matches?(request, pattern)
17
+ uri = URI.parse(target)
18
+ target_params = parse_query(uri.query)
19
+ request_params = parse_query(request.query_string)
20
+ query = build_query(merge_params(request_params, target_params))
21
+ uri.query = query if query.present?
22
+ return [status, { 'Location' => uri.to_s }, ['']]
23
+ end
24
+ @app.call(env)
25
+ end
26
+
27
+ protected
28
+
29
+ def status
30
+ @status
31
+ end
32
+
33
+ def parse_query(query)
34
+ Rack::Utils.parse_nested_query(query)
35
+ end
36
+
37
+ def build_query(params)
38
+ Rack::Utils.build_nested_query(params)
39
+ end
40
+
41
+ def merge_params(request_params, target_params)
42
+ target_params.merge(request_params)
43
+ end
44
+
45
+ def request_matches?(request, pattern)
46
+ if pattern.is_a?(Regexp)
47
+ request.path =~ pattern
48
+ elsif pattern.is_a?(String)
49
+ request.path == pattern
50
+ elsif pattern.is_a?(Symbol)
51
+ request.path == pattern.to_s.downcase
52
+ else
53
+ false
54
+ end
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class Redirect
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/redirect/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack_redirect"
8
+ spec.version = Rack::Redirect::VERSION
9
+ spec.authors = ["Koen Punt"]
10
+ spec.email = ["koen@koenpunt.nl"]
11
+
12
+ spec.summary = %q{Generic Rack redirect middleware.}
13
+ spec.description = %q{Rack middleware to redirect requests based on path, but can be easily extended to match hostname, user-agent, or whatever might be available in the request.}
14
+ spec.homepage = "https://github.com/koenpunt/rack-redirect"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "bin"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+
24
+ spec.add_runtime_dependency "rack", "~> 1.4"
25
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack_redirect
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Koen Punt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.4'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.4'
55
+ description: Rack middleware to redirect requests based on path, but can be easily
56
+ extended to match hostname, user-agent, or whatever might be available in the request.
57
+ email:
58
+ - koen@koenpunt.nl
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - README.md
66
+ - Rakefile
67
+ - bin/setup
68
+ - lib/rack/redirect.rb
69
+ - lib/rack/redirect/version.rb
70
+ - rack-redirect.gemspec
71
+ homepage: https://github.com/koenpunt/rack-redirect
72
+ licenses: []
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.4.5
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Generic Rack redirect middleware.
94
+ test_files: []