Class: Boilercode::Commands::Dotfiles::Download

Inherits:
Boilercode::Command show all
Defined in:
lib/boilercode/commands/dotfiles/download.rb

Instance Method Summary collapse

Methods inherited from Boilercode::Command

#ask, #client, #config, #credentials, #download, #link_to, #logger, #markdown, #open_in_browser, #parse_json, #pastel, #prompt

Constructor Details

#initialize(options) ⇒ Download

Returns a new instance of Download.



9
10
11
# File 'lib/boilercode/commands/dotfiles/download.rb', line 9

def initialize(options)
  @options = options
end

Instance Method Details

#choices(response, headers) ⇒ Object



56
57
58
59
60
61
# File 'lib/boilercode/commands/dotfiles/download.rb', line 56

def choices(response, headers)
  choices = response.map { |upload| upload["filename"] }

  choices << "Next Page" if headers["Total-Pages"].to_i > 1
  choices
end

#download_all(response) ⇒ Object



35
36
37
38
39
40
# File 'lib/boilercode/commands/dotfiles/download.rb', line 35

def download_all(response)
  response.each do |upload|
    download(upload["url"])
    puts pastel.green(upload["url"])
  end
end

#download_file?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/boilercode/commands/dotfiles/download.rb', line 63

def download_file?
  ask("Do you want to download the file? (y/n)") == "y"
end

#execute(input: $stdin, output: $stdout, page: 1) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/boilercode/commands/dotfiles/download.rb', line 13

def execute(input: $stdin, output: $stdout, page: 1)
  res = client.get("/dotfiles?page=#{page}")
  if res.success?
    output.puts handle_search_response(res)
  else
    output.puts pastel.red res.inspect
  end
end

#handle_search_response(res) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/boilercode/commands/dotfiles/download.rb', line 22

def handle_search_response(res)
  headers = res.headers
  response = parse_json(res)
  Dir.chdir(File.expand_path("~"))
  if response.empty?
    pastel.red "No uploads found, try updating your query."
  elsif prompt.yes?("Do you want to download all dotfiles?")
    download_all(response)
  else
    handle_selecting_dotfiles(response, headers)
  end
end

#handle_selecting_dotfiles(response, headers) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/boilercode/commands/dotfiles/download.rb', line 42

def handle_selecting_dotfiles(response, headers)
  dotfiles = prompt.multi_select("Choose your dotfiles:", choices(response, headers))
  # choice = dotfiles.first
  # if choice == "Next Page"
  #   return execute(page: headers["Current-Page"].to_i + 1)
  # end
  dotfiles.each do |file|
    upload = response.find { |dotfile| dotfile["filename"] == file }

    download(upload["url"]) if download_file?
    puts pastel.green(upload["url"])
  end
end