diff --git a/Gemfile.lock b/Gemfile.lock index c2888b9..2eeb4ab 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - yadisk (0.2.0) + yadisk (0.3.0) GEM remote: http://www.rubygems.org/ diff --git a/bin/yadisk b/bin/yadisk index c1c8f38..51c20d8 100755 --- a/bin/yadisk +++ b/bin/yadisk @@ -1,7 +1,7 @@ #!/usr/bin/env ruby # encoding: utf-8 -require 'yadisk' +require 'yadisk/main' require 'yadisk/check_runtime' require 'optparse' diff --git a/lib/yadisk.rb b/lib/yadisk.rb index 8bec843..6a65f34 100644 --- a/lib/yadisk.rb +++ b/lib/yadisk.rb @@ -1,37 +1,4 @@ # encoding: utf-8 -require 'cgi' -require 'cgi/util' -require 'uri' -require 'io/console' -require 'json' -require 'net/http' - -require 'yadisk/os' - module Yadisk - class Main - BASE_URL = 'https://cloud-api.yandex.net:443/v1/disk/public/resources/download?public_key=' - def download(url, folder: ".#{File::SEPARATOR}", wget_options: nil) - enc_url = CGI::escape(url) - response = Net::HTTP.get(URI("#{BASE_URL}#{enc_url}")) - json_res = JSON.parse(response) - download_url = json_res['href'] - filename = CGI::parse(URI(download_url).query)["filename"][0] - folder = folder + File::SEPARATOR if not folder.end_with?(File::SEPARATOR) - download_path = folder + filename - - wget_options = (wget_options || "") + "--no-check-certificate" if Yadisk::OS.windows? - system("wget #{esc(download_url)} -O #{esc(download_path)} #{wget_options}") - end - - private - def esc(str) - "#{escape_symbol}#{str}#{escape_symbol}" - end - - def escape_symbol - Yadisk::OS.windows? ? '"' : "'" - end - end end diff --git a/lib/yadisk/main.rb b/lib/yadisk/main.rb new file mode 100644 index 0000000..8bec843 --- /dev/null +++ b/lib/yadisk/main.rb @@ -0,0 +1,37 @@ +# encoding: utf-8 + +require 'cgi' +require 'cgi/util' +require 'uri' +require 'io/console' +require 'json' +require 'net/http' + +require 'yadisk/os' + +module Yadisk + class Main + BASE_URL = 'https://cloud-api.yandex.net:443/v1/disk/public/resources/download?public_key=' + def download(url, folder: ".#{File::SEPARATOR}", wget_options: nil) + enc_url = CGI::escape(url) + response = Net::HTTP.get(URI("#{BASE_URL}#{enc_url}")) + json_res = JSON.parse(response) + download_url = json_res['href'] + filename = CGI::parse(URI(download_url).query)["filename"][0] + folder = folder + File::SEPARATOR if not folder.end_with?(File::SEPARATOR) + download_path = folder + filename + + wget_options = (wget_options || "") + "--no-check-certificate" if Yadisk::OS.windows? + system("wget #{esc(download_url)} -O #{esc(download_path)} #{wget_options}") + end + + private + def esc(str) + "#{escape_symbol}#{str}#{escape_symbol}" + end + + def escape_symbol + Yadisk::OS.windows? ? '"' : "'" + end + end +end diff --git a/lib/yadisk/version.rb b/lib/yadisk/version.rb index 2c611e4..c25d306 100644 --- a/lib/yadisk/version.rb +++ b/lib/yadisk/version.rb @@ -1,3 +1,3 @@ module Yadisk - VERSION = '0.2.0' + VERSION = '0.3.0' end diff --git a/spec/feature/main_class_spec.rb b/spec/feature/main_class_spec.rb index d3b7fc4..15aac26 100644 --- a/spec/feature/main_class_spec.rb +++ b/spec/feature/main_class_spec.rb @@ -1,4 +1,4 @@ -require 'yadisk' +require 'yadisk/main' RSpec.describe Yadisk::Main do let(:yadisk) { Yadisk::Main.new } diff --git a/yadisk.gemspec b/yadisk.gemspec index f93d909..b242f47 100644 --- a/yadisk.gemspec +++ b/yadisk.gemspec @@ -11,7 +11,7 @@ Gem::Specification.new do |s| s.description = "Command line tool for download file from Yandex.Disk through share link" s.author = "Artem Yegorov" s.email = 'yegorov0725@yandex.ru' - s.files = ["lib/yadisk.rb", "lib/yadisk/check_runtime.rb", "lib/yadisk/version.rb", "lib/yadisk/os.rb"] + s.files = ["lib/yadisk.rb", "lib/yadisk/main.rb", "lib/yadisk/check_runtime.rb", "lib/yadisk/version.rb", "lib/yadisk/os.rb"] s.platform = Gem::Platform::RUBY s.executables << 'yadisk' s.add_development_dependency 'rspec', '~> 3.9', '< 4.0'