diff --git a/lib/noosfero/url.rb b/lib/noosfero/url.rb new file mode 100644 index 0000000..d366812 --- /dev/null +++ b/lib/noosfero/url.rb @@ -0,0 +1,36 @@ +require 'noosfero' + +# This module defines utility methods for generating URL's in contexts where +# one does not have a request (i.e. ActionMailer classes like TaskMailer). +# +# TODO: document the use of config/web.yml in a INSTALL document +module Noosfero::URL + + class << self + + def config + if @config.nil? + config_file = File.join(RAILS_ROOT, 'config', 'web.yml') + if File.exists?(config_file) + @config = YAML::load_file(config_file) + else + @config = { + 'path' => '', + 'port' => 3000 + } + end + end + + @config + end + end + + def port + Noosfero::URL.config['port'] + end + + def path + Noosfero::URL.config['path'] + end + +end diff --git a/test/unit/noosfero_url_test.rb b/test/unit/noosfero_url_test.rb new file mode 100644 index 0000000..044a1aa --- /dev/null +++ b/test/unit/noosfero_url_test.rb @@ -0,0 +1,37 @@ + +require File.dirname(__FILE__) + '/../test_helper' + +require 'noosfero/url' + +class NoosferoURLTest < Test::Unit::TestCase + + include Noosfero::URL + + def setup + Noosfero::URL.instance_variable_set('@config', nil) + end + + should 'read the config file' do + file = "#{RAILS_ROOT}/config/web.yml" + File.expects(:exists?).with(file).returns(true) + YAML.expects(:load_file).with(file).returns('path' => '/mypath', 'port' => 9999) + assert_equal({'path' => '/mypath', 'port' => 9999}, Noosfero::URL.config) + end + + should 'fallback correcly' do + file = "#{RAILS_ROOT}/config/web.yml" + File.expects(:exists?).with(file).returns(false) + assert_equal({'path' => '', 'port' => 3000}, Noosfero::URL.config) + end + + should 'read the correct path' do + Noosfero::URL.stubs(:config).returns('path' => '/mypath') + assert_equal '/mypath', self.path + end + + should 'read the correct port' do + Noosfero::URL.stubs(:config).returns('port' => 9999) + assert_equal 9999, self.port + end + +end -- libgit2 0.21.2