Class: BetterSeo::Sitemap::UrlEntry

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

Constant Summary collapse

VALID_CHANGEFREQ =
%w[always hourly daily weekly monthly yearly never].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loc, lastmod: nil, changefreq: "weekly", priority: 0.5) ⇒ UrlEntry

Returns a new instance of UrlEntry.



13
14
15
16
17
18
19
20
21
# File 'lib/better_seo/sitemap/url_entry.rb', line 13

def initialize(loc, lastmod: nil, changefreq: "weekly", priority: 0.5)
  @loc = loc
  @lastmod = format_date(lastmod) if lastmod
  @changefreq = changefreq
  @priority = priority
  @alternates = []
  @images = []
  @videos = []
end

Instance Attribute Details

#alternatesObject (readonly)

Returns the value of attribute alternates.



11
12
13
# File 'lib/better_seo/sitemap/url_entry.rb', line 11

def alternates
  @alternates
end

#changefreqObject

Returns the value of attribute changefreq.



11
12
13
# File 'lib/better_seo/sitemap/url_entry.rb', line 11

def changefreq
  @changefreq
end

#imagesObject (readonly)

Returns the value of attribute images.



11
12
13
# File 'lib/better_seo/sitemap/url_entry.rb', line 11

def images
  @images
end

#lastmodObject

Returns the value of attribute lastmod.



11
12
13
# File 'lib/better_seo/sitemap/url_entry.rb', line 11

def lastmod
  @lastmod
end

#locObject (readonly)

Returns the value of attribute loc.



11
12
13
# File 'lib/better_seo/sitemap/url_entry.rb', line 11

def loc
  @loc
end

#priorityObject

Returns the value of attribute priority.



11
12
13
# File 'lib/better_seo/sitemap/url_entry.rb', line 11

def priority
  @priority
end

#videosObject (readonly)

Returns the value of attribute videos.



11
12
13
# File 'lib/better_seo/sitemap/url_entry.rb', line 11

def videos
  @videos
end

Instance Method Details

#add_alternate(href, hreflang:) ⇒ Object

Add alternate language version (hreflang)



42
43
44
45
# File 'lib/better_seo/sitemap/url_entry.rb', line 42

def add_alternate(href, hreflang:)
  @alternates << { href: href, hreflang: hreflang }
  self
end

#add_image(loc, title: nil, caption: nil) ⇒ Object

Add image



48
49
50
51
52
53
54
# File 'lib/better_seo/sitemap/url_entry.rb', line 48

def add_image(loc, title: nil, caption: nil)
  image = { loc: loc }
  image[:title] = title if title
  image[:caption] = caption if caption
  @images << image
  self
end

#add_video(thumbnail_loc:, title:, description:, content_loc:, duration: nil) ⇒ Object

Add video



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/better_seo/sitemap/url_entry.rb', line 57

def add_video(thumbnail_loc:, title:, description:, content_loc:, duration: nil)
  video = {
    thumbnail_loc: thumbnail_loc,
    title: title,
    description: description,
    content_loc: content_loc
  }
  video[:duration] = duration if duration
  @videos << video
  self
end

#to_hObject



106
107
108
109
110
111
112
113
114
115
# File 'lib/better_seo/sitemap/url_entry.rb', line 106

def to_h
  hash = {
    loc: @loc,
    changefreq: @changefreq,
    priority: @priority
  }
  hash[:lastmod] = @lastmod if @lastmod
  hash[:alternates] = @alternates if @alternates.any?
  hash
end

#to_xmlObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/better_seo/sitemap/url_entry.rb', line 69

def to_xml
  xml = []
  xml << "  <url>"
  xml << "    <loc>#{escape_xml(@loc)}</loc>"
  xml << "    <lastmod>#{@lastmod}</lastmod>" if @lastmod
  xml << "    <changefreq>#{@changefreq}</changefreq>"
  xml << "    <priority>#{@priority}</priority>"

  # Add images
  @images.each do |image|
    xml << "    <image:image>"
    xml << "      <image:loc>#{escape_xml(image[:loc])}</image:loc>"
    xml << "      <image:title>#{escape_xml(image[:title])}</image:title>" if image[:title]
    xml << "      <image:caption>#{escape_xml(image[:caption])}</image:caption>" if image[:caption]
    xml << "    </image:image>"
  end

  # Add videos
  @videos.each do |video|
    xml << "    <video:video>"
    xml << "      <video:thumbnail_loc>#{escape_xml(video[:thumbnail_loc])}</video:thumbnail_loc>"
    xml << "      <video:title>#{escape_xml(video[:title])}</video:title>"
    xml << "      <video:description>#{escape_xml(video[:description])}</video:description>"
    xml << "      <video:content_loc>#{escape_xml(video[:content_loc])}</video:content_loc>"
    xml << "      <video:duration>#{video[:duration]}</video:duration>" if video[:duration]
    xml << "    </video:video>"
  end

  # Add hreflang alternates
  @alternates.each do |alternate|
    xml << "    <xhtml:link rel=\"alternate\" hreflang=\"#{alternate[:hreflang]}\" href=\"#{escape_xml(alternate[:href])}\" />"
  end

  xml << "  </url>"
  xml.join("\n")
end

#validate!Object

Raises:



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/better_seo/sitemap/url_entry.rb', line 117

def validate!
  raise ValidationError, "Location is required" if @loc.nil? || @loc.empty?

  begin
    uri = URI.parse(@loc)
    raise ValidationError, "Invalid URL format: #{@loc}" unless uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS)
  rescue URI::InvalidURIError
    raise ValidationError, "Invalid URL format: #{@loc}"
  end

  true
end