autodata-foreign-domain-routing 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,126 @@
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
+
124
+ == Copyright
125
+
126
+ Copyright (c) 2009 Joe Scharf. See LICENSE for details.
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "foreign-domain-routing"
8
+ gem.summary = %Q{This version of foreign_domain_routing merges foreign_domain_routing with request_routing}
9
+ gem.email = "william@gmail.com"
10
+ gem.homepage = "http://github.com/autodata/foreign-domain-routing"
11
+ gem.authors = ["Brian Mulloy", "Joe Scharf", "William Melody"]
12
+
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = false
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+
40
+ task :default => :test
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ if File.exist?('VERSION.yml')
45
+ config = YAML.load(File.read('VERSION.yml'))
46
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "foreign-domain-routing-gem #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
56
+
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 4
3
+ :major: 1
4
+ :minor: 0
@@ -0,0 +1,46 @@
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
+ def self.foreign_fqdn?(host)
24
+ native_domains.each do |domain|
25
+ return false if host =~ /\A#{domain}\Z/i
26
+ end
27
+ true
28
+ end
29
+
30
+ module Controller
31
+ def self.included(controller)
32
+ controller.helper_method(:foreign_domain?)
33
+ controller.helper_method(:foreign_fqdn?)
34
+ end
35
+
36
+ protected
37
+
38
+ def foreign_domain?
39
+ ForeignDomainRouting.foreign_domain?(request.host)
40
+ end
41
+
42
+ def foreign_fqdn?
43
+ ForeignDomainRouting.foreign_fqdn?(request.host)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,57 @@
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
+ result << "ForeignDomainRouting.foreign_fqdn?(env[:host])" if conditions[:foreign_fqdn] == true
16
+ result << "!ForeignDomainRouting.foreign_fqdn?(env[:host])" if conditions[:foreign_fqdn] == false
17
+
18
+ conditions.each do |method, value|
19
+ if TESTABLE_REQUEST_METHODS.include? method
20
+ result << if value.is_a? Regexp
21
+ "conditions[#{method.inspect}] =~ env[#{method.inspect}]"
22
+ else
23
+ "conditions[#{method.inspect}] === env[#{method.inspect}]"
24
+ end
25
+ else
26
+ end
27
+ end
28
+
29
+
30
+ result
31
+ end
32
+ end
33
+
34
+ module RouteSetExtensions
35
+ def self.included(base)
36
+ base.alias_method_chain :extract_request_environment, :foreign_domain
37
+ end
38
+
39
+ def extract_request_environment_with_foreign_domain(request)
40
+ extract_request_environment_without_foreign_domain(request).merge({
41
+ :host => request.host,
42
+ :method => request.method,
43
+ :subdomain => request.subdomains.first.to_s,
44
+ :domain => request.domain,
45
+ :port => request.port,
46
+ :remote_ip => request.remote_ip,
47
+ :content_type => request.content_type,
48
+ :accepts => request.accepts.map(&:to_s).join(','),
49
+ :request_uri => request.request_uri,
50
+ :protocol => request.protocol
51
+ })
52
+ end
53
+ end
54
+ end
55
+
56
+ ActionController::Routing::RouteSet.send :include, ForeignDomainRouting::RouteSetExtensions
57
+ ActionController::Routing::Route.send :include, ForeignDomainRouting::RouteExtensions
@@ -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,168 @@
1
+ require 'test_helper'
2
+
3
+ require File.dirname(__FILE__) + "/../init"
4
+ RAILS_ENV = :test
5
+
6
+ class TestController < Class.new(ActionController::Base)
7
+ def thing
8
+ end
9
+ end
10
+
11
+ class OtherTestController < Class.new(ActionController::Base)
12
+ def thing
13
+ end
14
+ end
15
+
16
+ class MockRequest < Struct.new(:path, :subdomains, :method, :remote_ip, :protocol, :path_parameters, :domain, :port, :content_type, :accepts, :request_uri, :host)
17
+ end
18
+
19
+ class ForeignDomainRoutingTest < ActionController::TestCase
20
+ attr_reader :rs
21
+ def setup
22
+ @rs = ::ActionController::Routing::RouteSet.new
23
+ ActionController::Routing.use_controllers! %w(test) if ActionController::Routing.respond_to? :use_controllers!
24
+ @rs.draw {|m| m.connect ':controller/:action/:id' }
25
+ @request = MockRequest.new(
26
+ '',
27
+ ['www'],
28
+ :post,
29
+ '1.2.3.4',
30
+ 'http://',
31
+ '',
32
+ 'thing.com',
33
+ 3432,
34
+ 'text/html',
35
+ ['*/*'],
36
+ '/',
37
+ 'www.example.com'
38
+ )
39
+ end
40
+
41
+ test "should route normally" do
42
+ assert_raise(ActionController::RoutingError) do
43
+ @rs.recognize(@request)
44
+ end
45
+
46
+ @request.path = '/test/thing'
47
+ assert(@rs.recognize(@request))
48
+ end
49
+
50
+ test "should route conditionally on subdomain" do
51
+ @rs.draw { |m| m.connect 'thing', :controller => 'test', :conditions => { :subdomain => 'www' } }
52
+ @request.path = '/thing'
53
+ assert(@rs.recognize(@request))
54
+ @request.subdomains = ['sdkg']
55
+ assert_raise(ActionController::RoutingError) do
56
+ @rs.recognize(@request)
57
+ end
58
+ end
59
+
60
+ test "should route conditionally on protocol" do
61
+ @rs.draw { |m| m.connect 'thing', :controller => 'test', :conditions => { :protocol => /^https:/ } }
62
+ @request.path = '/thing'
63
+ assert_raise(ActionController::RoutingError) do
64
+ @rs.recognize(@request)
65
+ end
66
+
67
+ @request.protocol = "https://"
68
+ assert(@rs.recognize(@request))
69
+ end
70
+
71
+ test "should route conditionally on alternate conditionals" do
72
+ @rs.draw { |m|
73
+ m.connect 'thing', :controller => 'test', :conditions => { :remote_ip => '1.2.3.4' }
74
+ m.connect 'thing', :controller => 'other_test', :conditions => { :remote_ip => '1.2.3.5' }
75
+ }
76
+
77
+ @request.path = '/thing'
78
+ assert(@rs.recognize(@request))
79
+
80
+ @request.remote_ip = '1.2.3.5'
81
+ assert(@rs.recognize(@request))
82
+ end
83
+
84
+ test "should route conditionally on foreign domain" do
85
+ ForeignDomainRouting.init_native_domains = {
86
+ :development => ['localhost'],
87
+ :test => ['www.example.com'],
88
+ :production => ['example.com', 'example.org', 'example.net']
89
+ }
90
+
91
+ @rs.draw { |m| m.connect 'thing', :controller => 'test', :conditions => { :foreign_domain => false } }
92
+ @request.path = '/thing'
93
+ assert(@rs.recognize(@request))
94
+ @request.host = ['foreign.domain.com']
95
+ assert_raise(ActionController::RoutingError) do
96
+ @rs.recognize(@request)
97
+ end
98
+ @rs.draw { |m| m.connect 'thing', :controller => 'test', :conditions => { :foreign_domain => true } }
99
+ @request.path = '/thing'
100
+ assert(@rs.recognize(@request))
101
+ end
102
+
103
+ test "should route conditionally on foreign domain and protocol" do
104
+ ForeignDomainRouting.init_native_domains = {
105
+ :development => ['localhost'],
106
+ :test => ['www.example.com'],
107
+ :production => ['example.com', 'example.org', 'example.net']
108
+ }
109
+
110
+ @rs.draw { |m| m.connect 'thing', :controller => 'test', :conditions => { :foreign_domain => false, :protocol => /^http:/ } }
111
+ @request.path = '/thing'
112
+ # :foreign_domain => false, :protocol => http:// (MATCH)
113
+ assert(@rs.recognize(@request))
114
+
115
+ # :foreign_domain => false, :protocol => https:// (NO MATCH)
116
+ @request.protocol = "https://"
117
+ assert_raise(ActionController::RoutingError) do
118
+ @rs.recognize(@request)
119
+ end
120
+
121
+ # :foreign_domain => true, :protocol => http:// (NO MATCH)
122
+ @request.host = ['foreign.domain.com']
123
+ @request.protocol = "http://"
124
+ assert_raise(ActionController::RoutingError) do
125
+ @rs.recognize(@request)
126
+ end
127
+
128
+ # :foreign_domain => true, :protocol => https:// (NO MATCH)
129
+ @request.protocol = "https://"
130
+ assert_raise(ActionController::RoutingError) do
131
+ @rs.recognize(@request)
132
+ end
133
+
134
+ end
135
+
136
+ test "should route conditionally on foreign fqdn and protocol" do
137
+ ForeignDomainRouting.init_native_domains = {
138
+ :development => ['localhost'],
139
+ :test => ['www.example.com'],
140
+ :production => ['example.com', 'example.org', 'example.net']
141
+ }
142
+
143
+ @rs.draw { |m| m.connect 'thing', :controller => 'test', :conditions => { :foreign_fqdn => false, :protocol => /^http:/ } }
144
+ @request.path = '/thing'
145
+ # :foreign_fqdn => false, :protocol => http:// (MATCH)
146
+ assert(@rs.recognize(@request))
147
+
148
+ # :foreign_fqdn => false, :protocol => https:// (NO MATCH)
149
+ @request.protocol = "https://"
150
+ assert_raise(ActionController::RoutingError) do
151
+ @rs.recognize(@request)
152
+ end
153
+
154
+ # :foreign_fqdn => true, :protocol => http:// (NO MATCH)
155
+ @request.host = ['foreign.example.com']
156
+ @request.protocol = "http://"
157
+ assert_raise(ActionController::RoutingError) do
158
+ @rs.recognize(@request)
159
+ end
160
+
161
+ # :foreign_fqdn => true, :protocol => https:// (NO MATCH)
162
+ @request.protocol = "https://"
163
+ assert_raise(ActionController::RoutingError) do
164
+ @rs.recognize(@request)
165
+ end
166
+
167
+ end
168
+ end
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'action_controller'
5
+
6
+
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
9
+ require 'foreign_domain_routing'
10
+
11
+ class Test::Unit::TestCase
12
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: autodata-foreign-domain-routing
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Brian Mulloy
8
+ - Joe Scharf
9
+ - William Melody
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2009-09-03 00:00:00 -07:00
15
+ default_executable:
16
+ dependencies: []
17
+
18
+ description:
19
+ email: william@gmail.com
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files:
25
+ - README
26
+ files:
27
+ - Rakefile
28
+ - VERSION.yml
29
+ - lib/foreign_domain_routing.rb
30
+ - lib/foreign_domain_routing/routing_extensions.rb
31
+ - rails/init.rb
32
+ - test/foreign_domain_routing_test.rb
33
+ - test/test_helper.rb
34
+ - README
35
+ has_rdoc: true
36
+ homepage: http://github.com/autodata/foreign-domain-routing
37
+ licenses:
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --charset=UTF-8
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.3.5
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: This version of foreign_domain_routing merges foreign_domain_routing with request_routing
62
+ test_files:
63
+ - test/foreign_domain_routing_test.rb
64
+ - test/test_helper.rb