Class: CarrierWave::Storage::Yandex::Disk

Inherits:
Abstract
  • Object
show all
Defined in:
lib/carrierwave/yandex/storage/disk.rb

Overview

Rubocop, shut up

Defined Under Namespace

Classes: File

Instance Method Summary collapse

Instance Method Details

#helper_create_publish(file) ⇒ Object

Store a single file



12
13
14
15
16
17
18
19
20
21
# File 'lib/carrierwave/yandex/storage/disk.rb', line 12

def helper_create_publish(file)
  location = "/#{uploader.store_path}"
  location_path = "/#{uploader.store_dir}"

  yandex_disk_client.mkdir_p(location_path)

  yandex_disk_client.put!(file.path.to_s, location)

  yandex_disk_client.make_public(location)
end

#helper_update_model(make_public_res) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/carrierwave/yandex/storage/disk.rb', line 23

def helper_update_model(make_public_res)
  location = "/#{uploader.store_path}"

  public_url = ERB::Util.url_encode make_public_res[:public_url]

  file_id = { location: location, public_url: public_url }.to_json

  uploader.model.update_column uploader.mounted_as, file_id
end

#retrieve!(file_id) ⇒ Object

Retrieve a single file



42
43
44
45
46
47
48
49
50
51
# File 'lib/carrierwave/yandex/storage/disk.rb', line 42

def retrieve!(file_id)
  file_id_hash = (JSON.parse file_id.gsub('=>', ':')).symbolize_keys

  public_url = file_id_hash[:public_url]

  location = file_id_hash[:location]

  CarrierWave::Storage::Yandex::Disk::File
    .new(public_url, location, yandex_disk_client)
end

#store!(file) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/carrierwave/yandex/storage/disk.rb', line 33

def store!(file)
  # I had to break this method into 2 helpers just because
  # rubocop insisted on that. Isn't it stupid?
  res = helper_create_publish(file)

  helper_update_model(res)
end

#yandex_disk_clientObject



53
54
55
56
57
# File 'lib/carrierwave/yandex/storage/disk.rb', line 53

def yandex_disk_client
  @yandex_disk_client ||= begin
    ::Yandex::Disk::Client.new(access_token: config[:access_token])
  end
end