1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
module Rack class Request def subdomains @env['rack.env.subdomains'] ||= lambda { return [] if (host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host)) host.split('.')[0...-2] }.call end end end before do @purl_base = if request.subdomains.empty? then request.host else request.host.sub("#{request.subdomains.join('.')}.", "") end #personalurl.com @purl = Purl.first(:purl => @purl_base) Purl.current = @purl @user = CData.first(:unique_name => request.subdomains.join('.')) unless request.subdomains.empty? #g.manley end get '/' do if request.subdomains.empty? then erb :"#{@purl.name}/blank" else erb :"#{@purl.name}/index" end end
Refactorings
No refactoring yet !
mikhailov
April 27, 2010, April 27, 2010 11:39, permalink
I usually use of third-level domains, so this my version of codesource
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
module Rack class Request def subdomain sub = host.present? && host.split('.')[0...-2] sub.one? && sub.first || nil end end end class ApiVersion1 < Sinatra::Application get '/api/v1/company.xml' do company = Company.find_by_subdomain(request.subdomain) headers 'Content-Type' => 'text/xml' company.to_xml end end
mikhailov
April 27, 2010, April 27, 2010 11:41, permalink
I usually use of third-level domains, so this my version of codesource
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
module Rack class Request def subdomain sub = host.present? && host.split('.')[0...-2] sub.one? && sub.first || nil end end end class ApiVersion1 < Sinatra::Application get '/api/v1/company.xml' do company = Company.find_by_subdomain(request.subdomain) headers 'Content-Type' => 'text/xml' company.to_xml end end
So i need to define @purl_base and @user. @user needs to be defined only if the url navigated to has a subdomain (g.manley.personalurl.com). @purl_base is currently being defined by sub'ing out the subdomain but if there is no subdomain then obviously it doesn't need to be subbed out. I currently have a semi working version of it but its pretty clunky and wanted to see if there was a better way like maybe even a rack function that i missed that defines the base url.
Its a sinatra app using datamapper.