sitemap_generator 5.3.1 → 6.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 5.3.1
1
+ 6.1.1
@@ -1,7 +1,7 @@
1
1
  namespace :sitemap do
2
2
  desc 'Create sitemap and ping search engines'
3
3
  task :refresh do
4
- on roles :web do
4
+ on roles fetch(:sitemap_roles, :web) do
5
5
  within release_path do
6
6
  with rails_env: (fetch(:rails_env) || fetch(:stage)) do
7
7
  execute :rake, "sitemap:refresh"
@@ -12,7 +12,7 @@ namespace :sitemap do
12
12
 
13
13
  desc 'Create sitemap without pinging search engines'
14
14
  task :create do
15
- on roles :web do
15
+ on roles fetch(:sitemap_roles, :web) do
16
16
  within release_path do
17
17
  with rails_env: (fetch(:rails_env) || fetch(:stage)) do
18
18
  execute :rake, "sitemap:create"
@@ -23,7 +23,7 @@ namespace :sitemap do
23
23
 
24
24
  desc 'Clean up sitemaps in sitemap_generator path'
25
25
  task :clean do
26
- on roles :web do
26
+ on roles fetch(:sitemap_roles, :web) do
27
27
  within release_path do
28
28
  with rails_env: (fetch(:rails_env) || fetch(:stage)) do
29
29
  execute :rake, "sitemap:clean"
@@ -7,14 +7,15 @@ require 'sitemap_generator/application'
7
7
  require 'sitemap_generator/sitemap_location'
8
8
 
9
9
  module SitemapGenerator
10
- autoload(:Interpreter, 'sitemap_generator/interpreter')
11
- autoload(:FileAdapter, 'sitemap_generator/adapters/file_adapter')
12
- autoload(:S3Adapter, 'sitemap_generator/adapters/s3_adapter')
13
- autoload(:AwsSdkAdapter, 'sitemap_generator/adapters/aws_sdk_adapter')
14
- autoload(:WaveAdapter, 'sitemap_generator/adapters/wave_adapter')
15
- autoload(:FogAdapter, 'sitemap_generator/adapters/fog_adapter')
16
- autoload(:BigDecimal, 'sitemap_generator/core_ext/big_decimal')
17
- autoload(:Numeric, 'sitemap_generator/core_ext/numeric')
10
+ autoload(:Interpreter, 'sitemap_generator/interpreter')
11
+ autoload(:FileAdapter, 'sitemap_generator/adapters/file_adapter')
12
+ autoload(:S3Adapter, 'sitemap_generator/adapters/s3_adapter')
13
+ autoload(:AwsSdkAdapter, 'sitemap_generator/adapters/aws_sdk_adapter')
14
+ autoload(:WaveAdapter, 'sitemap_generator/adapters/wave_adapter')
15
+ autoload(:FogAdapter, 'sitemap_generator/adapters/fog_adapter')
16
+ autoload(:GoogleStorageAdapter, 'sitemap_generator/adapters/google_storage_adapter')
17
+ autoload(:BigDecimal, 'sitemap_generator/core_ext/big_decimal')
18
+ autoload(:Numeric, 'sitemap_generator/core_ext/numeric')
18
19
 
19
20
  SitemapError = Class.new(StandardError)
20
21
  SitemapFullError = Class.new(SitemapError)
@@ -26,7 +27,7 @@ module SitemapGenerator
26
27
  MAX_SITEMAP_LINKS = 50_000 # max links per sitemap
27
28
  MAX_SITEMAP_IMAGES = 1_000 # max images per url
28
29
  MAX_SITEMAP_NEWS = 1_000 # max news sitemap per index_file
