Class: StaticGenerator::Crawler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Crawler

Returns a new instance of Crawler.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/static_generator.rb', line 37

def initialize(opts)
  @destination_path = opts[:destination_path]
  @url_prefix = opts[:url_prefix]
  @url = opts[:url]
  if @url_prefix.nil? 
    raise WrongURLPrefixError, "Expected an `url_prefix` option for the given URL."
  elsif @url !~ /^#{@url_prefix}/
    raise WrongURLPrefixError, "Expected the `url_prefix` option to exist in the given URL."
  elsif @url_prefix[-1, 1] != '/'
    raise WrongURLPrefixError, "Expected the `url_prefix` to end with a '/'."
  end

  if ! File.directory? @destination_path
    raise DestinationPathDoesNotExist
  elsif ! File.writable? @destination_path
    raise DestinationPathNotWritableError
  end
end

Instance Attribute Details

#destination_pathObject (readonly)

Returns the value of attribute destination_path.



35
36
37
# File 'lib/static_generator.rb', line 35

def destination_path
  @destination_path
end

#pagesObject (readonly)

Returns the value of attribute pages.



35
36
37
# File 'lib/static_generator.rb', line 35

def pages
  @pages
end

#url_prefixObject (readonly)

Returns the value of attribute url_prefix.



35
36
37
# File 'lib/static_generator.rb', line 35

def url_prefix
  @url_prefix
end

Instance Method Details

#crawl!Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/static_generator.rb', line 56

def crawl!
  @pages = []
  Anemone.crawl(@url) do |anemone|
    anemone.on_every_page do |page|
      @pages << Page.new(page, @url_prefix)
    end
  end
  
  generate_folders
end

#generate_foldersObject



67
68
69
70
71
72
73
# File 'lib/static_generator.rb', line 67

def generate_folders
  @pages.each{|page| 
    directory = File.expand_path(@destination_path)+File::SEPARATOR+(page.short_path.split('/').join(File::SEPARATOR))
    FileUtils.mkdir_p( directory )
    File.open("#{directory}/index.html", 'w') {|f| f.write(page.body) }
  }
end