Skip to content

Commit

Permalink
Make sure paths are normalized so it doesn't matter whether paths pas…
Browse files Browse the repository at this point in the history
…sed in as arguments have a leading slash or not
  • Loading branch information
alexrabarts committed Mar 12, 2009
1 parent d601a39 commit 7592970
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
8 changes: 6 additions & 2 deletions lib/big_sitemap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def initialize(options)
@base_url = options.delete(:base_url)
@max_per_sitemap = options.delete(:max_per_sitemap) || 50000
@batch_size = options.delete(:batch_size) || 1001 # TODO: Set this to 1000 once DM offset 37000 bug is fixed
@web_path = options.delete(:path) || 'sitemaps'
@web_path = strip_leading_slash(options.delete(:path) || 'sitemaps')
@ping_google = options[:ping_google].nil? ? true : options.delete(:ping_google)
@ping_yahoo = options[:ping_yahoo].nil? ? true : options.delete(:ping_yahoo)
@yahoo_app_id = options.delete(:yahoo_app_id)
Expand All @@ -42,7 +42,7 @@ def initialize(options)

def add(options)
raise ArgumentError, ':model and :path options must be provided' unless options[:model] && options[:path]
@sources << options
@sources << options.update(:path => strip_leading_slash(options[:path]))
self # Chainable
end

Expand Down Expand Up @@ -127,6 +127,10 @@ def generate
end

private
def strip_leading_slash(str)
str.sub(/^\//, '')
end

def pick_method(klass, candidates)
method = nil
candidates.each do |candidate|
Expand Down
19 changes: 16 additions & 3 deletions test/big_sitemap_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,21 @@ def teardown
assert_equal 1, num_elements(first_sitemaps_model_file, 'changefreq')
assert_equal 1, num_elements(second_sitemaps_model_file, 'changefreq')
end

should 'strip leading slashes from controller paths' do
create_sitemap
@sitemap.add(:model => TestModel, :path => '/test_controller').generate
assert(
!elements(single_sitemaps_model_file, 'loc').first.text.match(/\/\/test_controller\//),
'URL does not contain a double-slash before the controller path'
)
end
end

context 'add method' do
should 'be chainable' do
create_sitemap
assert_equal BigSitemap, @sitemap.add({:model => TestModel, :path => 'test_controller'}).class
assert_equal BigSitemap, @sitemap.add(:model => TestModel, :path => 'test_controller').class
end
end

Expand Down Expand Up @@ -198,8 +207,12 @@ def ns
{'s' => 'http://www.sitemaps.org/schemas/sitemap/0.9'}
end

def num_elements(filename, el)
def elements(filename, el)
data = Nokogiri::XML.parse(Zlib::GzipReader.open(filename).read)
data.search("//s:#{el}", ns).size
data.search("//s:#{el}", ns)
end

def num_elements(filename, el)
elements(filename, el).size
end
end
4 changes: 4 additions & 0 deletions test/fixtures/test_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def count_for_sitemap
self.find_for_sitemap.size
end

def num_items
10
end

def find_for_sitemap(options={})
instances = []
num_times = options.delete(:limit) || self.num_items
Expand Down

0 comments on commit 7592970

Please sign in to comment.