29
- MAX_SITEMAP_FILESIZE = 10_000_000 # bytes
30
+ MAX_SITEMAP_FILESIZE = 50_000_000 # bytes
30
31
  SCHEMAS = {
31
32
  'image' => 'http://www.google.com/schemas/sitemap-image/1.1',
32
33
  'mobile' => 'http://www.google.com/schemas/sitemap-mobile/1.0',
@@ -56,6 +57,7 @@ module SitemapGenerator
56
57
  attr_accessor :root, :app, :templates
57
58
  attr_writer :yield_sitemap, :verbose
58
59
  end
60
+ @yield_sitemap = nil
59
61
 
60
62
  # Global default for the verbose setting.
61
63
  def self.verbose
@@ -82,4 +84,4 @@ module SitemapGenerator
82
84
  self.app = SitemapGenerator::Application.new
83
85
  end
84
86
 
85
- require 'sitemap_generator/railtie' if SitemapGenerator.app.rails3?
87
+ require 'sitemap_generator/railtie' if SitemapGenerator.app.is_at_least_rails3?
@@ -1,45 +1,60 @@
1
- begin
2
- require 'aws-sdk'
3
- rescue LoadError
4
- raise LoadError.new("Missing required 'aws-sdk'. Please 'gem install "\
5
- "aws-sdk' and require it in your application, or "\
6
- "add: gem 'aws-sdk' to your Gemfile.")
1
+ if !defined?(Aws::S3::Resource) or !defined?(Aws::Credentials)
2
+ raise "Error: `Aws::S3::Resource` and/or `Aws::Credentials` are not defined.\n\n"\
3
+ "Please `require 'aws-sdk'` - or another library that defines these classes."
7
4
  end
8
5
 
9
6
  module SitemapGenerator
10
- # Class for uploading the sitemaps to an S3 bucket using the plain AWS SDK gem
7
+ # Class for uploading sitemaps to an S3 bucket using the AWS SDK gem.
11
8
  class AwsSdkAdapter
12
- # @param [String] bucket name of the S3 bucket
13
- # @param [Hash] opts alternate means of configuration other than ENV
14
- # @option opts [String] :aws_access_key_id instead of ENV['AWS_ACCESS_KEY_ID']
15
- # @option opts [String] :aws_region instead of ENV['AWS_REGION']
16
- # @option opts [String] :aws_secret_access_key instead of ENV['AWS_SECRET_ACCESS_KEY']
17
- # @option opts [String] :path use this prefix on the object key instead of 'sitemaps/'
18
- def initialize(bucket, opts = {})
9
+ # Specify your AWS bucket name, credentials, and/or region. By default
10
+ # the AWS SDK will auto-detect your credentials and region, but you can use
11
+ # the following options to configure - or override - them manually:
12
+ #
13
+ # Options:
14
+ # :aws_access_key_id [String] Your AWS access key id
15
+ # :aws_secret_access_key [String] Your AWS secret access key
16
+ # :aws_region [String] Your AWS region
17
+ #
18
+ # Requires Aws::S3::Resource and Aws::Credentials to be defined.
19
+ #
20
+ # @param [String] bucket Name of the S3 bucket
21
+ # @param [Hash] options AWS credential overrides, see above
22
+ def initialize(bucket, options = {})
19
23
  @bucket = bucket
20
-
21
- @aws_access_key_id = opts[:aws_access_key_id] || ENV['AWS_ACCESS_KEY_ID']
22
- @aws_region = opts[:aws_region] || ENV['AWS_REGION']
23
- @aws_secret_access_key = opts[:aws_secret_access_key] || ENV['AWS_SECRET_ACCESS_KEY']
24
-
25
- @path = opts[:path] || 'sitemaps/'
24
+ @aws_access_key_id = options[:aws_access_key_id]
25
+ @aws_secret_access_key = options[:aws_secret_access_key]
26
+ @aws_region = options[:aws_region]
27
+ @aws_endpoint = options[:aws_endpoint]
26
28
  end
27
29
 
28
30
  # Call with a SitemapLocation and string data
29
31
  def write(location, raw_data)
30
32
  SitemapGenerator::FileAdapter.new.write(location, raw_data)
33
+ s3_object = s3_resource.bucket(@bucket).object(location.path_in_public)
34
+ s3_object.upload_file(location.path,
35
+ acl: 'public-read',
36
+ cache_control: 'private, max-age=0, no-cache',
37
+ content_type: location[:compress] ? 'application/x-gzip' : 'application/xml'
38
+ )
39
+ end
31
40
 
32
- credentials = Aws::Credentials.new(@aws_access_key_id, @aws_secret_access_key)
33
- s3 = Aws::S3::Resource.new(credentials: credentials, region: @aws_region)
41
+ private
34
42
 
35
- s3_object_key = "#{@path}#{location.path_in_public}"
36
- s3_object = s3.bucket(@bucket).object(s3_object_key)
43
+ def s3_resource
44
+ @s3_resource ||= Aws::S3::Resource.new(s3_resource_options)
45
+ end
37
46
 
38
- content_type = location[:compress] ? 'application/x-gzip' : 'application/xml'
39
- s3_object.upload_file(location.path,
40
- acl: 'public-read',
41
- cache_control: 'private, max-age=0, no-cache',
42
- content_type: content_type)
47
+ def s3_resource_options
48
+ options = {}
49
+ options[:region] = @aws_region if !@aws_region.nil?
50
+ options[:endpoint] = @aws_endpoint if !@aws_endpoint.nil?
51
+ if !@aws_access_key_id.nil? && !@aws_secret_access_key.nil?
52
+ options[:credentials] = Aws::Credentials.new(
53
+ @aws_access_key_id,
54
+ @aws_secret_access_key
55
+ )
56
+ end
57
+ options
43
58
  end
44
59
  end
45
60
  end
@@ -1,7 +1,6 @@
1
1
  module SitemapGenerator
2
2
  # Class for writing out data to a file.
3
3
  class FileAdapter
4
-
5
4
  # Write data to a file.
6
5
  # @param location - File object giving the full path and file name of the file.
7
6
  # If the location specifies a directory(ies) which does not exist, the directory(ies)
@@ -12,7 +11,7 @@ module SitemapGenerator
12
11
  def write(location, raw_data)
13
12
  # Ensure that the directory exists
14
13
  dir = location.directory
15
- if !File.exists?(dir)
14
+ if !File.exist?(dir)
16
15
  FileUtils.mkdir_p(dir)
17
16
  elsif !File.directory?(dir)
18
17
  raise SitemapError.new("#{dir} should be a directory!")
@@ -1,12 +1,16 @@
1
- begin
2
- require 'fog'
3
- rescue LoadError
4
- raise LoadError.new("Missing required 'fog'. Please 'gem install fog' and require it in your application.")
1
+ if !defined?(Fog::Storage)
2
+ raise "Error: `Fog::Storage` is not defined.\n\n"\
3
+ "Please `require 'fog'` - or another library that defines this class."
5
4
  end
6
5
 
7
6
  module SitemapGenerator
7
+ # Class for uploading sitemaps to a Fog supported endpoint.
8
8
  class FogAdapter
9
-
9
+ # Requires Fog::Storage to be defined.
10
+ #
11
+ # @param [Hash] opts Fog configuration options
12
+ # @option :fog_credentials [Hash] Credentials for connecting to the remote server
13
+ # @option :fog_directory [String] Your AWS S3 bucket or similar directory name
10
14
  def initialize(opts = {})
11
15
  @fog_credentials = opts[:fog_credentials]
12
16
  @fog_directory = opts[:fog_directory]
@@ -0,0 +1,37 @@
1
+ if !defined?(Google::Cloud::Storage)
2
+ raise "Error: `Google::Cloud::Storage` is not defined.\n\n"\
3
+ "Please `require 'google/cloud/storage'` - or another library that defines this class."
4
+ end
5
+
6
+ module SitemapGenerator
7
+ # Class for uploading sitemaps to a Google Storage using `google-cloud-storage` gem.
8
+ class GoogleStorageAdapter
9
+ # Requires Google::Cloud::Storage to be defined.
10
+ #
11
+ # @param [Hash] opts Google::Cloud::Storage configuration options.
12
+ # @option :bucket [String] Required. Name of Google Storage Bucket where the file is to be uploaded.
13
+ #
14
+ # All options other than the `:bucket` option are passed to the `Google::Cloud::Storage.new`
15
+ # initializer. See https://googleapis.dev/ruby/google-cloud-storage/latest/file.AUTHENTICATION.html
16
+ # for all the supported environment variables and https://github.com/googleapis/google-cloud-ruby/blob/master/google-cloud-storage/lib/google/cloud/storage.rb
17
+ # for supported options.
18
+ #
19
+ # Suggested Options:
20
+ # @option :credentials [String] Path to Google service account JSON file, or JSON contents.
21
+ # @option :project_id [String] Google Accounts project id where the storage bucket resides.
22
+ def initialize(opts = {})
23
+ opts = opts.clone
24
+ @bucket = opts.delete(:bucket)
25
+ @storage_options = opts
26
+ end
27
+
28
+ # Call with a SitemapLocation and string data
29
+ def write(location, raw_data)
30
+ SitemapGenerator::FileAdapter.new.write(location, raw_data)
31
+
32
+ storage = Google::Cloud::Storage.new(@storage_options)
33
+ bucket = storage.bucket(@bucket)
34
+ bucket.create_file(location.path, location.path_in_public, acl: 'public')
35
+ end
36
+ end
37
+ end
@@ -1,17 +1,21 @@
1
- # Without this require, fog-core 1.2.0 raises
2
- # NameError: uninitialized constant Fog::ServicesMixin.
3
- # I don't know which versions this affects.
4
- require 'fog/core/services_mixin' rescue nil
5
-
6
- begin
7
- require 'fog/storage'
8
- rescue LoadError
9
- raise LoadError.new("Missing required 'fog-aws'. Please 'gem install fog-aws' and require it in your application.")
1
+ if !defined?(Fog::Storage)
2
+ raise "Error: `Fog::Storage` is not defined.\n\n"\
3
+ "Please `require 'fog-aws'` - or another library that defines this class."
10
4
  end
11
5
 
12
6
  module SitemapGenerator
7
+ # Class for uploading sitemaps to an S3 bucket using the Fog gem.
13
8
  class S3Adapter
14
-
9
+ # Requires Fog::Storage to be defined.
10
+ #
11
+ # @param [Hash] opts Fog configuration options
12
+ # @option :aws_access_key_id [String] Your AWS access key id
13
+ # @option :aws_secret_access_key [String] Your AWS secret access key
14
+ # @option :fog_provider [String]
15
+ # @option :fog_directory [String]
16
+ # @option :fog_region [String]
17
+ # @option :fog_path_style [String]
18
+ # @option :fog_storage_options [Hash] Other options to pass to `Fog::Storage`
15
19
  def initialize(opts = {})
16
20
  @aws_access_key_id = opts[:aws_access_key_id] || ENV['AWS_ACCESS_KEY_ID']
17
21
  @aws_secret_access_key = opts[:aws_secret_access_key] || ENV['AWS_SECRET_ACCESS_KEY']
@@ -46,6 +50,5 @@ module SitemapGenerator
46
50
  :public => true
47
51
  )
