Skip to content

Commit

Permalink
Implement Yadisk::Main class, write specs, update readme, add .gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegorov committed Jan 23, 2018
1 parent 0b22cf9 commit edf1477
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 9 deletions.
56 changes: 56 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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/
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -39,5 +57,4 @@ Feel free for send me pull request.

## License

MIT

License (MIT) Copyright (c) 2018 Yegorov A. [email protected]
11 changes: 9 additions & 2 deletions bin/yadisk
Original file line number Diff line number Diff line change
@@ -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
26 changes: 25 additions & 1 deletion lib/yadisk.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 10 additions & 2 deletions spec/feature/main_class_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Empty file added spec/files/.gitkeep
Empty file.

0 comments on commit edf1477

Please sign in to comment.