41b76a0d9fd81133d545fb2b994013ab

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.

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 !

82896ae7662ba5391678578911d5a9b4

mikhailov

April 27, 2010, April 27, 2010 11:39, permalink

No rating. Login to rate!

I usually use of third-level domains, so this my version of codesource

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
82896ae7662ba5391678578911d5a9b4

mikhailov

April 27, 2010, April 27, 2010 11:41, permalink

No rating. Login to rate!

I usually use of third-level domains, so this my version of codesource

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
00bb568ccadc6ede0191b4dc7477b104

Chinese Zodiac

October 16, 2010, October 16, 2010 02:55, permalink

No rating. Login to rate!

"How-do-you-do, just needed you to learn We've extra your internet site to my Yahoo bookmarks because of your remarkable weblog layout. But severely, I assume your internet site has a single of the freshest theme I've came across. It genuinely helps make studying your blog a great deal simpler."

Da4546ca8c94d34b8b1ac90015ee2c9a

harley davidson

November 2, 2010, November 02, 2010 06:04, permalink

No rating. Login to rate!

The man who has made up his mind to win will never say "impossible ".

Your refactoring





Format Copy from initial code

or Cancel