48
52
  end
49
-
50
53
  end
51
54
  end
@@ -1,10 +1,10 @@
1
- begin
2
- require 'carrierwave'
3
- rescue LoadError
4
- raise LoadError.new("Missing required 'carrierwave'. Please 'gem install carrierwave' and require it in your application.")
1
+ if !defined?(::CarrierWave::Uploader::Base)
2
+ raise "Error: `CarrierWave::Uploader::Base` is not defined.\n\n"\
3
+ "Please `require 'carrierwave'` - or another library that defines this class."
5
4
  end
6
5
 
7
6
  module SitemapGenerator
7
+ # Class for uploading sitemaps to a remote server using the CarrierWave gem.
8
8
  class WaveAdapter < ::CarrierWave::Uploader::Base
9
9
  attr_accessor :store_dir
10
10
 
@@ -2,15 +2,15 @@ require 'pathname'
2
2
 
3
3
  module SitemapGenerator
4
4
  class Application
5
- def rails?
5
+ def is_rails?
6
6
  !!defined?(Rails::VERSION)
7
7
  end
8
8
 
9
9
  # Returns a boolean indicating whether this environment is Rails 3
10
10
  #
11
11
  # @return [Boolean]
12
- def rails3?
13
- rails? && Rails.version.to_f >= 3
12
+ def is_at_least_rails3?
13
+ is_rails? && Rails.version.to_f >= 3
14
14
  rescue
