Skip to content

Support gem Backup #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
5. Create a new Pull Request
10 changes: 7 additions & 3 deletions lib/yandex-api/disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down
64 changes: 64 additions & 0 deletions lib/yandex-api/disk/backup_storage.rb
Original file line number Diff line number Diff line change
@@ -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