Class: DiscourseTheme::Downloader

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

Instance Method Summary collapse

Constructor Details

#initialize(dir:, client:) ⇒ Downloader

Returns a new instance of Downloader.



3
4
5
6
7
# File 'lib/discourse_theme/downloader.rb', line 3

def initialize(dir:, client:)
  @dir = dir
  @client = client
  @theme_id = nil
end

Instance Method Details

#download_theme(id) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/discourse_theme/downloader.rb', line 9

def download_theme(id)
  raw = @client.get_raw_theme_export(id)
  sio = StringIO.new(raw)
  gz = Zlib::GzipReader.new(sio)
  Minitar.unpack(gz, @dir)

  # Minitar extracts into a sub directory, move all the files up one dir
  Dir.chdir(@dir) do
    folders = Dir.glob('*/')
    raise "Extraction failed" unless folders.length == 1
    FileUtils.mv(Dir.glob("#{folders[0]}*"), "./")
    FileUtils.remove_dir(folders[0])
  end
end