15
15
  false # Rails.version defined in 2.1.0
16
16
  end
@@ -27,12 +27,9 @@ module SitemapGenerator
27
27
  #
28
28
  # @return [String, nil]
29
29
  def rails_root
30
- if defined?(::Rails.root)
31
- return ::Rails.root.to_s if ::Rails.root
32
- raise "ERROR: Rails.root is nil!"
33
- end
30
+ return ::Rails.root.to_s if defined?(::Rails.root) && ::Rails.root
34
31
  return RAILS_ROOT.to_s if defined?(RAILS_ROOT)
35
- return nil
32
+ nil
36
33
  end
37
34
 
38
35
  # Returns the environment of the Rails application,
@@ -43,7 +40,7 @@ module SitemapGenerator
43
40
  def rails_env
44
41
  return ::Rails.env.to_s if defined?(::Rails.env)
45
42
  return RAILS_ENV.to_s if defined?(RAILS_ENV)
46
- return nil
43
+ nil
47
44
  end
48
45
  end
49
46
  end
@@ -30,6 +30,7 @@ module SitemapGenerator
30
30
  # Store the URL of the first sitemap added because if create_index is
31
31
  # false this is the "index" URL
32
32
  @first_sitemap_url = nil
33
+ @create_index = nil
33
34
  end
