Class: SharenowCli::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/sharenow_cli/cli.rb

Constant Summary collapse

BASE_URL =
'https://www.share-now.me'.freeze
SharenowError =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#push(target_file_paths) ⇒ Object

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sharenow_cli/cli.rb', line 17

def push(target_file_paths)
  raise SharenowError.new('API token is not set in ENV["SHARENOW_API_TOKEN"] variable') if ENV['SHARENOW_API_TOKEN'].nil?

  title, desc, delete_limit = options[:title], options[:desc], options[:delete_limit]

  target_file_paths = target_file_paths.split(' ')

  raise SharenowError.new('The correct file path is not specified') if target_file_paths.size == 0

  upload_files = target_file_paths.inject([]) do |upload_files, file_path|
    file_paths = Dir.glob(file_path)

    raise SharenowError.new('Directory is not supported') if file_paths.find{|file| FileTest.directory?(file) }

    upload_ios = file_paths.map { |file_path| Faraday::UploadIO.new(file_path,  MIME::Types.type_for(file_path)[0].to_s) }
    upload_files.concat(upload_ios).uniq
  end

  res = faraday_multipart_conn("#{BASE_URL}/api/v1/contents").post do |req|
    req.body = body_params(title, desc, delete_limit, upload_files)
  end

  if res.status == 201
    res_body =  JSON.parse(res.body)
    puts res_body['shared_link']
  elsif res.status == 400
    puts JSON.parse(res.body).join("\n")
  else
    puts res.body
  end
end