Class: Broadway::Runner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Runner

Returns a new instance of Runner.



7
8
9
10
11
# File 'lib/broadway/runner.rb', line 7

def initialize(site)
  self.site = site
  self.url = URI.parse(site.config[:url])
  self.content = site.pages + site.posts
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



5
6
7
# File 'lib/broadway/runner.rb', line 5

def content
  @content
end

#siteObject

Returns the value of attribute site.



5
6
7
# File 'lib/broadway/runner.rb', line 5

def site
  @site
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/broadway/runner.rb', line 5

def url
  @url
end

Instance Method Details

#error?(response, path, whiny = true) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
# File 'lib/broadway/runner.rb', line 38

def error?(response, path, whiny = true)
  if !response.is_a?(Net::HTTPSuccess)
    raise message if whiny
    puts "Error at '#{path}': #{response.to_s}"
    return true
  end
  false
end

#run(&block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/broadway/runner.rb', line 13

def run(&block)
  begin
    Net::HTTP.start(url.host, url.port) do |http|
      self.content.each do |item|
        response = http.get(item.url)
        # if the file doesn't exist or is not rendered correctly
        next if error?(response, item.url, false)
        write_dir = File.expand_path(File.join(site.config[:destination], item.dir))
        name = File.join(write_dir, "index.html")
        FileUtils.mkdir_p(write_dir)
        text = ""
        text << "---\n"
        text << "categories: #{item.categories.sort.join(" ")}\n"
        text << "---\n"
        text << response.body
        yield text if block_given?
        File.write(name, text)
      end
    end
  rescue Exception => e
    puts e.inspect
    raise "Make sure you've started the server, run: 'ruby app.rb'"
  end
end