34
35
 
35
36
  # Finalize sitemaps as they are added to the index.
@@ -8,18 +8,30 @@ end
8
8
  require 'yaml'
9
9
 
10
10
  # Define our own class rather than modify the global class
11
- class SitemapGenerator::BigDecimal < BigDecimal
11
+ class SitemapGenerator::BigDecimal
12
12
  YAML_TAG = 'tag:yaml.org,2002:float'
13
13
  YAML_MAPPING = { 'Infinity' => '.Inf', '-Infinity' => '-.Inf', 'NaN' => '.NaN' }
14
14
 
15
- yaml_as YAML_TAG
15
+ yaml_tag YAML_TAG
16
+
17
+ def initialize(num)
18
+ @value = BigDecimal(num)
19
+ end
20
+
21
+ def *(other)
22
+ other * @value
23
+ end
24
+
25
+ def /(other)
26
+ SitemapGenerator::BigDecimal === other ? @value / other.instance_variable_get(:@value) : @value / other
27
+ end
16
28
 
17
29
  # This emits the number without any scientific notation.
18
30
  # This is better than self.to_f.to_s since it doesn't lose precision.
19
31
  #
20
32
  # Note that reconstituting YAML floats to native floats may lose precision.
21
33
  def to_yaml(opts = {})
22
- return super if defined?(YAML::ENGINE) && !YAML::ENGINE.syck?
34
+ return super unless defined?(YAML::ENGINE) && YAML::ENGINE.syck?
23
35
 
24
36
  YAML.quick_emit(nil, opts) do |out|
25
37
  string = to_s
