Class: MiddlemanTorrent::Extension

Inherits:
Middleman::Extension
  • Object
show all
Defined in:
lib/middleman-torrent/extension.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options_hash = {}, &block) ⇒ Extension

Returns a new instance of Extension.



8
9
10
11
# File 'lib/middleman-torrent/extension.rb', line 8

def initialize(app, options_hash={}, &block)
  super
  require 'mktorrent'
end

Instance Method Details

#after_build(builder) ⇒ Object

Create the torrent after the site is built



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/middleman-torrent/extension.rb', line 22

def after_build(builder)
  torrent = Torrent.new options.tracker

  torrent.defaultdir = options.name
  torrent.set_private if options.private

  # Move to build_dir so it's not added to the torrent
  within_build_path do
    app.sitemap.resources.each do |file|
      # We decode the path because it can have spaces (%20) and stuff
      torrent.add_file URI.decode(file.path)
      builder.say_status 'to torrent', URI.decode(file.path)
    end

    torrent.write_torrent options.file
  end

  builder.say_status :create, File.join(app.config.build_dir, options.file)
end

#after_configurationObject

Expose configuration values



14
15
16
17
18
19
# File 'lib/middleman-torrent/extension.rb', line 14

def after_configuration
  app.set :torrent_tracker, options.tracker
  app.set :torrent_file, options.file
  app.set :torrent_name, options.name
  app.set :torrent_private, options.private
end

#within_build_path(&block) ⇒ Object

Move to build dir for the block



43
44
45
46
47
# File 'lib/middleman-torrent/extension.rb', line 43

def within_build_path(&block)
  Dir.chdir app.config.build_dir do
    yield
  end
end