quantipay-foreign_domain_routing 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2008 Michael Bleigh (http://www.mbleigh.com) and
2
+ Intridea, Inc (http://www.intridea.com)
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,123 @@
1
+ Foreign Domain Routing +
2
+ Request Routing Plugin for Ruby on Rails
3
+ =========================================
4
+ = Foreign Domain Request Routing
5
+
6
+ -------------------------------------------------------------------------------
7
+ Foreign Domain Routing provides simple handling of foreign domains in Rails.
8
+ It borrows largely from SubdomainFu and some other snippets found around the web.
9
+
10
+ Request routing allows you to define routing conditions that test
11
+ methods/properties of the request object such as subdomain, domain,
12
+ port. You can test them either against a value or with a Regexp
13
+ (assuming the method returns a string)
14
+
15
+ Merging these two plugins allows them to work together and enhance the routing
16
+ capability of Rails.
17
+
18
+ Documentation from the original plugins follows...
19
+
20
+ -------------------------------------------------------------------------------
21
+ Foreign Domain Routing
22
+ -------------------------------------------------------------------------------
23
+
24
+ Installation
25
+ ============
26
+
27
+ Foreign Domain Routing is available as a plugin. To install it with Rails 2.1
28
+ or later):
29
+
30
+ script/plugin install git://github.com/brianmulloy/foreign-domain-routing.git
31
+
32
+ Examples
33
+ ========
34
+
35
+ Foreign Domain Routing extends Rails's routing mechanisms to provide a way to
36
+ redirect non-native domains.
37
+
38
+ Let's say my rails app domain is 'mydomain.com' and I am creating an app that
39
+ allows users to add a CNAME record to map their subdomain so that
40
+ 'foo.usersdomain.com' would actually point to 'mydomain.com/users/1234'.
41
+
42
+ The route at the top of config/routes.rb would look like:
43
+
44
+ map.connect '*path', :controller => 'users',
45
+ :action => 'index', :conditions => { :foreign_domain => true }
46
+
47
+ And in the users controller:
48
+
49
+ @user = User.find_by_foreign_domain(request.host.downcase)
50
+ # this example would require a database field called foreign_domain
51
+
52
+ Configuration
53
+ =============
54
+
55
+ You will need to configure Foreign Domain Routing based on your native hostnames.
56
+
57
+ native_domains
58
+ --------
59
+
60
+ A hash of arrays for the native domain names for each relevant environment.
61
+
62
+ Create the file config/initializers/native_domains.rb and put something like:
63
+
64
+ ForeignDomainRouting.init_native_domains = {
65
+ :development => ['localhost'],
66
+ :staging => ['staging.example.com'],
67
+ :production => ['example.com', 'example.org', 'example.net']
68
+ } # set all at once (also the defaults)
69
+
70
+
71
+ Or set the native domains on the fly with:
72
+
73
+ ForeignDomainRouting.native_domains =
74
+ ['example.com', 'example.org', 'example.net'] # sets for current environment
75
+
76
+ Resources
77
+ =========
78
+
79
+ * GitHub Repository: http://github.com/brianmulloy/foreign_domain_routing
80
+ * 2008 by Brian Mulloy (http://landlessness.net/). Released under the MIT license.
81
+
82
+
83
+ *******************************************************************************
84
+
85
+ -------------------------------------------------------------------------------
86
+ Request Routing Plugin for Ruby on Rails
87
+ -------------------------------------------------------------------------------
88
+ (c) Dan Webb 2006 (dan@vivabit.com)
89
+
90
+ Plugin that allows you to define routing conditions that test
91
+ methods/properties of the request object such as subdomain, domain,
92
+ port. You can test them either against a value or with a Regexp
93
+ (assuming the method returns a string)
94
+
95
+ *UPDATE* Now works with the new routing code as implemented in edge rails. Note the
96
+ change in API: use :conditions instead of :requirements.
97
+
98
+ == Installation
99
+
100
+ ruby script/plugin install http://svn.vivabit.net/external/rubylibs/request_routing/
101
+
102
+ == Usage
103
+
104
+ In routes.rb you can specify use the :requirements hash with request properties:
105
+
106
+ map.connect '', :controller => 'main', :conditions => { :subdomain => 'www' }
107
+
108
+ map.connect 'admin', :controller => 'admin', :conditions => { :remote_ip => /^127\.0\.0\.[0-9]$/ }
109
+
110
+ You can also, of course, use the conditions hash in map.with_options calls.
111
+
112
+ The allowed properties are:
113
+
114
+ :subdomain (only checks the first subdomain)
115
+ :domain (only accurate for single tld domain names at the moment)
116
+ :method (a symbol)
117
+ :port (a number)
118
+ :remote_ip
119
+ :content_type (content type of the post body)
120
+ :accepts
121
+ :request_uri (the entire request uri)
122
+ :protocol (either http:// or https://)
123
+
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the foreign_domain_routing plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the foreign_domain_routing plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'Foreign Domain Routing'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init"
@@ -0,0 +1,34 @@
1
+ require 'foreign_domain_routing/routing_extensions'
2
+
3
+ module ForeignDomainRouting
4
+ DEFAULT_NATIVE_DOMAINS = {:development => ['localhost:3000'], :test => ['test.host'], :production => ['example.com'] }
5
+ mattr_accessor :init_native_domains
6
+ @@init_native_domains = DEFAULT_NATIVE_DOMAINS.dup
7
+
8
+ def self.native_domains
9
+ init_native_domains[RAILS_ENV.to_sym]
10
+ end
11
+
12
+ def self.native_domains=(value)
13
+ init_native_domains[RAILS_ENV.to_sym] = value
14
+ end
15
+
16
+ def self.foreign_domain?(host)
17
+ native_domains.each do |domain|
18
+ return false if host =~ /#{domain}\Z/i
19
+ end
20
+ true
21
+ end
22
+
23
+ module Controller
24
+ def self.included(controller)
25
+ controller.helper_method(:foreign_domain?)
26
+ end
27
+
28
+ protected
29
+
30
+ def foreign_domain?
31
+ ForeignDomainRouting.foreign_domain?(request.host)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,55 @@
1
+ module ForeignDomainRouting
2
+ module RouteExtensions
3
+
4
+ TESTABLE_REQUEST_METHODS = [:subdomain, :domain, :method, :port, :remote_ip,
5
+ :content_type, :accepts, :request_uri, :protocol]
6
+
7
+ def self.included(base)
8
+ base.alias_method_chain :recognition_conditions, :foreign_domain
9
+ end
10
+
11
+ def recognition_conditions_with_foreign_domain
12
+ result = recognition_conditions_without_foreign_domain
13
+ result << "ForeignDomainRouting.foreign_domain?(env[:host])" if conditions[:foreign_domain] == true
14
+ result << "!ForeignDomainRouting.foreign_domain?(env[:host])" if conditions[:foreign_domain] == false
15
+
16
+ conditions.each do |method, value|
17
+ if TESTABLE_REQUEST_METHODS.include? method
18
+ result << if value.is_a? Regexp
19
+ "conditions[#{method.inspect}] =~ env[#{method.inspect}]"
20
+ else
21
+ "conditions[#{method.inspect}] === env[#{method.inspect}]"
22
+ end
23
+ else
24
+ end
25
+ end
26
+
27
+
28
+ result
29
+ end
30
+ end
31
+
32
+ module RouteSetExtensions
33
+ def self.included(base)
34
+ base.alias_method_chain :extract_request_environment, :foreign_domain
35
+ end
36
+
37
+ def extract_request_environment_with_foreign_domain(request)
38
+ extract_request_environment_without_foreign_domain(request).merge({
39
+ :host => request.host,
40
+ :method => request.method,
41
+ :subdomain => request.subdomains.first.to_s,
42
+ :domain => request.domain,
43
+ :port => request.port,
44
+ :remote_ip => request.remote_ip,
45
+ :content_type => request.content_type,
46
+ :accepts => request.accepts.map(&:to_s).join(','),
47
+ :request_uri => request.request_uri,
48
+ :protocol => request.protocol
49
+ })
50
+ end
51
+ end
52
+ end
53
+
54
+ ActionController::Routing::RouteSet.send :include, ForeignDomainRouting::RouteSetExtensions
55
+ ActionController::Routing::Route.send :include, ForeignDomainRouting::RouteExtensions
data/rails/init.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'foreign_domain_routing'
2
+
3
+ ActionController::Base.send :include, ForeignDomainRouting::Controller
4
+
5
+ # RAILS_DEFAULT_LOGGER.info("** ForeignDomainRouting: initialized properly")
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :foreign_domain_routing do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,137 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'action_controller'
4
+
5
+ require File.dirname(__FILE__) + "/../init"
6
+ RAILS_ENV = :test
7
+
8
+ class TestController < Class.new(ActionController::Base)
9
+ def thing
10
+ end
11
+ end
12
+
13
+ class OtherTestController < Class.new(ActionController::Base)
14
+ def thing
15
+ end
16
+ end
17
+
18
+ class MockRequest < Struct.new(:path, :subdomains, :method, :remote_ip, :protocol, :path_parameters, :domain, :port, :content_type, :accepts, :request_uri, :host)
19
+ end
20
+
21
+ class ForeignDomainRoutingTest < ActionController::TestCase
22
+ attr_reader :rs
23
+ def setup
24
+ @rs = ::ActionController::Routing::RouteSet.new
25
+ ActionController::Routing.use_controllers! %w(test) if ActionController::Routing.respond_to? :use_controllers!
26
+ @rs.draw {|m| m.connect ':controller/:action/:id' }
27
+ @request = MockRequest.new(
28
+ '',
29
+ ['www'],
30
+ :post,
31
+ '1.2.3.4',
32
+ 'http://',
33
+ '',
34
+ 'thing.com',
35
+ 3432,
36
+ 'text/html',
37
+ ['*/*'],
38
+ '/',
39
+ 'www.example.com'
40
+ )
41
+ end
42
+
43
+ test "should route normally" do
44
+ assert_raise(ActionController::RoutingError) do
45
+ @rs.recognize(@request)
46
+ end
47
+
48
+ @request.path = '/test/thing'
49
+ assert(@rs.recognize(@request))
50
+ end
51
+
52
+ test "should route conditionally on subdomain" do
53
+ @rs.draw { |m| m.connect 'thing', :controller => 'test', :conditions => { :subdomain => 'www' } }
54
+ @request.path = '/thing'
55
+ assert(@rs.recognize(@request))
56
+ @request.subdomains = ['sdkg']
57
+ assert_raise(ActionController::RoutingError) do
58
+ @rs.recognize(@request)
59
+ end
60
+ end
61
+
62
+ test "should route conditionally on protocol" do
63
+ @rs.draw { |m| m.connect 'thing', :controller => 'test', :conditions => { :protocol => /^https:/ } }
64
+ @request.path = '/thing'
65
+ assert_raise(ActionController::RoutingError) do
66
+ @rs.recognize(@request)
67
+ end
68
+
69
+ @request.protocol = "https://"
70
+ assert(@rs.recognize(@request))
71
+ end
72
+
73
+ test "should route conditionally on alternate conditionals" do
74
+ @rs.draw { |m|
75
+ m.connect 'thing', :controller => 'test', :conditions => { :remote_ip => '1.2.3.4' }
76
+ m.connect 'thing', :controller => 'other_test', :conditions => { :remote_ip => '1.2.3.5' }
77
+ }
78
+
79
+ @request.path = '/thing'
80
+ assert(@rs.recognize(@request))
81
+
82
+ @request.remote_ip = '1.2.3.5'
83
+ assert(@rs.recognize(@request))
84
+ end
85
+
86
+ test "should route conditionally on foreign domain" do
87
+ ForeignDomainRouting.init_native_domains = {
88
+ :development => ['localhost'],
89
+ :test => ['www.example.com'],
90
+ :production => ['example.com', 'example.org', 'example.net']
91
+ }
92
+
93
+ @rs.draw { |m| m.connect 'thing', :controller => 'test', :conditions => { :foreign_domain => false } }
94
+ @request.path = '/thing'
95
+ assert(@rs.recognize(@request))
96
+ @request.host = ['foreign.domain.com']
97
+ assert_raise(ActionController::RoutingError) do
98
+ @rs.recognize(@request)
99
+ end
100
+ @rs.draw { |m| m.connect 'thing', :controller => 'test', :conditions => { :foreign_domain => true } }
101
+ @request.path = '/thing'
102
+ assert(@rs.recognize(@request))
103
+ end
104
+
105
+ test "should route conditionally on foreign domain and protocol" do
106
+ ForeignDomainRouting.init_native_domains = {
107
+ :development => ['localhost'],
108
+ :test => ['www.example.com'],
109
+ :production => ['example.com', 'example.org', 'example.net']
110
+ }
111
+
112
+ @rs.draw { |m| m.connect 'thing', :controller => 'test', :conditions => { :foreign_domain => false, :protocol => /^http:/ } }
113
+ @request.path = '/thing'
114
+ # :foreign_domain => false, :protocol => http:// (MATCH)
115
+ assert(@rs.recognize(@request))
116
+
117
+ # :foreign_domain => false, :protocol => https:// (NO MATCH)
118
+ @request.protocol = "https://"
119
+ assert_raise(ActionController::RoutingError) do
120
+ @rs.recognize(@request)
121
+ end
122
+
123
+ # :foreign_domain => true, :protocol => http:// (NO MATCH)
124
+ @request.host = ['foreign.domain.com']
125
+ @request.protocol = "http://"
126
+ assert_raise(ActionController::RoutingError) do
127
+ @rs.recognize(@request)
128
+ end
129
+
130
+ # :foreign_domain => true, :protocol => https:// (NO MATCH)
131
+ @request.protocol = "https://"
132
+ assert_raise(ActionController::RoutingError) do
133
+ @rs.recognize(@request)
134
+ end
135
+
136
+ end
137
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quantipay-foreign_domain_routing
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Multiple
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-04 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: This version of foreign_domain_routing merges foreign_domain_routing with request_routing
17
+ email: joe@quantipay.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - init.rb
26
+ - lib
27
+ - lib/foreign_domain_routing
28
+ - lib/foreign_domain_routing/routing_extensions.rb
29
+ - lib/foreign_domain_routing.rb
30
+ - rails
31
+ - rails/init.rb
32
+ - tasks
33
+ - tasks/foreign_domain_routing_tasks.rake
34
+ - test
35
+ - test/foreign_domain_routing_test.rb
36
+ - MIT-LICENSE
37
+ - Rakefile
38
+ - README
39
+ has_rdoc: true
40
+ homepage: http://github.com/quantipay/foreign_domain_routing
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --inline-source
44
+ - --charset=UTF-8
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project: foreign_domain_routing
62
+ rubygems_version: 1.2.0
63
+ signing_key:
64
+ specification_version: 2
65
+ summary: This version of foreign_domain_routing merges foreign_domain_routing with request_routing
66
+ test_files: []
67
+