Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/dynamic_sitemaps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ module DynamicSitemaps
DEFAULT_INDEX_FILE_NAME = "sitemap.xml"
DEFAULT_ALWAYS_GENERATE_INDEX = false
DEFAULT_PROTOCOL = "http"
DEFAULT_PROXY_HOST = nil
DEFAULT_PROXY_PORT = nil
SEARCH_ENGINE_PING_URLS = [
"http://www.google.com/webmasters/sitemaps/ping?sitemap=%s",
"http://www.bing.com/webmaster/ping.aspx?siteMap=%s"
]
DEFAULT_PING_ENVIRONMENTS = ["production"]

class << self
attr_writer :index_file_name, :always_generate_index, :per_page, :search_engine_ping_urls, :ping_environments
attr_writer :index_file_name, :always_generate_index, :per_page, :search_engine_ping_urls, :ping_environments, :proxy_host, :proxy_port

# Generates the sitemap(s) and index based on the configuration file specified in DynamicSitemaps.config_path.
# If you supply a block, that block is evaluated instead of the configuration file.
Expand Down Expand Up @@ -111,11 +113,19 @@ def protocol
@protocol ||= DEFAULT_PROTOCOL
end

def proxy_host
@proxy_host ||= DEFAULT_PROXY_HOST
end

def proxy_port
@proxy_port ||= DEFAULT_PROXY_PORT
end

# Resets all instance variables. Used for testing.
def reset!
instance_variables.each { |var| remove_instance_variable var }
# reset the protocol to http
Rails.application.routes.default_url_options[:protocol] = "http"
end
end
end
end
12 changes: 10 additions & 2 deletions lib/dynamic_sitemaps/pinger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,16 @@ def ping_search_engines_with_sitemap_url(sitemap_url)

def ping(url)
Logger.info "Pinging #{url} ..."
uri = URI.parse(url)
begin
Net::HTTP.get(URI.parse(url))
Net::HTTP.new(
uri.host,
uri.port,
DynamicSitemaps.proxy_host,
DynamicSitemaps.proxy_port
).start do |http|
http.get(uri.request_uri)
end
rescue Exception => e
Logger.warn "Failed to ping #{url} : #{e}"
end
Expand All @@ -37,4 +45,4 @@ def ping_for_environment?(env)
end
end
end
end
end
6 changes: 4 additions & 2 deletions lib/dynamic_sitemaps/sitemap.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module DynamicSitemaps
class Sitemap
attr_reader :name, :collection, :block, :host, :folder, :protocol
attr_reader :name, :collection, :block, :host, :folder, :protocol, :proxy_host, :proxy_port

# Initializes a sitemap object.
#
Expand All @@ -19,6 +19,8 @@ def initialize(*args, &block)
@host = options[:host]
@folder = options[:folder]
@collection = options[:collection]
@proxy_host = options[:proxy_host]
@proxy_port = options[:proxy_port]
end

@block = block
Expand All @@ -40,4 +42,4 @@ def protocol
def generate
end
end
end
end
6 changes: 6 additions & 0 deletions test/dynamic_sitemaps_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@ class DynamicSitemapsTest < ActiveSupport::TestCase
assert_equal 50000, DynamicSitemaps.per_page
assert_equal ["production"], DynamicSitemaps.ping_environments
assert_equal Rails.root.join("tmp", "dynamic_sitemaps").to_s, DynamicSitemaps.temp_path
assert_nil DynamicSitemaps.proxy_host
assert_nil DynamicSitemaps.proxy_port
end

test "configuration block" do
DynamicSitemaps.configure do |config|
config.folder = "mycustomfolder"
config.per_page = 1234
config.proxy_host = "proxy"
config.proxy_port = 3128
end

assert_equal "mycustomfolder", DynamicSitemaps.folder
assert_equal 1234, DynamicSitemaps.per_page
assert_equal "proxy", DynamicSitemaps.proxy_host
assert_equal 3128, DynamicSitemaps.proxy_port
end

test "raises error on blank paths" do
Expand Down
4 changes: 2 additions & 2 deletions test/pinger_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ class PingerTest < ActiveSupport::TestCase
assert !DynamicSitemaps::Pinger.ping_for_environment?("development")
assert !DynamicSitemaps::Pinger.ping_for_environment?("test")
assert !DynamicSitemaps::Pinger.ping_for_environment?("staging")

DynamicSitemaps.ping_environments << "staging"
assert DynamicSitemaps::Pinger.ping_for_environment?("staging")
assert DynamicSitemaps::Pinger.ping_for_environment?("production")
end
end
end