From edf1477eee7f20efa4af01e5627e53cb7277894d Mon Sep 17 00:00:00 2001 From: Yegorov Date: Tue, 23 Jan 2018 21:32:41 +0300 Subject: [PATCH] Implement Yadisk::Main class, write specs, update readme, add .gitignore --- .gitignore | 56 +++++++++++++++++++++++++++++++++ README.md | 25 ++++++++++++--- bin/yadisk | 11 +++++-- lib/yadisk.rb | 26 ++++++++++++++- spec/feature/main_class_spec.rb | 12 +++++-- spec/files/.gitkeep | 0 6 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 .gitignore create mode 100644 spec/files/.gitkeep diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..185e4cd --- /dev/null +++ b/.gitignore @@ -0,0 +1,56 @@ +### Ruby ### +*.gem +*.rbc +/.config +/coverage/ +/InstalledFiles +/pkg/ +/spec/reports/ +/spec/examples.txt +/test/tmp/ +/test/version_tmp/ +/tmp/ + +# Used by dotenv library to load environment variables. +# .env + +## Specific to RubyMotion: +.dat* +.repl_history +build/ +*.bridgesupport +build-iPhoneOS/ +build-iPhoneSimulator/ + +## Specific to RubyMotion (use of CocoaPods): +# +# We recommend against adding the Pods directory to your .gitignore. However +# you should judge for yourself, the pros and cons are mentioned at: +# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control +# +# vendor/Pods/ + +## Documentation cache and generated files: +/.yardoc/ +/_yardoc/ +/doc/ +/rdoc/ + +## Environment normalization: +/.bundle/ +/vendor/bundle +/lib/bundler/man/ + +# for a library or gem, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# Gemfile.lock +# .ruby-version +# .ruby-gemset + +# unless supporting rvm < 1.11.0 or doing something fancy, ignore this: +.rvmrc + +# Ignore backup files +*~ +# Ignore download test files +# /spec/files/ diff --git a/README.md b/README.md index c01a835..d6a6f86 100644 --- a/README.md +++ b/README.md @@ -19,12 +19,30 @@ $ gem install --dev yadisk ``` # Save to current directory -$ yadisk https://yadi.sk/i/UFD +$ yadisk https://yadi.sk/i/HEjuI2Ln3RiRcQ # Save to other directory -$ yadisk https://yadi.sk/i/UFD /path/to/directory +$ yadisk https://yadi.sk/i/HEjuI2Ln3RiRcQ /path/to/directory ``` +## How use for development + +``` +# Local build and install to rubygems +$ gem build yadisk.gemspec && gem install yadisk-* + +# Remove local file and uninstall from rubygems +$ rm yadisk-*.gem && gem uninstall yadisk + +# Run script from local folder +$ ruby -Ilib ./bin/yadisk https://yadi.sk/i/HEjuI2Ln3RiRcQ +``` + +## Dependencies + +* [wget](https://www.gnu.org/software/wget/) +* [ruby](https://www.ruby-lang.org/ru/downloads/) >= 2.2 + ## Test For run test use: @@ -39,5 +57,4 @@ Feel free for send me pull request. ## License -MIT - +License (MIT) Copyright (c) 2018 Yegorov A. yegorov0725@yandex.ru diff --git a/bin/yadisk b/bin/yadisk index 2835736..7b45a23 100755 --- a/bin/yadisk +++ b/bin/yadisk @@ -1,8 +1,15 @@ #!/usr/bin/env ruby - -# handle ARGV, and execute Yadisk::Main class +# encoding: utf-8 require 'yadisk' require 'yadisk/check_runtime' Yadisk::CheckRuntime.check_wget + +if ARGV.length == 1 + Yadisk::Main.new.download(ARGV[0]) +elsif ARGV.length == 2 + Yadisk::Main.new.download(ARGV[0], folder: ARGV[1]) +else + puts "Use: $ yadisk url [/path/to/download/local/folder]\n" +end diff --git a/lib/yadisk.rb b/lib/yadisk.rb index 7a4ec3c..633a172 100644 --- a/lib/yadisk.rb +++ b/lib/yadisk.rb @@ -1 +1,25 @@ -# write Yadisk::Main class +# encoding: utf-8 + +require 'cgi' +require 'cgi/util' +require 'uri' +require 'io/console' +require 'json' + +module Yadisk + class Main + BASE_URL = 'https://cloud-api.yandex.net:443/v1/disk/public/resources/download?public_key=' + def download(url, folder: './') + enc_url = CGI::escape(url) + + puts "wget -qO - #{BASE_URL}#{enc_url}" + res = IO.popen("wget -qO - #{BASE_URL}#{enc_url}").read + json_res = JSON.parse(res) + download_url = json_res['href'] + filename = CGI::parse(URI(download_url).query)["filename"][0] + folder = folder + "/" if not folder.end_with?('/') + + system("wget '#{download_url}' -O '#{folder}#{filename}'") + end + end +end diff --git a/spec/feature/main_class_spec.rb b/spec/feature/main_class_spec.rb index b233051..6831240 100644 --- a/spec/feature/main_class_spec.rb +++ b/spec/feature/main_class_spec.rb @@ -1,6 +1,14 @@ +require 'yadisk' + RSpec.describe Yadisk::Main do - it "#download" do - yadisk = Yadisk::Main.new + let(:yadisk) { Yadisk::Main.new } + + it "has download method" do expect(yadisk).to respond_to(:download) end + + it "must download file" do + yadisk.download("https://yadi.sk/i/HEjuI2Ln3RiRcQ", folder: "./spec/files/") + expect(File.exists?("./spec/files/Обои для рабочего стола.jpg")).to be true + end end diff --git a/spec/files/.gitkeep b/spec/files/.gitkeep new file mode 100644 index 0000000..e69de29