Permalink
Browse files

The generator should be able to generate either http or https url.

We can pass the protocol parameter to it and the default is "http"
  • Loading branch information...
1 parent 6592de4 commit b54dab689941f17cdff41115ff747f268996fd84 @sishen sishen committed Sep 18, 2012
Showing with 53 additions and 6 deletions.
  1. +8 −5 lib/sitemap/generator.rb
  2. +45 −1 spec/generator_spec.rb
View
@@ -10,12 +10,13 @@ class Generator
:priority => "priority"
}
- attr_accessor :store, :host, :routes, :fragments
+ attr_accessor :store, :protocol, :host, :routes, :fragments
# Instantiates a new object.
# Should never be called directly.
def initialize
self.class.send(:include, Rails.application.routes.url_helpers)
+ self.protocol = "http"
self.fragments = []
self.store = Store.new(:max_entries => Sitemap.configuration.max_urls)
self.store.before_reset do |entries|
@@ -35,7 +36,7 @@ def initialize
#
# Sitemap::Generator.instance.load :host => "mywebsite.com" do
# literal "/some_fancy_url"
- # end
+ # end
#
# Simple paths can be added as follows:
#
@@ -63,16 +64,17 @@ def load(options = {}, &block)
end
self.routes = block
end
-
+
# Adds the literal url (for consistency, starting with a "/" as in "/my_url")
# accepts similar options to path and resources
def literal(target_url, options = {})
search = Sitemap.configuration.search.clone.merge!(options.select { |k, v| SEARCH_ATTRIBUTES.keys.include?(k) })
search.merge!(search) { |type, value| get_data(nil, value) }
-
+
output_host = options[:host] || host
+ output_protocol = options[:protocol] || protocol
self.store << {
- :url =>"http://#{output_host}#{target_url}",
+ :url =>"#{output_protocol}://#{output_host}#{target_url}",
:search => search
}
end
@@ -92,6 +94,7 @@ def literal(target_url, options = {})
#
def path(object, options = {})
params = Sitemap.configuration.params.clone.merge!(options[:params] || {})
+ params[:protocol] ||= protocol # Use global protocol if none was specified.
params[:host] ||= host # Use global host if none was specified.
params.merge!(params) { |type, value| get_data(object, value) }
View
@@ -19,7 +19,7 @@
doc.errors.length.must_equal 0
doc.root.name.must_equal "urlset"
end
-
+
it "should create entries based on literals" do
urls = ["http://someplace.com/target_url", "http://someplace.com/another_url"]
Sitemap::Generator.instance.load(:host => "someplace.com") do
@@ -35,6 +35,21 @@
end
end
+ it "should create entries based on literals with https" do
+ urls = ["https://someplace.com/target_url", "https://someplace.com/another_url"]
+ Sitemap::Generator.instance.load(:host => "someplace.com", :protocol => "https") do
+ literal "/target_url"
+ literal "/another_url"
+ end
+ Sitemap::Generator.instance.build!
+ doc = Nokogiri::HTML(Sitemap::Generator.instance.render)
+ elements = doc.xpath "//url/loc"
+ elements.length.must_equal urls.length
+ elements.each_with_index do |element, i|
+ element.text.must_equal urls[i]
+ end
+ end
+
it "should create entries based on the route paths" do
urls = ["http://someplace.com/", "http://someplace.com/questions"]
Sitemap::Generator.instance.load(:host => "someplace.com") do
@@ -50,6 +65,21 @@
end
end
+ it "should create entries based on the route paths with https" do
+ urls = ["https://someplace.com/", "https://someplace.com/questions"]
+ Sitemap::Generator.instance.load(:host => "someplace.com", :protocol => "https") do
+ path :root
+ path :faq
+ end
+ Sitemap::Generator.instance.build!
+ doc = Nokogiri::HTML(Sitemap::Generator.instance.render)
+ elements = doc.xpath "//url/loc"
+ elements.length.must_equal urls.length
+ elements.each_with_index do |element, i|
+ element.text.must_equal urls[i]
+ end
+ end
+
it "should create entries based on the route resources" do
Sitemap::Generator.instance.load(:host => "someplace.com") do
resources :activities
@@ -64,6 +94,20 @@
end
end
+ it "should create entries based on the route resources with https" do
+ Sitemap::Generator.instance.load(:host => "someplace.com", :protocol => "https") do
+ resources :activities
+ end
+ Sitemap::Generator.instance.build!
+ doc = Nokogiri::HTML(Sitemap::Generator.instance.render)
+ elements = doc.xpath "//url/loc"
+ elements.length.must_equal (Activity.count + 1)
+ elements.first.text.must_equal "https://someplace.com/activities"
+ elements[1..-1].each_with_index do |element, i|
+ element.text.must_equal "https://someplace.com/activities/#{i + 1}"
+ end
+ end
+
it "should create entries using only for the specified objects" do
activities = proc { Activity.where(:published => true) }
Sitemap::Generator.instance.load(:host => "someplace.com") do

0 comments on commit b54dab6

Please sign in to comment.