Class: Insup::Uploader::InsalesUploader

Inherits:
Insup::Uploader show all
Defined in:
lib/insup/uploader/insales_uploader.rb

Constant Summary collapse

InsalesUploaderError =
Class.new(Insup::Exceptions::UploaderError)

Instance Method Summary collapse

Methods inherited from Insup::Uploader

#batch_upload, #delete_file, find_uploader, uploader

Constructor Details

#initialize(config) ⇒ InsalesUploader

Returns a new instance of InsalesUploader.



10
11
12
13
14
# File 'lib/insup/uploader/insales_uploader.rb', line 10

def initialize(config)
  super
  Insup::Insales.configure_api
  assets_list(true)
end

Instance Method Details

#remove_file(file) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/insup/uploader/insales_uploader.rb', line 73

def remove_file(file)
  asset = find_asset(file)

  if !asset
    raise InsalesUploaderError, "Cannot find remote counterpart for file #{file.path}"
  end

  puts "Deleting #{file.path}".red
  asset.destroy
  assets_list.delete(asset)
end

#upload_file(file) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/insup/uploader/insales_uploader.rb', line 16

def upload_file(file)
  case file.state
  when Insup::TrackedFile::NEW
    upload_new_file(file)
  when Insup::TrackedFile::MODIFIED
    upload_modified_file(file)
  when Insup::TrackedFile::UNSURE
    upload_modified_file(file)
  end
end

#upload_modified_file(file) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/insup/uploader/insales_uploader.rb', line 54

def upload_modified_file(file)
  asset = find_asset(file)

  if !asset
    upload_new_file(file)
  end

  puts "Updating #{file.path}".yellow

  file_contents = File.read(file.path)

  if asset.content_type.start_with? 'text/'
    asset.update_attribute(:content, file_contents)
  else
    remove_file(file)
    upload_new_file(file)
  end
end

#upload_new_file(file) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/insup/uploader/insales_uploader.rb', line 27

def upload_new_file(file)
  asset = find_asset(file)

  if !asset.nil?
    upload_modified_file(file)
    return
  end

  puts "Creating #{file.path}".green
  asset_type = get_asset_type(file.path)

  if(!asset_type)
    raise InsalesUploaderError, "Cannot determine asset type for file #{file.path}"
  end

  file_contents = File.read(file.path)

  asset = ::Insup::Insales::Asset.create({
    name: file.file_name,
    attachment: Base64.encode64(file_contents),
    theme_id: @config['theme_id'],
    type: asset_type
  })

  assets_list << asset
end