Class: BetterSeo::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/better_seo/configuration.rb

Defined Under Namespace

Classes: NestedConfiguration

Constant Summary collapse

DEFAULT_CONFIG =

Default values

{
  site_name: nil,
  default_locale: :en,
  available_locales: [:en],

  # Meta tags defaults
  meta_tags: {
    default_title: nil,
    title_separator: " | ",
    append_site_name: true,
    default_description: nil,
    default_keywords: [],
    default_author: nil
  },

  # Open Graph defaults
  open_graph: {
    enabled: true,
    site_name: nil,
    default_type: "website",
    default_locale: "en_US",
    default_image: {
      url: nil,
      width: 1200,
      height: 630
    }
  },

  # Twitter Cards defaults
  twitter: {
    enabled: true,
    site: nil,
    creator: nil,
    card_type: "summary_large_image"
  },

  # Structured Data defaults
  structured_data: {
    enabled: true,
    organization: {},
    website: {}
  },

  # Sitemap defaults
  sitemap: {
    enabled: false,
    output_path: "public/sitemap.xml",
    host: nil,
    compress: false,
    ping_search_engines: false,
    defaults: {
      changefreq: "weekly",
      priority: 0.5
    }
  },

  # Robots.txt defaults
  robots: {
    enabled: false,
    output_path: "public/robots.txt",
    user_agents: {
      "*" => {
        allow: ["/"],
        disallow: [],
        crawl_delay: nil
      }
    }
  },

  # Image optimization defaults
  images: {
    enabled: false,
    webp: {
      enabled: true,
      quality: 80
    },
    sizes: {
      thumbnail: { width: 150, height: 150 },
      small: { width: 300 },
      medium: { width: 600 },
      large: { width: 1200 },
      og_image: { width: 1200, height: 630, crop: true }
    }
  },

  # I18n settings
  i18n: {
    load_path: "config/locales/seo/**/*.yml",
    auto_reload: false
  }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/better_seo/configuration.rb', line 105

def initialize
  # Deep dup per evitare shared state
  @config = deep_dup(DEFAULT_CONFIG)

  # Initialize nested configurations
  @meta_tags = NestedConfiguration.new(@config[:meta_tags])
  @open_graph = NestedConfiguration.new(@config[:open_graph])
  @twitter = NestedConfiguration.new(@config[:twitter])
  @structured_data = NestedConfiguration.new(@config[:structured_data])
  @sitemap = NestedConfiguration.new(@config[:sitemap])
  @robots = NestedConfiguration.new(@config[:robots])
  @images = NestedConfiguration.new(@config[:images])
  @i18n = NestedConfiguration.new(@config[:i18n])

  # Top-level attributes
  @site_name = @config[:site_name]
  @default_locale = @config[:default_locale]
  @available_locales = @config[:available_locales]
end

Instance Attribute Details

#available_localesObject

Returns the value of attribute available_locales.



101
102
103
# File 'lib/better_seo/configuration.rb', line 101

def available_locales
  @available_locales
end

#default_localeObject

Returns the value of attribute default_locale.



101
102
103
# File 'lib/better_seo/configuration.rb', line 101

def default_locale
  @default_locale
end

#i18nObject (readonly)

Returns the value of attribute i18n.



102
103
104
# File 'lib/better_seo/configuration.rb', line 102

def i18n
  @i18n
end

#imagesObject (readonly)

Returns the value of attribute images.



102
103
104
# File 'lib/better_seo/configuration.rb', line 102

def images
  @images
end

#meta_tagsObject (readonly)

Returns the value of attribute meta_tags.



102
103
104
# File 'lib/better_seo/configuration.rb', line 102

def meta_tags
  @meta_tags
end

#open_graphObject (readonly)

Returns the value of attribute open_graph.



102
103
104
# File 'lib/better_seo/configuration.rb', line 102

def open_graph
  @open_graph
end

#robotsObject (readonly)

Returns the value of attribute robots.



102
103
104
# File 'lib/better_seo/configuration.rb', line 102

def robots
  @robots
end

#site_nameObject

Returns the value of attribute site_name.



101
102
103
# File 'lib/better_seo/configuration.rb', line 101

def site_name
  @site_name
end

#sitemapObject (readonly)

Returns the value of attribute sitemap.



102
103
104
# File 'lib/better_seo/configuration.rb', line 102

def sitemap
  @sitemap
end

#structured_dataObject (readonly)

Returns the value of attribute structured_data.



102
103
104
# File 'lib/better_seo/configuration.rb', line 102

def structured_data
  @structured_data
end

#twitterObject (readonly)

Returns the value of attribute twitter.



102
103
104
# File 'lib/better_seo/configuration.rb', line 102

def twitter
  @twitter
end

Instance Method Details

#images_enabled?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/better_seo/configuration.rb', line 170

def images_enabled?
  images.enabled == true
end

#load_from_hash(hash) ⇒ Object

Load configuration from hash



126
127
128
129
# File 'lib/better_seo/configuration.rb', line 126

def load_from_hash(hash)
  merge_hash!(hash)
  self
end

#open_graph_enabled?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/better_seo/configuration.rb', line 174

def open_graph_enabled?
  open_graph.enabled == true
end

#robots_enabled?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/better_seo/configuration.rb', line 166

def robots_enabled?
  robots.enabled == true
end

#sitemap_enabled?Boolean

Feature enabled checks

Returns:

  • (Boolean)


162
163
164
# File 'lib/better_seo/configuration.rb', line 162

def sitemap_enabled?
  sitemap.enabled == true
end

#structured_data_enabled?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/better_seo/configuration.rb', line 182

def structured_data_enabled?
  structured_data.enabled == true
end

#to_hObject

Convert to hash



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/better_seo/configuration.rb', line 187

def to_h
  {
    site_name: site_name,
    default_locale: default_locale,
    available_locales: available_locales,
    meta_tags: meta_tags.to_h,
    open_graph: open_graph.to_h,
    twitter: twitter.to_h,
    structured_data: structured_data.to_h,
    sitemap: sitemap.to_h,
    robots: robots.to_h,
    images: images.to_h,
    i18n: i18n.to_h
  }
end

#twitter_enabled?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/better_seo/configuration.rb', line 178

def twitter_enabled?
  twitter.enabled == true
end

#validate!Object

Validate configuration

Raises:



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/better_seo/configuration.rb', line 132

def validate!
  errors = []

  # Validate locales
  unless available_locales.is_a?(Array) && available_locales.any?
    errors << "available_locales must be a non-empty array"
  end

  if available_locales.is_a?(Array) && !available_locales.include?(default_locale)
    errors << "default_locale must be included in available_locales"
  end

  # Validate sitemap
  errors << "sitemap.host is required when sitemap is enabled" if sitemap.enabled && sitemap.host.nil?

  # Validate meta tags lengths
  if meta_tags.default_title && meta_tags.default_title.length > 60
    errors << "meta_tags.default_title should be max 60 characters"
  end

  if meta_tags.default_description && meta_tags.default_description.length > 160
    errors << "meta_tags.default_description should be max 160 characters"
  end

  raise ValidationError, errors.join(", ") if errors.any?

  true
end