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 = "joe@quantipay.com"
10
+ gem.homepage = "http://github.com/quantipay/foreign_domain_routing"
11
+ gem.authors = ["Joe Scharf"]
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,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
@@ -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,135 @@
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
+ 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,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: foreign_domain_routing
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Joe Scharf
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-06 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: joe@quantipay.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - Rakefile
26
+ - VERSION.yml
27
+ - lib/foreign_domain_routing.rb
28
+ - lib/foreign_domain_routing/routing_extensions.rb
29
+ - rails/init.rb
30
+ - test/foreign_domain_routing_test.rb
31
+ - test/test_helper.rb
32
+ - README
33
+ has_rdoc: true
34
+ homepage: http://github.com/quantipay/foreign_domain_routing
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --charset=UTF-8
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.3.5
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: This version of foreign_domain_routing merges foreign_domain_routing with request_routing
61
+ test_files:
62
+ - test/foreign_domain_routing_test.rb
63
+ - test/test_helper.rb