From bbfc5ee7f5023a74004ecba1d3b9b595f86de612 Mon Sep 17 00:00:00 2001 From: lorenso Date: Mon, 30 Oct 2017 12:33:46 +0300 Subject: [PATCH] Add gem Backup --- README.md | 22 ++++++++- lib/yandex-api/disk.rb | 10 +++-- lib/yandex-api/disk/backup_storage.rb | 64 +++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 lib/yandex-api/disk/backup_storage.rb diff --git a/README.md b/README.md index 2d0d083..5550acb 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,13 @@ Modules: * Translate - contain methods for work with Yandex.Translate (http://translate.yandex.ru/) * Disk - contain methods for work with Yandex.Disk (http://disk.yandex.ru/) +To get your `access_token`: + +1. Register your app at [Yandex](https://oauth.yandex.ru/client/new) + 1. Be sure to check 'Yandex.Disk permits' + 2. Be sure to check 'Client for development' (it will set https://oauth.yandex.ru/verification_code?dev=True as `Callback URI`) +2. Get access token https://oauth.yandex.ru/authorize?response_type=token&client_id=YOUR_APP_ID +3. Get your access token from redirect url (right from the browser, it will be one of parameters) ## Installation @@ -79,7 +86,7 @@ Create configuration file yandex_translate.yml token: "token" ui: "ru" verbose: true - + Create yandex_translate.rb in config/initializers Yandex::API::Translate.load File.join(Rails.root,"config","yandex_translate.yml"), Rails.env @@ -134,10 +141,21 @@ Create configuration file yandex_disk.yml Storage.mkdir(path) # create directory Storage.exists?(path) # existing path or not? +### Using it with [backup gem](https://github.com/meskyanichi/backup) + + require 'yandex-api/disk/backup_storage' + + Backup::Model.new(:my_backup, 'Description for my_backup') do + store_with Yandex::API::Disk::BackupStorage do |disk| + disk.access_token = 'YOUR_ACCESS_TOKEN' + disk.keep = 5 + end + end + ## Contributing 1. Fork it ( https://github.com/jpascal/yandex-api/fork ) 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) -5. Create a new Pull Request \ No newline at end of file +5. Create a new Pull Request diff --git a/lib/yandex-api/disk.rb b/lib/yandex-api/disk.rb index 08c5048..944830f 100644 --- a/lib/yandex-api/disk.rb +++ b/lib/yandex-api/disk.rb @@ -13,10 +13,14 @@ def self.configuration @configuration end + def self.configuration=(config) + @configuration = config + end + def self.load file, env = nil @environment = env.to_s if env config = YAML.load_file(file) - @configuration = defined?(@environment) ? config[@environment] : config + self.configuration = defined?(@environment) ? config[@environment] : config end def self.parse_json json @@ -26,7 +30,7 @@ def self.parse_json json raise RuntimeError.new "#{e.message} in response" end end - + def self.connection return @connection if defined? @connection uri = URI.parse(URL_API) @@ -96,7 +100,7 @@ def initialize(hash={}) class Object < BaseStruct.new(:name, :size, :created, :modified, :mime_type, :md5, :media_type, :path) end - + class Folder < BaseStruct.new(:name, :path, :modified, :created) end diff --git a/lib/yandex-api/disk/backup_storage.rb b/lib/yandex-api/disk/backup_storage.rb new file mode 100644 index 0000000..34fb202 --- /dev/null +++ b/lib/yandex-api/disk/backup_storage.rb @@ -0,0 +1,64 @@ +require 'yandex-api' + +module Yandex + module API + module Disk + class BackupStorage < Backup::Storage::Base + include Backup::Storage::Cycler + + class Error < Backup::Error; end + + attr_accessor :access_token + attr_accessor :path + attr_accessor :verbose + + def initialize(model, storage_id = nil, &block) + super + instance_eval(&block) if block_given? + @path ||= 'app:/' + @verbose ||= true + end + + def connection + unless @connection + Disk.configuration = { 'token' => access_token, 'verbose' => verbose } + @connection = Disk::Storage + end + @connection + end + + def transfer! + tmp = Pathname.new(@path) + remote_path.scan(/\/([^\/]+)/).each do |dir| + tmp = tmp.join(dir[0]) + connection.mkdir(tmp) + end + + package.filenames.each do |filename| + src = File.join(Backup::Config.tmp_path, filename) + dest = File.join(remote_path, filename) + Backup::Logger.info "Storing '#{ dest }'..." + connection.write!(File.open(src), dest) + end + rescue => e + ::Backup::Logger.error "Error: #{e.message}" + raise + end + + def remove!(package) + Backup::Logger.info "Removing backup package dated #{ package.time }..." + remote_path = remote_path_for(package) + connection.rm!(remote_path) + rescue => e + return if e.message.empty? + Backup::Storage::Base::Logger.warn "Error: #{e.message}" + raise + end + + def storage_name + 'Yandex::Disk' + end + end + end + end +end