@@ -37,9 +49,7 @@ class SitemapGenerator::BigDecimal < BigDecimal
37
49
  end
38
50
 
39
51
  DEFAULT_STRING_FORMAT = 'F'
40
- def to_formatted_s(format = DEFAULT_STRING_FORMAT)
41
- _original_to_s(format)
52
+ def to_s(format = DEFAULT_STRING_FORMAT)
53
+ @value.to_s(format)
42
54
  end
43
- alias_method :_original_to_s, :to_s
44
- alias_method :to_s, :to_formatted_s
45
55
  end
@@ -6,9 +6,11 @@ module SitemapGenerator
6
6
  # and API methods available to it.
7
7
  class Interpreter
8
8
 
9
- if SitemapGenerator.app.rails3?
10
- include ::Rails.application.routes.url_helpers
11
- elsif SitemapGenerator.app.rails?
9
+ if SitemapGenerator.app.is_at_least_rails3?
10
+ if !::Rails.application.nil?
11
+ include ::Rails.application.routes.url_helpers
12
+ end
13
+ elsif SitemapGenerator.app.is_rails?
12
14
  require 'action_controller'
13
15
  include ActionController::UrlWriter
14
16
  end
@@ -34,7 +36,7 @@ module SitemapGenerator
34
36
  def add_to_index(*args)
35
37
  @linkset.add_to_index(*args)
36
38
  end
37
-
39
+
38
40
  # Start a new group of sitemaps. Any of the options to SitemapGenerator.new may
39
41
  # be passed. Pass a block with calls to +add+ to add links to the sitemaps.
40
42
  #
@@ -118,6 +118,8 @@ module SitemapGenerator
118
118
  # Note: When adding a new option be sure to include it in `options_for_group()` if
119
119
  # the option should be inherited by groups.
120
120
  def initialize(options={})
121
+ @default_host, @sitemaps_host, @yield_sitemap, @sitemaps_path, @adapter, @verbose, @protect_index, @sitemap_index, @added_default_links, @created_group, @sitemap = nil
122
+
121
123
  options = SitemapGenerator::Utilities.reverse_merge(options,
122
124
  :include_root => true,
123
125
  :include_index => false,
@@ -217,6 +219,7 @@ module SitemapGenerator
217
219
  @sitemap.location.merge!(@group.sitemap_location)
218
220
  if block_given?
219
221
  @group.interpreter.eval(:yield_sitemap => @yield_sitemap || SitemapGenerator.yield_sitemap?, &block)
222
+ @group.finalize_sitemap!
220
223
  @sitemap.location.merge!(@original_location)
221
224
  end
222
225
  else
@@ -292,7 +295,7 @@ module SitemapGenerator
292
295
  name = Utilities.titleize(engine.to_s)
293
296
  begin
294
297
  Timeout::timeout(10) {
295
- open(link)
298
+ URI.open(link)
296
299
  }
297
300
  output(" Successful ping of #{name}")
298
301
  rescue Timeout::Error, StandardError => e
@@ -430,13 +433,14 @@ module SitemapGenerator
430
433
  # Add default links if those options are turned on. Record the fact that we have done so
431
434
  # in an instance variable.
432
435
  def add_default_links
436
+ @added_default_links = true
437
+ link_options = { :lastmod => Time.now, :changefreq => 'always', :priority => 1.0 }
433
438
  if include_root?
434
- sitemap.add('/', :lastmod => Time.now, :changefreq => 'always', :priority => 1.0, :host => @default_host)
439
+ add('/', link_options)
435
440
  end
436
441
  if include_index?
437
- sitemap.add(sitemap_index, :lastmod => Time.now, :changefreq => 'always', :priority => 1.0)
442
+ add(sitemap_index, link_options)
438
443
  end
439
- @added_default_links = true
440
444
  end
441
445
 
442
446
  # Finalize a sitemap by including it in the index and outputting a summary line.