Class: SEOChecker

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

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ SEOChecker

Returns a new instance of SEOChecker.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/seo_checker.rb', line 13

def initialize(url, options={})
  @url = url
  @locations = []
  @titles = {}
  @descriptions = {}
  @id_urls = {}
  @excessive_keywords = []
  @nesting_subdirectories = []
  @no_titles = []
  @no_descriptions = []
  @unreachables = []
  @batch_size = options[:batch_size] ? options[:batch_size].to_i : nil
  @interval_time = options[:interval_time].to_i
  @logger = options[:logger] == true ? Logger.new(STDOUT) : options[:logger]
end

Instance Method Details

#checkObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/seo_checker.rb', line 29

def check
  begin
    check_robot
    check_sitemap("#{@url}/sitemap.xml") if @locations.empty?
    check_sitemap("#{@url}/sitemap.xml.gz") if @locations.empty?
    raise SEOException, "Error: There is no sitemap.xml or sitemap.xml.gz" if @locations.empty?

    check_location

    report
  rescue SEOException => e
    puts e.message
  end
end

#check_locationObject



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_checker.rb', line 68

def check_location
  @batch_size ||= @locations.size
  @locations.each_slice(@batch_size) do |batch_locations|
    batch_locations.each do |location|
      @logger.debug "checking #{location}" if @logger
      response = get_response(URI.parse(location))
      if response.is_a? Net::HTTPSuccess
        if response.body =~ %r{<head>(.*?)</head>}m
          check_title($1, location)
          check_description($1, location)
        else
          @no_titles << location
          @no_descriptions << location
        end
        check_url(location)
      else
        @unreachables << location
      end
    end
    sleep(@interval_time)
  end
end

#check_robotObject



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

def check_robot
  uri = URI.parse(@url)
  uri.path = '/robots.txt'
  response = get_response(uri)
  if response.is_a? Net::HTTPSuccess and response.body =~ /Sitemap:\s*(.*)/
    check_sitemap($1)
  end
end

#check_sitemap(url) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/seo_checker.rb', line 53

def check_sitemap(url)
  @logger.debug "checking #{url} file" if @logger
  uri = URI.parse(url)
  response = get_response(uri)
  if response.is_a? Net::HTTPSuccess
    body = url =~ /gz$/ ? Zlib::GzipReader.new(StringIO.new(response.body)).read : response.body
    if body.index "<sitemap>"
      sitemap_locs = body.scan(%r{<loc>(.*?)</loc>}).flatten
      sitemap_locs.each { |loc| check_sitemap(loc) }
    else
      @locations = body.scan(%r{<loc>(.*?)</loc>}).flatten
    end
  end
end