Class: SeoToolbox

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

Overview

@author: Philipp Petersen

@date: 04.06.2009

@license: MIT

Klasse mit Tools für die Suchmaschinenoptimierung.

NOTE

Die Datei sitemap_data.dat ist nach dem folgenden Schema aufgebaut:

  • URL:::lastmod:::changefreq:::priority

INFO

Achten Sie darauf, dass zumindest die sitemap_data.dat in einem Verzeichnis liegt, dass bei dem deployment nicht überschrieben wird.</b>

Constant Summary collapse

VERSION =
"0.9.3"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_to_sitemap, path_to_data, url, changefreq = 'weekly', priority = '1.0') ⇒ SeoToolbox

Mögliche Werte für changefreq:

  • always

  • hourly

  • daily

  • weekly

  • monthly

  • yearly

  • never

Der Typische Wert der priority ist 0.5 und kann zwischen 0.0 und 1.0 liegen.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/seo_toolbox.rb', line 24

def initialize path_to_sitemap, path_to_data, url, changefreq='weekly', priority='1.0'
  @url = url
  @path_to_sitemap = path_to_sitemap
  @changefreq = changefreq
  @priority = priority
  @path_to_data = path_to_data

  #Prüfe ob das Temp-File bereits existiert:
  if not File.exists?("#{path_to_data}/sitemap_data.dat")      
    save_new_url_to_sitemap?(@url, DateTime.now.strftime("%Y-%m-%d"), changefreq, priority)
  else
    #prüfe ob die Sitemap existiert. Existiert diese nicht, lege sie neu an:
    if not File.exists?("#{path_to_sitemap}/sitemap.xml")
      create_xml_sitemap?
    end
  end
end

Instance Attribute Details

#changefreqObject (readonly)

Returns the value of attribute changefreq.



11
12
13
# File 'lib/seo_toolbox.rb', line 11

def changefreq
  @changefreq
end

#path_to_dataObject (readonly)

Returns the value of attribute path_to_data.



11
12
13
# File 'lib/seo_toolbox.rb', line 11

def path_to_data
  @path_to_data
end

#path_to_sitemapObject (readonly)

Returns the value of attribute path_to_sitemap.



11
12
13
# File 'lib/seo_toolbox.rb', line 11

def path_to_sitemap
  @path_to_sitemap
end

#priorityObject (readonly)

Returns the value of attribute priority.



11
12
13
# File 'lib/seo_toolbox.rb', line 11

def priority
  @priority
end

#urlObject (readonly)

Returns the value of attribute url.



11
12
13
# File 'lib/seo_toolbox.rb', line 11

def url
  @url
end

Class Method Details

.include_metatags(options = {}) ⇒ Object

Retuniert alle für die OnPage-Optimierung relevanten Metatags. So können diese leicht in eine Rails-App eingefügt werden. options = => ‘String’, :description => ‘String’, :author => ‘String’, :language => ‘String’, :robots => ‘String’



44
45
46
47
48
49
50
# File 'lib/seo_toolbox.rb', line 44

def self.include_metatags options={}
  meta_str = ""
  options.each_pair { |key, value|
    meta_str << "<meta name=\"#{key}\" content=\"#{value}\" />\n"
  }
  meta_str << "<!--This site use the seo_toolbox gem from Philipp Petersen-->"
end

Instance Method Details

#create_robots_txt?(content, path_to_robots_txt = @path_to_sitemap) ⇒ Boolean

Erstellt die Datei robots.txt in gewählten Verzeichnis

Die URL zur sitemap wird automatisch eingefügt. Wird kein Inhalt für content angegeben, werden die Default-Werte eingesetzt:

  • User-Agent: *

  • Allow: /

Returns:

  • (Boolean)


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/seo_toolbox.rb', line 144

def create_robots_txt? content, path_to_robots_txt=@path_to_sitemap
  begin
    file = File.open("#{path_to_robots_txt}/robots.txt", "w")
    if content.nil?
      file << "User-Agent: *\nAllow: /\n"
    else
      file << content
    end
    file << "Sitemap:#{@url}/sitemap.xml"
    file.close
    return true
  rescue
    return false
  end
end

#create_xml_sitemap?Boolean

Returns:

  • (Boolean)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/seo_toolbox.rb', line 120

def create_xml_sitemap?
  if @url.empty? || @path_to_sitemap.empty?      
    return false
  else
    file = File.open("#{@path_to_sitemap}/sitemap.xml", "w")
    file << get_xml_head

    #Lese Daten ein:
    data_file = File.new("#{@path_to_data}/sitemap_data.dat")
    data_file.each_line{|line|        
      file << get_xml_url_element(line)
    }
    file << get_xml_url_bottom
    data_file.close
    file.close
    return true
  end
end

#remove_url_from_sitemap?(url) ⇒ Boolean

Löscht eine bestehende URL aus der Sitemap

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/seo_toolbox.rb', line 92

def remove_url_from_sitemap? url
  begin
    url = url.gsub("&", "&amp;").gsub("'", "&apos;").gsub("\"", "&quot;").gsub(">", "&gt;").gsub("<", "&lt;")

    file_content = []
    file = File.open("#{@path_to_data}/sitemap_data.dat")
    file.each_line{ |line|
        file_content.push(line)
    }
    file.close

    file_content.each_index do |i|
      if file_content[i].match(/^#{url}/)          
        file_content.delete_at(i)
      end
    end

    #schreibe wieder in das file zurück
    file = File.open("#{@path_to_data}/sitemap_data.dat", "w")
    file_content.each { |item| file << item }
    file.close

    return true
  rescue
    return false
  end
end

#save_new_url_to_sitemap?(url, lastmod, changefreq = @changefreq, priority = @priority) ⇒ Boolean

Speichert eine neue URL in der Sitemap.lastmod=“2009-01-31”

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/seo_toolbox.rb', line 53

def save_new_url_to_sitemap? url, lastmod, changefreq=@changefreq, priority=@priority    
  begin      
    #Encoding:
    url = url.gsub("&", "&amp;").gsub("'", "&apos;").gsub("\"", "&quot;").gsub(">", "&gt;").gsub("<", "&lt;")

    #Prüfe ab, ob der Eintrag schon vorhanden ist:
    if File.exists?("#{@path_to_data}/sitemap_data.dat")
      #Speichere die Datei in einem Array
      file_content = []
      file = File.open("#{@path_to_data}/sitemap_data.dat")
      
      file.each_line{ |line|
        file_content.push(line)
      }
      file.close
      
      file_content.each_index do |i|
        if file_content[i].match(/^#{url}/)
          file_content.delete_at(i)
        end
      end
      file_content.push "#{url}:::#{lastmod}:::#{changefreq}:::#{priority}\n"

      #schreibe wieder in das file zurück
      file = File.open("#{@path_to_data}/sitemap_data.dat", "w")
      file_content.each { |item| file << item }
      file.close
    else        
      file = File.open("#{@path_to_data}/sitemap_data.dat", "w")
      file << "#{url}:::#{lastmod}:::#{changefreq}:::#{priority}\n"
      file.close
    end
    return true
  rescue
    return false
  end
end